Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Test / PHPUnit / Controller / AbstractHttpControllerTestCase.php
blobab4335f1017f7ba6038959f185e67f5efbefdffb
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9 namespace Zend\Test\PHPUnit\Controller;
11 use PHPUnit_Framework_ExpectationFailedException;
12 use Zend\Dom;
14 abstract class AbstractHttpControllerTestCase extends AbstractControllerTestCase
16 /**
17 * HTTP controller must not use the console request
18 * @var bool
20 protected $useConsoleRequest = false;
22 /**
23 * XPath namespaces
24 * @var array
26 protected $xpathNamespaces = array();
28 /**
29 * Get response header by key
31 * @param string $header
32 * @return \Zend\Http\Header\HeaderInterface|false
34 protected function getResponseHeader($header)
36 $response = $this->getResponse();
37 $headers = $response->getHeaders();
38 $responseHeader = $headers->get($header, false);
39 return $responseHeader;
42 /**
43 * Assert response header exists
45 * @param string $header
47 public function assertHasResponseHeader($header)
49 $responseHeader = $this->getResponseHeader($header);
50 if (false === $responseHeader) {
51 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
52 'Failed asserting response header "%s" found',
53 $header
54 ));
56 $this->assertNotEquals(false, $responseHeader);
59 /**
60 * Assert response header does not exist
62 * @param string $header
64 public function assertNotHasResponseHeader($header)
66 $responseHeader = $this->getResponseHeader($header);
67 if (false !== $responseHeader) {
68 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
69 'Failed asserting response header "%s" WAS NOT found',
70 $header
71 ));
73 $this->assertFalse($responseHeader);
76 /**
77 * Assert response header exists and contains the given string
79 * @param string $header
80 * @param string $match
82 public function assertResponseHeaderContains($header, $match)
84 $responseHeader = $this->getResponseHeader($header);
85 if (!$responseHeader) {
86 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
87 'Failed asserting response header, header "%s" do not exists',
88 $header
89 ));
91 if ($match != $responseHeader->getFieldValue()) {
92 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
93 'Failed asserting response header "%s" exists and contains "%s", actual content is "%s"',
94 $header,
95 $match,
96 $responseHeader->getFieldValue()
97 ));
99 $this->assertEquals($match, $responseHeader->getFieldValue());
103 * Assert response header exists and contains the given string
105 * @param string $header
106 * @param string $match
108 public function assertNotResponseHeaderContains($header, $match)
110 $responseHeader = $this->getResponseHeader($header);
111 if (!$responseHeader) {
112 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
113 'Failed asserting response header, header "%s" do not exists',
114 $header
117 if ($match == $responseHeader->getFieldValue()) {
118 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
119 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"',
120 $header,
121 $match
124 $this->assertNotEquals($match, $responseHeader->getFieldValue());
128 * Assert response header exists and matches the given pattern
130 * @param string $header
131 * @param string $pattern
133 public function assertResponseHeaderRegex($header, $pattern)
135 $responseHeader = $this->getResponseHeader($header);
136 if (!$responseHeader) {
137 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
138 'Failed asserting response header, header "%s" do not exists',
139 $header
142 if (!preg_match($pattern, $responseHeader->getFieldValue())) {
143 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
144 'Failed asserting response header "%s" exists and matches regex "%s", actual content is "%s"',
145 $header,
146 $pattern,
147 $responseHeader->getFieldValue()
150 $this->assertTrue((bool) preg_match($pattern, $responseHeader->getFieldValue()));
154 * Assert response header does not exist and/or does not match the given regex
156 * @param string $header
157 * @param string $pattern
159 public function assertNotResponseHeaderRegex($header, $pattern)
161 $responseHeader = $this->getResponseHeader($header);
162 if (!$responseHeader) {
163 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
164 'Failed asserting response header, header "%s" do not exists',
165 $header
168 if (preg_match($pattern, $responseHeader->getFieldValue())) {
169 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
170 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"',
171 $header,
172 $pattern
175 $this->assertFalse((bool) preg_match($pattern, $responseHeader->getFieldValue()));
179 * Assert that response is a redirect
181 public function assertRedirect()
183 $responseHeader = $this->getResponseHeader('Location');
184 if (false === $responseHeader) {
185 throw new PHPUnit_Framework_ExpectationFailedException(
186 'Failed asserting response is a redirect'
189 $this->assertNotEquals(false, $responseHeader);
193 * Assert that response is NOT a redirect
195 public function assertNotRedirect()
197 $responseHeader = $this->getResponseHeader('Location');
198 if (false !== $responseHeader) {
199 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
200 'Failed asserting response is NOT a redirect, actual redirection is "%s"',
201 $responseHeader->getFieldValue()
204 $this->assertFalse($responseHeader);
208 * Assert that response redirects to given URL
210 * @param string $url
212 public function assertRedirectTo($url)
214 $responseHeader = $this->getResponseHeader('Location');
215 if (!$responseHeader) {
216 throw new PHPUnit_Framework_ExpectationFailedException(
217 'Failed asserting response is a redirect'
220 if ($url != $responseHeader->getFieldValue()) {
221 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
222 'Failed asserting response redirects to "%s", actual redirection is "%s"',
223 $url,
224 $responseHeader->getFieldValue()
227 $this->assertEquals($url, $responseHeader->getFieldValue());
231 * Assert that response does not redirect to given URL
233 * @param string $url
235 public function assertNotRedirectTo($url)
237 $responseHeader = $this->getResponseHeader('Location');
238 if (!$responseHeader) {
239 throw new PHPUnit_Framework_ExpectationFailedException(
240 'Failed asserting response is a redirect'
243 if ($url == $responseHeader->getFieldValue()) {
244 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
245 'Failed asserting response redirects to "%s"',
246 $url
249 $this->assertNotEquals($url, $responseHeader->getFieldValue());
253 * Assert that redirect location matches pattern
255 * @param string $pattern
257 public function assertRedirectRegex($pattern)
259 $responseHeader = $this->getResponseHeader('Location');
260 if (!$responseHeader) {
261 throw new PHPUnit_Framework_ExpectationFailedException(
262 'Failed asserting response is a redirect'
265 if (!preg_match($pattern, $responseHeader->getFieldValue())) {
266 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
267 'Failed asserting response redirects to URL MATCHING "%s", actual redirection is "%s"',
268 $pattern,
269 $responseHeader->getFieldValue()
272 $this->assertTrue((bool) preg_match($pattern, $responseHeader->getFieldValue()));
276 * Assert that redirect location does not match pattern
278 * @param string $pattern
280 public function assertNotRedirectRegex($pattern)
282 $responseHeader = $this->getResponseHeader('Location');
283 if (!$responseHeader) {
284 throw new PHPUnit_Framework_ExpectationFailedException(
285 'Failed asserting response is a redirect'
288 if (preg_match($pattern, $responseHeader->getFieldValue())) {
289 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
290 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"',
291 $pattern
294 $this->assertFalse((bool) preg_match($pattern, $responseHeader->getFieldValue()));
298 * Register XPath namespaces
300 * @param array $xpathNamespaces
302 public function registerXpathNamespaces(array $xpathNamespaces)
304 $this->xpathNamespaces = $xpathNamespaces;
308 * Execute a DOM/XPath query
310 * @param string $path
311 * @param bool $useXpath
312 * @return array
314 private function query($path, $useXpath = false)
316 $response = $this->getResponse();
317 $dom = new Dom\Query($response->getContent());
318 if ($useXpath) {
319 $dom->registerXpathNamespaces($this->xpathNamespaces);
320 return $dom->queryXpath($path);
322 return $dom->execute($path);
326 * Execute a xpath query
328 * @param string $path
329 * @return array
331 private function xpathQuery($path)
333 return $this->query($path, true);
337 * Count the dom query executed
339 * @param string $path
340 * @return int
342 private function queryCount($path)
344 return count($this->query($path, false));
348 * Count the dom query executed
350 * @param string $path
351 * @return int
353 private function xpathQueryCount($path)
355 return count($this->xpathQuery($path));
359 * Assert against DOM/XPath selection
361 * @param string $path
362 * @param bool $useXpath
364 private function queryAssertion($path, $useXpath = false)
366 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
367 $match = $this->$method($path);
368 if (!$match > 0) {
369 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
370 'Failed asserting node DENOTED BY %s EXISTS',
371 $path
374 $this->assertTrue($match > 0);
378 * Assert against DOM selection
380 * @param string $path CSS selector path
382 public function assertQuery($path)
384 $this->queryAssertion($path, false);
388 * Assert against XPath selection
390 * @param string $path XPath path
392 public function assertXpathQuery($path)
394 $this->queryAssertion($path, true);
398 * Assert against DOM/XPath selection
400 * @param string $path CSS selector path
401 * @param bool $useXpath
403 private function notQueryAssertion($path, $useXpath = false)
405 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
406 $match = $this->$method($path);
407 if ($match != 0) {
408 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
409 'Failed asserting node DENOTED BY %s DOES NOT EXIST',
410 $path
413 $this->assertEquals(0, $match);
417 * Assert against DOM selection
419 * @param string $path CSS selector path
421 public function assertNotQuery($path)
423 $this->notQueryAssertion($path, false);
427 * Assert against XPath selection
429 * @param string $path XPath path
431 public function assertNotXpathQuery($path)
433 $this->notQueryAssertion($path, true);
437 * Assert against DOM/XPath selection; should contain exact number of nodes
439 * @param string $path CSS selector path
440 * @param string $count Number of nodes that should match
441 * @param bool $useXpath
443 private function queryCountAssertion($path, $count, $useXpath = false)
445 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
446 $match = $this->$method($path);
447 if ($match != $count) {
448 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
449 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times, actually occurs %d times',
450 $path,
451 $count,
452 $match
455 $this->assertEquals($match, $count);
459 * Assert against DOM selection; should contain exact number of nodes
461 * @param string $path CSS selector path
462 * @param string $count Number of nodes that should match
464 public function assertQueryCount($path, $count)
466 $this->queryCountAssertion($path, $count, false);
470 * Assert against XPath selection; should contain exact number of nodes
472 * @param string $path XPath path
473 * @param string $count Number of nodes that should match
475 public function assertXpathQueryCount($path, $count)
477 $this->queryCountAssertion($path, $count, true);
481 * Assert against DOM/XPath selection; should NOT contain exact number of nodes
483 * @param string $path CSS selector path
484 * @param string $count Number of nodes that should NOT match
485 * @param bool $useXpath
487 private function notQueryCountAssertion($path, $count, $useXpath = false)
489 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
490 $match = $this->$method($path);
491 if ($match == $count) {
492 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
493 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times',
494 $path,
495 $count
498 $this->assertNotEquals($match, $count);
502 * Assert against DOM selection; should NOT contain exact number of nodes
504 * @param string $path CSS selector path
505 * @param string $count Number of nodes that should NOT match
507 public function assertNotQueryCount($path, $count)
509 $this->notQueryCountAssertion($path, $count, false);
513 * Assert against XPath selection; should NOT contain exact number of nodes
515 * @param string $path XPath path
516 * @param string $count Number of nodes that should NOT match
518 public function assertNotXpathQueryCount($path, $count)
520 $this->notQueryCountAssertion($path, $count, true);
524 * Assert against DOM/XPath selection; should contain at least this number of nodes
526 * @param string $path CSS selector path
527 * @param string $count Minimum number of nodes that should match
528 * @param bool $useXpath
530 private function queryCountMinAssertion($path, $count, $useXpath = false)
532 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
533 $match = $this->$method($path);
534 if ($match < $count) {
535 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
536 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times, actually occurs %d times',
537 $path,
538 $count,
539 $match
542 $this->assertTrue($match >= $count);
546 * Assert against DOM selection; should contain at least this number of nodes
548 * @param string $path CSS selector path
549 * @param string $count Minimum number of nodes that should match
551 public function assertQueryCountMin($path, $count)
553 $this->queryCountMinAssertion($path, $count, false);
557 * Assert against XPath selection; should contain at least this number of nodes
559 * @param string $path XPath path
560 * @param string $count Minimum number of nodes that should match
562 public function assertXpathQueryCountMin($path, $count)
564 $this->queryCountMinAssertion($path, $count, true);
568 * Assert against DOM/XPath selection; should contain no more than this number of nodes
570 * @param string $path CSS selector path
571 * @param string $count Maximum number of nodes that should match
572 * @param bool $useXpath
574 private function queryCountMaxAssertion($path, $count, $useXpath = false)
576 $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
577 $match = $this->$method($path);
578 if ($match > $count) {
579 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
580 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times, actually occurs %d times',
581 $path,
582 $count,
583 $match
586 $this->assertTrue($match <= $count);
590 * Assert against DOM selection; should contain no more than this number of nodes
592 * @param string $path CSS selector path
593 * @param string $count Maximum number of nodes that should match
595 public function assertQueryCountMax($path, $count)
597 $this->queryCountMaxAssertion($path, $count, false);
601 * Assert against XPath selection; should contain no more than this number of nodes
603 * @param string $path XPath path
604 * @param string $count Maximum number of nodes that should match
606 public function assertXpathQueryCountMax($path, $count)
608 $this->queryCountMaxAssertion($path, $count, true);
612 * Assert against DOM/XPath selection; node should contain content
614 * @param string $path CSS selector path
615 * @param string $match content that should be contained in matched nodes
616 * @param bool $useXpath
618 private function queryContentContainsAssertion($path, $match, $useXpath = false)
620 $result = $this->query($path, $useXpath);
621 if ($result->count() == 0) {
622 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
623 'Failed asserting node DENOTED BY %s EXISTS',
624 $path
627 foreach ($result as $node) {
628 if ($node->nodeValue == $match) {
629 $this->assertEquals($match, $node->nodeValue);
630 return;
633 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
634 'Failed asserting node denoted by %s CONTAINS content "%s"',
635 $path,
636 $match
641 * Assert against DOM selection; node should contain content
643 * @param string $path CSS selector path
644 * @param string $match content that should be contained in matched nodes
646 public function assertQueryContentContains($path, $match)
648 $this->queryContentContainsAssertion($path, $match, false);
652 * Assert against XPath selection; node should contain content
654 * @param string $path XPath path
655 * @param string $match content that should be contained in matched nodes
657 public function assertXpathQueryContentContains($path, $match)
659 $this->queryContentContainsAssertion($path, $match, true);
663 * Assert against DOM/XPath selection; node should NOT contain content
665 * @param string $path CSS selector path
666 * @param string $match content that should NOT be contained in matched nodes
667 * @param bool $useXpath
669 private function notQueryContentContainsAssertion($path, $match, $useXpath = false)
671 $result = $this->query($path, $useXpath);
672 if ($result->count() == 0) {
673 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
674 'Failed asserting node DENOTED BY %s EXISTS',
675 $path
678 foreach ($result as $node) {
679 if ($node->nodeValue == $match) {
680 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
681 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"',
682 $path,
683 $match
687 $currentValue = $node->nodeValue;
688 $this->assertNotEquals($currentValue, $match);
692 * Assert against DOM selection; node should NOT contain content
694 * @param string $path CSS selector path
695 * @param string $match content that should NOT be contained in matched nodes
697 public function assertNotQueryContentContains($path, $match)
699 $this->notQueryContentContainsAssertion($path, $match, false);
703 * Assert against XPath selection; node should NOT contain content
705 * @param string $path XPath path
706 * @param string $match content that should NOT be contained in matched nodes
708 public function assertNotXpathQueryContentContains($path, $match)
710 $this->notQueryContentContainsAssertion($path, $match, true);
714 * Assert against DOM/XPath selection; node should match content
716 * @param string $path CSS selector path
717 * @param string $pattern Pattern that should be contained in matched nodes
718 * @param bool $useXpath
720 private function queryContentRegexAssertion($path, $pattern, $useXpath = false)
722 $result = $this->query($path, $useXpath);
723 if ($result->count() == 0) {
724 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
725 'Failed asserting node DENOTED BY %s EXISTS',
726 $path
729 if (!preg_match($pattern, $result->current()->nodeValue)) {
730 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
731 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s", actual content is "%s"',
732 $path,
733 $pattern,
734 $result->current()->nodeValue
737 $this->assertTrue((bool) preg_match($pattern, $result->current()->nodeValue));
741 * Assert against DOM selection; node should match content
743 * @param string $path CSS selector path
744 * @param string $pattern Pattern that should be contained in matched nodes
746 public function assertQueryContentRegex($path, $pattern)
748 $this->queryContentRegexAssertion($path, $pattern, false);
752 * Assert against XPath selection; node should match content
754 * @param string $path XPath path
755 * @param string $pattern Pattern that should be contained in matched nodes
757 public function assertXpathQueryContentRegex($path, $pattern)
759 $this->queryContentRegexAssertion($path, $pattern, true);
763 * Assert against DOM/XPath selection; node should NOT match content
765 * @param string $path CSS selector path
766 * @param string $pattern pattern that should NOT be contained in matched nodes
767 * @param bool $useXpath
769 private function notQueryContentRegexAssertion($path, $pattern, $useXpath = false)
771 $result = $this->query($path, $useXpath);
772 if ($result->count() == 0) {
773 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
774 'Failed asserting node DENOTED BY %s EXISTS',
775 $path
778 if (preg_match($pattern, $result->current()->nodeValue)) {
779 throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
780 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"',
781 $path,
782 $pattern
785 $this->assertFalse((bool) preg_match($pattern, $result->current()->nodeValue));
789 * Assert against DOM selection; node should NOT match content
791 * @param string $path CSS selector path
792 * @param string $pattern pattern that should NOT be contained in matched nodes
794 public function assertNotQueryContentRegex($path, $pattern)
796 $this->notQueryContentRegexAssertion($path, $pattern, false);
800 * Assert against XPath selection; node should NOT match content
802 * @param string $path XPath path
803 * @param string $pattern pattern that should NOT be contained in matched nodes
805 public function assertNotXpathQueryContentRegex($path, $pattern)
807 $this->notQueryContentRegexAssertion($path, $pattern, true);