1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11: namespace expect\matcher;
12:
13: use expect\FailedMessage;
14: use expect\Matcher;
15: use expect\matcher\strategy\ArrayInclusionStrategy;
16: use expect\matcher\strategy\StringInclusionStrategy;
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: final class ToContain implements ReportableMatcher
36: {
37: 38: 39:
40: private $type;
41:
42: 43: 44:
45: private $actual;
46:
47: 48: 49:
50: private $matchResult;
51:
52: 53: 54:
55: private $expectValues;
56:
57: 58: 59:
60: public function __construct($expected)
61: {
62: $this->expectValues = is_array($expected) ? $expected : [$expected];
63: }
64:
65: 66: 67:
68: public function match($actual)
69: {
70: $this->actual = $actual;
71:
72: $strategy = $this->createStrategy();
73: $this->matchResult = $strategy->match($this->expectValues);
74:
75: return $this->matchResult->isMatched();
76: }
77:
78: 79: 80:
81: public function reportFailed(FailedMessage $message)
82: {
83: $unmatchResults = $this->matchResult->getUnmatchResults();
84:
85: $message->appendText('Expected ')
86: ->appendText($this->type)
87: ->appendText(' to contain ')
88: ->appendValues($unmatchResults);
89: }
90:
91: 92: 93:
94: public function reportNegativeFailed(FailedMessage $message)
95: {
96: $matchResults = $this->matchResult->getMatchResults();
97:
98: $message->appendText('Expected ')
99: ->appendText($this->type)
100: ->appendText(' not to contain ')
101: ->appendValues($matchResults);
102: }
103:
104: private function createStrategy()
105: {
106: $strategy = null;
107:
108: if (is_string($this->actual)) {
109: $this->type = 'string';
110: $strategy = new StringInclusionStrategy($this->actual);
111: } elseif (is_array($this->actual)) {
112: $this->type = 'array';
113: $strategy = new ArrayInclusionStrategy($this->actual);
114: }
115:
116: return $strategy;
117: }
118: }
119: