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