|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Test |
|
17 * @subpackage PHPUnit |
|
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id$ |
|
21 */ |
|
22 |
|
23 /** @see Zend_Dom_Query */ |
|
24 require_once 'Zend/Dom/Query.php'; |
|
25 |
|
26 /** |
|
27 * Zend_Dom_Query-based PHPUnit Constraint |
|
28 * |
|
29 * @uses PHPUnit_Framework_Constraint |
|
30 * @category Zend |
|
31 * @package Zend_Test |
|
32 * @subpackage PHPUnit |
|
33 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
|
34 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
35 */ |
|
36 class Zend_Test_PHPUnit_Constraint_DomQuery41 extends PHPUnit_Framework_Constraint |
|
37 { |
|
38 /**#@+ |
|
39 * Assertion type constants |
|
40 */ |
|
41 const ASSERT_QUERY = 'assertQuery'; |
|
42 const ASSERT_CONTENT_CONTAINS = 'assertQueryContentContains'; |
|
43 const ASSERT_CONTENT_REGEX = 'assertQueryContentRegex'; |
|
44 const ASSERT_CONTENT_COUNT = 'assertQueryCount'; |
|
45 const ASSERT_CONTENT_COUNT_MIN= 'assertQueryCountMin'; |
|
46 const ASSERT_CONTENT_COUNT_MAX= 'assertQueryCountMax'; |
|
47 /**#@-*/ |
|
48 |
|
49 /** |
|
50 * Current assertion type |
|
51 * @var string |
|
52 */ |
|
53 protected $_assertType = null; |
|
54 |
|
55 /** |
|
56 * Available assertion types |
|
57 * @var array |
|
58 */ |
|
59 protected $_assertTypes = array( |
|
60 self::ASSERT_QUERY, |
|
61 self::ASSERT_CONTENT_CONTAINS, |
|
62 self::ASSERT_CONTENT_REGEX, |
|
63 self::ASSERT_CONTENT_COUNT, |
|
64 self::ASSERT_CONTENT_COUNT_MIN, |
|
65 self::ASSERT_CONTENT_COUNT_MAX, |
|
66 ); |
|
67 |
|
68 /** |
|
69 * Content being matched |
|
70 * @var string |
|
71 */ |
|
72 protected $_content = null; |
|
73 |
|
74 /** |
|
75 * Whether or not assertion is negated |
|
76 * @var bool |
|
77 */ |
|
78 protected $_negate = false; |
|
79 |
|
80 /** |
|
81 * CSS selector or XPath path to select against |
|
82 * @var string |
|
83 */ |
|
84 protected $_path = null; |
|
85 |
|
86 /** |
|
87 * Whether or not to use XPath when querying |
|
88 * @var bool |
|
89 */ |
|
90 protected $_useXpath = false; |
|
91 |
|
92 /** |
|
93 * XPath namespaces |
|
94 * @var array |
|
95 */ |
|
96 protected $_xpathNamespaces = array(); |
|
97 |
|
98 /** |
|
99 * Constructor; setup constraint state |
|
100 * |
|
101 * @param string $path CSS selector path |
|
102 * @return void |
|
103 */ |
|
104 public function __construct($path) |
|
105 { |
|
106 $this->_path = $path; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Indicate negative match |
|
111 * |
|
112 * @param bool $flag |
|
113 * @return void |
|
114 */ |
|
115 public function setNegate($flag = true) |
|
116 { |
|
117 $this->_negate = $flag; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Whether or not path is a straight XPath expression |
|
122 * |
|
123 * @param bool $flag |
|
124 * @return Zend_Test_PHPUnit_Constraint_DomQuery |
|
125 */ |
|
126 public function setUseXpath($flag = true) |
|
127 { |
|
128 $this->_useXpath = (bool) $flag; |
|
129 return $this; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Evaluate an object to see if it fits the constraints |
|
134 * |
|
135 * @param string Response content to be matched against (haystack) |
|
136 * @param null|string Assertion type |
|
137 * @param string (optional) String to match (needle), may be required depending on assertion type |
|
138 * @return bool |
|
139 * |
|
140 * NOTE: |
|
141 * Drastic changes up to PHPUnit 3.5.15 this was: |
|
142 * public function evaluate($other, $assertType = null) |
|
143 * In PHPUnit 3.6.0 they changed the interface into this: |
|
144 * public function evaluate($other, $description = '', $returnResult = FALSE) |
|
145 * We use the new interface for PHP-strict checking, but emulate the old one |
|
146 */ |
|
147 public function evaluate($content, $assertType = '', $match = FALSE) |
|
148 { |
|
149 if (strstr($assertType, 'Not')) { |
|
150 $this->setNegate(true); |
|
151 $assertType = str_replace('Not', '', $assertType); |
|
152 } |
|
153 |
|
154 if (strstr($assertType, 'Xpath')) { |
|
155 $this->setUseXpath(true); |
|
156 $assertType = str_replace('Xpath', 'Query', $assertType); |
|
157 } |
|
158 |
|
159 if (!in_array($assertType, $this->_assertTypes)) { |
|
160 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
161 throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__)); |
|
162 } |
|
163 |
|
164 $this->_assertType = $assertType; |
|
165 |
|
166 $method = $this->_useXpath ? 'queryXpath' : 'query'; |
|
167 $domQuery = new Zend_Dom_Query($content); |
|
168 $domQuery->registerXpathNamespaces($this->_xpathNamespaces); |
|
169 $result = $domQuery->$method($this->_path); |
|
170 |
|
171 switch ($assertType) { |
|
172 case self::ASSERT_CONTENT_CONTAINS: |
|
173 if (!$match) { |
|
174 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
175 throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match'); |
|
176 } |
|
177 $this->_content = $match; |
|
178 return ($this->_negate) |
|
179 ? $this->_notMatchContent($result, $match) |
|
180 : $this->_matchContent($result, $match); |
|
181 case self::ASSERT_CONTENT_REGEX: |
|
182 if (!$match) { |
|
183 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
184 throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match'); |
|
185 } |
|
186 $this->_content = $match; |
|
187 return ($this->_negate) |
|
188 ? $this->_notRegexContent($result, $match) |
|
189 : $this->_regexContent($result, $match); |
|
190 case self::ASSERT_CONTENT_COUNT: |
|
191 case self::ASSERT_CONTENT_COUNT_MIN: |
|
192 case self::ASSERT_CONTENT_COUNT_MAX: |
|
193 if ($match === false) { |
|
194 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
195 throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare'); |
|
196 } |
|
197 $this->_content = $match; |
|
198 return $this->_countContent($result, $match, $assertType); |
|
199 case self::ASSERT_QUERY: |
|
200 default: |
|
201 if ($this->_negate) { |
|
202 return (0 == count($result)); |
|
203 } else { |
|
204 return (0 != count($result)); |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 /** |
|
210 * Report Failure |
|
211 * |
|
212 * @see PHPUnit_Framework_Constraint for implementation details |
|
213 * @param mixed CSS selector path |
|
214 * @param string Failure description |
|
215 * @param object Cannot be used, null |
|
216 * @return void |
|
217 * @throws PHPUnit_Framework_ExpectationFailedException |
|
218 * NOTE: |
|
219 * Drastic changes up to PHPUnit 3.5.15 this was: |
|
220 * public function fail($other, $description, $not = false) |
|
221 * In PHPUnit 3.6.0 they changed the interface into this: |
|
222 * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL) |
|
223 * We use the new interface for PHP-strict checking |
|
224 * NOTE 2: |
|
225 * Interface changed again in PHPUnit 4.1.0 because of refactoring to SebastianBergmann\Comparator |
|
226 */ |
|
227 public function fail($other, $description, \SebastianBergmann\Comparator\ComparisonFailure $cannot_be_used = NULL) |
|
228 { |
|
229 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
230 switch ($this->_assertType) { |
|
231 case self::ASSERT_CONTENT_CONTAINS: |
|
232 $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"'; |
|
233 if ($this->_negate) { |
|
234 $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"'; |
|
235 } |
|
236 $failure = sprintf($failure, $other, $this->_content); |
|
237 break; |
|
238 case self::ASSERT_CONTENT_REGEX: |
|
239 $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"'; |
|
240 if ($this->_negate) { |
|
241 $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"'; |
|
242 } |
|
243 $failure = sprintf($failure, $other, $this->_content); |
|
244 break; |
|
245 case self::ASSERT_CONTENT_COUNT: |
|
246 $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times'; |
|
247 if ($this->_negate) { |
|
248 $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times'; |
|
249 } |
|
250 $failure = sprintf($failure, $other, $this->_content); |
|
251 break; |
|
252 case self::ASSERT_CONTENT_COUNT_MIN: |
|
253 $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times'; |
|
254 $failure = sprintf($failure, $other, $this->_content); |
|
255 break; |
|
256 case self::ASSERT_CONTENT_COUNT_MAX: |
|
257 $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times'; |
|
258 $failure = sprintf($failure, $other, $this->_content); |
|
259 break; |
|
260 case self::ASSERT_QUERY: |
|
261 default: |
|
262 $failure = 'Failed asserting node DENOTED BY %s EXISTS'; |
|
263 if ($this->_negate) { |
|
264 $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST'; |
|
265 } |
|
266 $failure = sprintf($failure, $other); |
|
267 break; |
|
268 } |
|
269 |
|
270 if (!empty($description)) { |
|
271 $failure = $description . "\n" . $failure; |
|
272 } |
|
273 |
|
274 throw new Zend_Test_PHPUnit_Constraint_Exception($failure); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Complete implementation |
|
279 * |
|
280 * @return string |
|
281 */ |
|
282 public function toString() |
|
283 { |
|
284 return ''; |
|
285 } |
|
286 |
|
287 /** |
|
288 * Register XPath namespaces |
|
289 * |
|
290 * @param array $xpathNamespaces |
|
291 * @return void |
|
292 */ |
|
293 public function registerXpathNamespaces($xpathNamespaces) |
|
294 { |
|
295 $this->_xpathNamespaces = $xpathNamespaces; |
|
296 } |
|
297 |
|
298 /** |
|
299 * Check to see if content is matched in selected nodes |
|
300 * |
|
301 * @param Zend_Dom_Query_Result $result |
|
302 * @param string $match Content to match |
|
303 * @return bool |
|
304 */ |
|
305 protected function _matchContent($result, $match) |
|
306 { |
|
307 $match = (string) $match; |
|
308 |
|
309 if (0 == count($result)) { |
|
310 return false; |
|
311 } |
|
312 |
|
313 foreach ($result as $node) { |
|
314 $content = $this->_getNodeContent($node); |
|
315 if (strstr($content, $match)) { |
|
316 return true; |
|
317 } |
|
318 } |
|
319 |
|
320 return false; |
|
321 } |
|
322 |
|
323 /** |
|
324 * Check to see if content is NOT matched in selected nodes |
|
325 * |
|
326 * @param Zend_Dom_Query_Result $result |
|
327 * @param string $match |
|
328 * @return bool |
|
329 */ |
|
330 protected function _notMatchContent($result, $match) |
|
331 { |
|
332 if (0 == count($result)) { |
|
333 return true; |
|
334 } |
|
335 |
|
336 foreach ($result as $node) { |
|
337 $content = $this->_getNodeContent($node); |
|
338 if (strstr($content, $match)) { |
|
339 return false; |
|
340 } |
|
341 } |
|
342 |
|
343 return true; |
|
344 } |
|
345 |
|
346 /** |
|
347 * Check to see if content is matched by regex in selected nodes |
|
348 * |
|
349 * @param Zend_Dom_Query_Result $result |
|
350 * @param string $pattern |
|
351 * @return bool |
|
352 */ |
|
353 protected function _regexContent($result, $pattern) |
|
354 { |
|
355 if (0 == count($result)) { |
|
356 return false; |
|
357 } |
|
358 |
|
359 foreach ($result as $node) { |
|
360 $content = $this->_getNodeContent($node); |
|
361 if (preg_match($pattern, $content)) { |
|
362 return true; |
|
363 } |
|
364 } |
|
365 |
|
366 return false; |
|
367 } |
|
368 |
|
369 /** |
|
370 * Check to see if content is NOT matched by regex in selected nodes |
|
371 * |
|
372 * @param Zend_Dom_Query_Result $result |
|
373 * @param string $pattern |
|
374 * @return bool |
|
375 */ |
|
376 protected function _notRegexContent($result, $pattern) |
|
377 { |
|
378 if (0 == count($result)) { |
|
379 return true; |
|
380 } |
|
381 |
|
382 foreach ($result as $node) { |
|
383 $content = $this->_getNodeContent($node); |
|
384 if (preg_match($pattern, $content)) { |
|
385 return false; |
|
386 } |
|
387 } |
|
388 |
|
389 return true; |
|
390 } |
|
391 |
|
392 /** |
|
393 * Determine if content count matches criteria |
|
394 * |
|
395 * @param Zend_Dom_Query_Result $result |
|
396 * @param int $test Value against which to test |
|
397 * @param string $type assertion type |
|
398 * @return boolean |
|
399 */ |
|
400 protected function _countContent($result, $test, $type) |
|
401 { |
|
402 $count = count($result); |
|
403 |
|
404 switch ($type) { |
|
405 case self::ASSERT_CONTENT_COUNT: |
|
406 return ($this->_negate) |
|
407 ? ($test != $count) |
|
408 : ($test == $count); |
|
409 case self::ASSERT_CONTENT_COUNT_MIN: |
|
410 return ($count >= $test); |
|
411 case self::ASSERT_CONTENT_COUNT_MAX: |
|
412 return ($count <= $test); |
|
413 default: |
|
414 return false; |
|
415 } |
|
416 } |
|
417 |
|
418 /** |
|
419 * Get node content, minus node markup tags |
|
420 * |
|
421 * @param DOMNode $node |
|
422 * @return string |
|
423 */ |
|
424 protected function _getNodeContent(DOMNode $node) |
|
425 { |
|
426 if ($node instanceof DOMAttr) { |
|
427 return $node->value; |
|
428 } else { |
|
429 $doc = $node->ownerDocument; |
|
430 $content = $doc->saveXML($node); |
|
431 $tag = $node->nodeName; |
|
432 $regex = '|</?' . $tag . '[^>]*>|'; |
|
433 return preg_replace($regex, '', $content); |
|
434 } |
|
435 } |
|
436 } |