|
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_DomQuery34 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 $other String to examine |
|
136 * @param null|string Assertion type |
|
137 * @return bool |
|
138 */ |
|
139 public function evaluate($other, $assertType = null) |
|
140 { |
|
141 if (strstr($assertType, 'Not')) { |
|
142 $this->setNegate(true); |
|
143 $assertType = str_replace('Not', '', $assertType); |
|
144 } |
|
145 |
|
146 if (strstr($assertType, 'Xpath')) { |
|
147 $this->setUseXpath(true); |
|
148 $assertType = str_replace('Xpath', 'Query', $assertType); |
|
149 } |
|
150 |
|
151 if (!in_array($assertType, $this->_assertTypes)) { |
|
152 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
153 throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__)); |
|
154 } |
|
155 |
|
156 $this->_assertType = $assertType; |
|
157 |
|
158 $method = $this->_useXpath ? 'queryXpath' : 'query'; |
|
159 $domQuery = new Zend_Dom_Query($other); |
|
160 $domQuery->registerXpathNamespaces($this->_xpathNamespaces); |
|
161 $result = $domQuery->$method($this->_path); |
|
162 $argv = func_get_args(); |
|
163 $argc = func_num_args(); |
|
164 |
|
165 switch ($assertType) { |
|
166 case self::ASSERT_CONTENT_CONTAINS: |
|
167 if (3 > $argc) { |
|
168 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
169 throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match'); |
|
170 } |
|
171 $this->_content = $content = $argv[2]; |
|
172 return ($this->_negate) |
|
173 ? $this->_notMatchContent($result, $content) |
|
174 : $this->_matchContent($result, $content); |
|
175 case self::ASSERT_CONTENT_REGEX: |
|
176 if (3 > $argc) { |
|
177 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
178 throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match'); |
|
179 } |
|
180 $this->_content = $content = $argv[2]; |
|
181 return ($this->_negate) |
|
182 ? $this->_notRegexContent($result, $content) |
|
183 : $this->_regexContent($result, $content); |
|
184 case self::ASSERT_CONTENT_COUNT: |
|
185 case self::ASSERT_CONTENT_COUNT_MIN: |
|
186 case self::ASSERT_CONTENT_COUNT_MAX: |
|
187 if (3 > $argc) { |
|
188 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
189 throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare'); |
|
190 } |
|
191 $this->_content = $content = $argv[2]; |
|
192 return $this->_countContent($result, $content, $assertType); |
|
193 case self::ASSERT_QUERY: |
|
194 default: |
|
195 if ($this->_negate) { |
|
196 return (0 == count($result)); |
|
197 } else { |
|
198 return (0 != count($result)); |
|
199 } |
|
200 } |
|
201 } |
|
202 |
|
203 /** |
|
204 * Report Failure |
|
205 * |
|
206 * @see PHPUnit_Framework_Constraint for implementation details |
|
207 * @param mixed $other CSS selector path |
|
208 * @param string $description |
|
209 * @param bool $not |
|
210 * @return void |
|
211 * @throws PHPUnit_Framework_ExpectationFailedException |
|
212 */ |
|
213 public function fail($other, $description, $not = false) |
|
214 { |
|
215 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
216 switch ($this->_assertType) { |
|
217 case self::ASSERT_CONTENT_CONTAINS: |
|
218 $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"'; |
|
219 if ($this->_negate) { |
|
220 $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"'; |
|
221 } |
|
222 $failure = sprintf($failure, $other, $this->_content); |
|
223 break; |
|
224 case self::ASSERT_CONTENT_REGEX: |
|
225 $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"'; |
|
226 if ($this->_negate) { |
|
227 $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"'; |
|
228 } |
|
229 $failure = sprintf($failure, $other, $this->_content); |
|
230 break; |
|
231 case self::ASSERT_CONTENT_COUNT: |
|
232 $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times'; |
|
233 if ($this->_negate) { |
|
234 $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times'; |
|
235 } |
|
236 $failure = sprintf($failure, $other, $this->_content); |
|
237 break; |
|
238 case self::ASSERT_CONTENT_COUNT_MIN: |
|
239 $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times'; |
|
240 $failure = sprintf($failure, $other, $this->_content); |
|
241 break; |
|
242 case self::ASSERT_CONTENT_COUNT_MAX: |
|
243 $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times'; |
|
244 $failure = sprintf($failure, $other, $this->_content); |
|
245 break; |
|
246 case self::ASSERT_QUERY: |
|
247 default: |
|
248 $failure = 'Failed asserting node DENOTED BY %s EXISTS'; |
|
249 if ($this->_negate) { |
|
250 $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST'; |
|
251 } |
|
252 $failure = sprintf($failure, $other); |
|
253 break; |
|
254 } |
|
255 |
|
256 if (!empty($description)) { |
|
257 $failure = $description . "\n" . $failure; |
|
258 } |
|
259 |
|
260 throw new Zend_Test_PHPUnit_Constraint_Exception($failure); |
|
261 } |
|
262 |
|
263 /** |
|
264 * Complete implementation |
|
265 * |
|
266 * @return string |
|
267 */ |
|
268 public function toString() |
|
269 { |
|
270 return ''; |
|
271 } |
|
272 |
|
273 /** |
|
274 * Register XPath namespaces |
|
275 * |
|
276 * @param array $xpathNamespaces |
|
277 * @return void |
|
278 */ |
|
279 public function registerXpathNamespaces($xpathNamespaces) |
|
280 { |
|
281 $this->_xpathNamespaces = $xpathNamespaces; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Check to see if content is matched in selected nodes |
|
286 * |
|
287 * @param Zend_Dom_Query_Result $result |
|
288 * @param string $match Content to match |
|
289 * @return bool |
|
290 */ |
|
291 protected function _matchContent($result, $match) |
|
292 { |
|
293 $match = (string) $match; |
|
294 |
|
295 if (0 == count($result)) { |
|
296 return false; |
|
297 } |
|
298 |
|
299 foreach ($result as $node) { |
|
300 $content = $this->_getNodeContent($node); |
|
301 if (strstr($content, $match)) { |
|
302 return true; |
|
303 } |
|
304 } |
|
305 |
|
306 return false; |
|
307 } |
|
308 |
|
309 /** |
|
310 * Check to see if content is NOT matched in selected nodes |
|
311 * |
|
312 * @param Zend_Dom_Query_Result $result |
|
313 * @param string $match |
|
314 * @return bool |
|
315 */ |
|
316 protected function _notMatchContent($result, $match) |
|
317 { |
|
318 if (0 == count($result)) { |
|
319 return true; |
|
320 } |
|
321 |
|
322 foreach ($result as $node) { |
|
323 $content = $this->_getNodeContent($node); |
|
324 if (strstr($content, $match)) { |
|
325 return false; |
|
326 } |
|
327 } |
|
328 |
|
329 return true; |
|
330 } |
|
331 |
|
332 /** |
|
333 * Check to see if content is matched by regex in selected nodes |
|
334 * |
|
335 * @param Zend_Dom_Query_Result $result |
|
336 * @param string $pattern |
|
337 * @return bool |
|
338 */ |
|
339 protected function _regexContent($result, $pattern) |
|
340 { |
|
341 if (0 == count($result)) { |
|
342 return false; |
|
343 } |
|
344 |
|
345 foreach ($result as $node) { |
|
346 $content = $this->_getNodeContent($node); |
|
347 if (preg_match($pattern, $content)) { |
|
348 return true; |
|
349 } |
|
350 } |
|
351 |
|
352 return false; |
|
353 } |
|
354 |
|
355 /** |
|
356 * Check to see if content is NOT matched by regex in selected nodes |
|
357 * |
|
358 * @param Zend_Dom_Query_Result $result |
|
359 * @param string $pattern |
|
360 * @return bool |
|
361 */ |
|
362 protected function _notRegexContent($result, $pattern) |
|
363 { |
|
364 if (0 == count($result)) { |
|
365 return true; |
|
366 } |
|
367 |
|
368 foreach ($result as $node) { |
|
369 $content = $this->_getNodeContent($node); |
|
370 if (preg_match($pattern, $content)) { |
|
371 return false; |
|
372 } |
|
373 } |
|
374 |
|
375 return true; |
|
376 } |
|
377 |
|
378 /** |
|
379 * Determine if content count matches criteria |
|
380 * |
|
381 * @param Zend_Dom_Query_Result $result |
|
382 * @param int $test Value against which to test |
|
383 * @param string $type assertion type |
|
384 * @return boolean |
|
385 */ |
|
386 protected function _countContent($result, $test, $type) |
|
387 { |
|
388 $count = count($result); |
|
389 |
|
390 switch ($type) { |
|
391 case self::ASSERT_CONTENT_COUNT: |
|
392 return ($this->_negate) |
|
393 ? ($test != $count) |
|
394 : ($test == $count); |
|
395 case self::ASSERT_CONTENT_COUNT_MIN: |
|
396 return ($count >= $test); |
|
397 case self::ASSERT_CONTENT_COUNT_MAX: |
|
398 return ($count <= $test); |
|
399 default: |
|
400 return false; |
|
401 } |
|
402 } |
|
403 |
|
404 /** |
|
405 * Get node content, minus node markup tags |
|
406 * |
|
407 * @param DOMNode $node |
|
408 * @return string |
|
409 */ |
|
410 protected function _getNodeContent(DOMNode $node) |
|
411 { |
|
412 if ($node instanceof DOMAttr) { |
|
413 return $node->value; |
|
414 } else { |
|
415 $doc = $node->ownerDocument; |
|
416 $content = $doc->saveXML($node); |
|
417 $tag = $node->nodeName; |
|
418 $regex = '|</?' . $tag . '[^>]*>|'; |
|
419 return preg_replace($regex, '', $content); |
|
420 } |
|
421 } |
|
422 } |