1 | <?php |
||
15 | class PropertyMatcher extends AbstractMatcher |
||
16 | { |
||
17 | /** |
||
18 | * @var string|int |
||
19 | */ |
||
20 | protected $key; |
||
21 | |||
22 | /** |
||
23 | * @var mixed |
||
24 | */ |
||
25 | protected $value; |
||
26 | |||
27 | /** |
||
28 | * @var mixed |
||
29 | */ |
||
30 | protected $actualValue; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $actualValueSet = false; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $isDeep = false; |
||
41 | |||
42 | /** |
||
43 | * @param mixed $key |
||
44 | * @param string $value |
||
45 | */ |
||
46 | public function __construct($key, $value = '') |
||
52 | |||
53 | /** |
||
54 | * Return the expected object or array key. |
||
55 | * |
||
56 | * @return int|string |
||
57 | */ |
||
58 | public function getKey() |
||
62 | |||
63 | /** |
||
64 | * Set the expected object or array key. |
||
65 | * |
||
66 | * @param int|string $key |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function setKey($key) |
||
75 | |||
76 | /** |
||
77 | * Return the expected property value. |
||
78 | * |
||
79 | * @return mixed |
||
80 | */ |
||
81 | public function getValue() |
||
85 | |||
86 | /** |
||
87 | * Set the expected property value. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function setValue($value) |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | * |
||
102 | * @return TemplateInterface |
||
103 | */ |
||
104 | public function getDefaultTemplate() |
||
119 | |||
120 | /** |
||
121 | * Return the actual value given to the matcher. |
||
122 | * |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function getActualValue() |
||
129 | |||
130 | /** |
||
131 | * Set the actual value given to the matcher. Used to |
||
132 | * store whether or not the actual value was set. |
||
133 | * |
||
134 | * @param mixed $actualValue |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setActualValue($actualValue) |
||
144 | |||
145 | /** |
||
146 | * Return if the actual value has been set. |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function isActualValueSet() |
||
154 | |||
155 | /** |
||
156 | * Tell the property matcher to match deep properties. |
||
157 | * |
||
158 | * return $this |
||
159 | */ |
||
160 | public function setIsDeep($isDeep) |
||
166 | |||
167 | /** |
||
168 | * Return whether or not the matcher is matching deep properties. |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isDeep() |
||
176 | |||
177 | /** |
||
178 | * Matches if the actual value has a property, optionally matching |
||
179 | * the expected value of that property. If the deep flag is set, |
||
180 | * the matcher will use the ObjectPath utility to parse deep expressions. |
||
181 | * |
||
182 | * @code |
||
183 | * |
||
184 | * $this->doMatch('child->name->first', 'brian'); |
||
185 | * |
||
186 | * @endcode |
||
187 | * |
||
188 | * @param mixed $actual |
||
189 | * @return mixed |
||
190 | */ |
||
191 | protected function doMatch($actual) |
||
203 | |||
204 | /** |
||
205 | * Convert the actual value to an array, whether it is an object or an array. |
||
206 | * |
||
207 | * @param object|array $actual |
||
208 | * @return array|object |
||
209 | */ |
||
210 | protected function actualToArray($actual) |
||
218 | |||
219 | /** |
||
220 | * Match that an array index exists, and matches |
||
221 | * the expected value if set. |
||
222 | * |
||
223 | * @param $actual |
||
224 | * @return bool |
||
225 | */ |
||
226 | protected function matchArrayIndex($actual) |
||
236 | |||
237 | /** |
||
238 | * Uses ObjectPath to parse an expression if the deep flag |
||
239 | * is set. |
||
240 | * |
||
241 | * @param $actual |
||
242 | * @return bool |
||
243 | */ |
||
244 | protected function matchDeep($actual) |
||
245 | { |
||
246 | $path = new ObjectPath($actual); |
||
247 | $value = $path->get($this->getKey()); |
||
248 | |||
249 | if ($value === null) { |
||
250 | return false; |
||
251 | } |
||
252 | |||
253 | $this->assertion->setActual($value->getPropertyValue()); |
||
254 | |||
255 | return $this->isExpected($value->getPropertyValue()); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Check if the given value is expected. |
||
260 | * |
||
261 | * @param $value |
||
262 | * @return bool |
||
263 | */ |
||
264 | protected function isExpected($value) |
||
265 | { |
||
266 | if ($expected = $this->getValue()) { |
||
267 | $this->setActualValue($value); |
||
268 | |||
269 | return $this->getActualValue() === $expected; |
||
270 | } |
||
271 | |||
272 | return true; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Ensure that the actual value is an object or an array. |
||
277 | * |
||
278 | * @param $actual |
||
279 | */ |
||
280 | protected function validateActual($actual) |
||
286 | |||
287 | /** |
||
288 | * Returns the strings used in creating the template for the matcher. |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | protected function getTemplateStrings() |
||
309 | } |
||
310 |