File indexing completed on 2024-12-22 05:37:08
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Test 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** @see Zend_Controller_Front */ 0023 // require_once 'Zend/Controller/Front.php'; 0024 0025 /** @see Zend_Controller_Action_HelperBroker */ 0026 // require_once 'Zend/Controller/Action/HelperBroker.php'; 0027 0028 /** @see Zend_Layout */ 0029 // require_once 'Zend/Layout.php'; 0030 0031 /** @see Zend_Session */ 0032 // require_once 'Zend/Session.php'; 0033 0034 /** @see Zend_Registry */ 0035 // require_once 'Zend/Registry.php'; 0036 0037 /** 0038 * Functional testing scaffold for MVC applications 0039 * 0040 * @uses PHPUnit_Framework_TestCase 0041 * @category Zend 0042 * @package Zend_Test 0043 * @subpackage PHPUnit 0044 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0045 * @license http://framework.zend.com/license/new-bsd New BSD License 0046 */ 0047 abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase 0048 { 0049 /** 0050 * @var mixed Bootstrap file path or callback 0051 */ 0052 public $bootstrap; 0053 0054 /** 0055 * @var Zend_Controller_Front 0056 */ 0057 protected $_frontController; 0058 0059 /** 0060 * @var Zend_Dom_Query 0061 */ 0062 protected $_query; 0063 0064 /** 0065 * @var Zend_Controller_Request_Abstract 0066 */ 0067 protected $_request; 0068 0069 /** 0070 * @var Zend_Controller_Response_Abstract 0071 */ 0072 protected $_response; 0073 0074 /** 0075 * XPath namespaces 0076 * @var array 0077 */ 0078 protected $_xpathNamespaces = array(); 0079 0080 /** 0081 * Overloading: prevent overloading to special properties 0082 * 0083 * @param string $name 0084 * @param mixed $value 0085 * @throws Zend_Exception 0086 */ 0087 public function __set($name, $value) 0088 { 0089 if (in_array($name, array('request', 'response', 'frontController'))) { 0090 // require_once 'Zend/Exception.php'; 0091 throw new Zend_Exception(sprintf('Setting %s object manually is not allowed', $name)); 0092 } 0093 $this->$name = $value; 0094 } 0095 0096 /** 0097 * Overloading for common properties 0098 * 0099 * Provides overloading for request, response, and frontController objects. 0100 * 0101 * @param mixed $name 0102 * @return null|Zend_Controller_Front|Zend_Controller_Request_HttpTestCase|Zend_Controller_Response_HttpTestCase 0103 */ 0104 public function __get($name) 0105 { 0106 switch ($name) { 0107 case 'request': 0108 return $this->getRequest(); 0109 case 'response': 0110 return $this->getResponse(); 0111 case 'frontController': 0112 return $this->getFrontController(); 0113 } 0114 0115 return null; 0116 } 0117 0118 /** 0119 * Set up MVC app 0120 * 0121 * Calls {@link bootstrap()} by default 0122 */ 0123 protected function setUp() 0124 { 0125 $this->bootstrap(); 0126 } 0127 0128 /** 0129 * Bootstrap the front controller 0130 * 0131 * Resets the front controller, and then bootstraps it. 0132 * 0133 * If {@link $bootstrap} is a callback, executes it; if it is a file, it include's 0134 * it. When done, sets the test case request and response objects into the 0135 * front controller. 0136 */ 0137 final public function bootstrap() 0138 { 0139 $this->reset(); 0140 if (null !== $this->bootstrap) { 0141 if ($this->bootstrap instanceof Zend_Application) { 0142 $this->bootstrap->bootstrap(); 0143 $this->_frontController = $this->bootstrap->getBootstrap()->getResource('frontcontroller'); 0144 } elseif (is_callable($this->bootstrap)) { 0145 call_user_func($this->bootstrap); 0146 } elseif (is_string($this->bootstrap)) { 0147 // require_once 'Zend/Loader.php'; 0148 if (Zend_Loader::isReadable($this->bootstrap)) { 0149 include $this- >bootstrap; 0150 } 0151 } 0152 } 0153 $this->frontController 0154 ->setRequest($this->getRequest()) 0155 ->setResponse($this->getResponse()); 0156 } 0157 0158 /** 0159 * Dispatch the MVC 0160 * 0161 * If a URL is provided, sets it as the request URI in the request object. 0162 * Then sets test case request and response objects in front controller, 0163 * disables throwing exceptions, and disables returning the response. 0164 * Finally, dispatches the front controller. 0165 * 0166 * @param string|null $url 0167 */ 0168 public function dispatch($url = null) 0169 { 0170 // redirector should not exit 0171 $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); 0172 $redirector->setExit(false); 0173 0174 // json helper should not exit 0175 $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json'); 0176 $json->suppressExit = true; 0177 0178 $request = $this->getRequest(); 0179 if (null !== $url) { 0180 $request->setRequestUri($url); 0181 } 0182 $request->setPathInfo(null); 0183 0184 $controller = $this->getFrontController(); 0185 $this->frontController 0186 ->setRequest($request) 0187 ->setResponse($this->getResponse()) 0188 ->throwExceptions(false) 0189 ->returnResponse(false); 0190 0191 if ($this->bootstrap instanceof Zend_Application) { 0192 $this->bootstrap->run(); 0193 } else { 0194 $this->frontController->dispatch(); 0195 } 0196 } 0197 0198 /** 0199 * Reset MVC state 0200 * 0201 * Creates new request/response objects, resets the front controller 0202 * instance, and resets the action helper broker. 0203 * 0204 * @todo Need to update Zend_Layout to add a resetInstance() method 0205 */ 0206 public function reset() 0207 { 0208 $_SESSION = array(); 0209 $_GET = array(); 0210 $_POST = array(); 0211 $_COOKIE = array(); 0212 $this->resetRequest(); 0213 $this->resetResponse(); 0214 Zend_Layout::resetMvcInstance(); 0215 Zend_Controller_Action_HelperBroker::resetHelpers(); 0216 $this->frontController->resetInstance(); 0217 Zend_Session::$_unitTestEnabled = true; 0218 } 0219 0220 /** 0221 * Rest all view placeholders 0222 */ 0223 protected function _resetPlaceholders() 0224 { 0225 $registry = Zend_Registry::getInstance(); 0226 $remove = array(); 0227 foreach ($registry as $key => $value) { 0228 if (strstr($key, '_View_')) { 0229 $remove[] = $key; 0230 } 0231 } 0232 0233 foreach ($remove as $key) { 0234 unset($registry[$key]); 0235 } 0236 } 0237 0238 /** 0239 * Reset the request object 0240 * 0241 * Useful for test cases that need to test multiple trips to the server. 0242 * 0243 * @return Zend_Test_PHPUnit_ControllerTestCase 0244 */ 0245 public function resetRequest() 0246 { 0247 if ($this->_request instanceof Zend_Controller_Request_HttpTestCase) { 0248 $this->_request->clearQuery() 0249 ->clearPost(); 0250 } 0251 $this->_request = null; 0252 return $this; 0253 } 0254 0255 /** 0256 * Reset the response object 0257 * 0258 * Useful for test cases that need to test multiple trips to the server. 0259 * 0260 * @return Zend_Test_PHPUnit_ControllerTestCase 0261 */ 0262 public function resetResponse() 0263 { 0264 $this->_response = null; 0265 $this->_resetPlaceholders(); 0266 return $this; 0267 } 0268 0269 /** 0270 * Assert against DOM selection 0271 * 0272 * @param string $path CSS selector path 0273 * @param string $message 0274 */ 0275 public function assertQuery($path, $message = '') 0276 { 0277 $this->_incrementAssertionCount(); 0278 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0279 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0280 $content = $this->response->outputBody(); 0281 if (!$constraint->evaluate($content, __FUNCTION__)) { 0282 $constraint->fail($path, $message); 0283 } 0284 } 0285 0286 /** 0287 * Assert against DOM selection 0288 * 0289 * @param string $path CSS selector path 0290 * @param string $message 0291 */ 0292 public function assertNotQuery($path, $message = '') 0293 { 0294 $this->_incrementAssertionCount(); 0295 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0296 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0297 $content = $this->response->outputBody(); 0298 if (!$constraint->evaluate($content, __FUNCTION__)) { 0299 $constraint->fail($path, $message); 0300 } 0301 } 0302 0303 /** 0304 * Assert against DOM selection; node should contain content 0305 * 0306 * @param string $path CSS selector path 0307 * @param string $match content that should be contained in matched nodes 0308 * @param string $message 0309 */ 0310 public function assertQueryContentContains($path, $match, $message = '') 0311 { 0312 $this->_incrementAssertionCount(); 0313 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0314 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0315 $content = $this->response->outputBody(); 0316 if (!$constraint->evaluate($content, __FUNCTION__, $match)) { 0317 $constraint->fail($path, $message); 0318 } 0319 } 0320 0321 /** 0322 * Assert against DOM selection; node should NOT contain content 0323 * 0324 * @param string $path CSS selector path 0325 * @param string $match content that should NOT be contained in matched nodes 0326 * @param string $message 0327 */ 0328 public function assertNotQueryContentContains($path, $match, $message = '') 0329 { 0330 $this->_incrementAssertionCount(); 0331 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0332 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0333 $content = $this->response->outputBody(); 0334 if (!$constraint->evaluate($content, __FUNCTION__, $match)) { 0335 $constraint->fail($path, $message); 0336 } 0337 } 0338 0339 /** 0340 * Assert against DOM selection; node should match content 0341 * 0342 * @param string $path CSS selector path 0343 * @param string $pattern Pattern that should be contained in matched nodes 0344 * @param string $message 0345 */ 0346 public function assertQueryContentRegex($path, $pattern, $message = '') 0347 { 0348 $this->_incrementAssertionCount(); 0349 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0350 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0351 $content = $this->response->outputBody(); 0352 if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) { 0353 $constraint->fail($path, $message); 0354 } 0355 } 0356 0357 /** 0358 * Assert against DOM selection; node should NOT match content 0359 * 0360 * @param string $path CSS selector path 0361 * @param string $pattern pattern that should NOT be contained in matched nodes 0362 * @param string $message 0363 */ 0364 public function assertNotQueryContentRegex($path, $pattern, $message = '') 0365 { 0366 $this->_incrementAssertionCount(); 0367 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0368 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0369 $content = $this->response->outputBody(); 0370 if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) { 0371 $constraint->fail($path, $message); 0372 } 0373 } 0374 0375 /** 0376 * Assert against DOM selection; should contain exact number of nodes 0377 * 0378 * @param string $path CSS selector path 0379 * @param string $count Number of nodes that should match 0380 * @param string $message 0381 */ 0382 public function assertQueryCount($path, $count, $message = '') 0383 { 0384 $this->_incrementAssertionCount(); 0385 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0386 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0387 $content = $this->response->outputBody(); 0388 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0389 $constraint->fail($path, $message); 0390 } 0391 } 0392 0393 /** 0394 * Assert against DOM selection; should NOT contain exact number of nodes 0395 * 0396 * @param string $path CSS selector path 0397 * @param string $count Number of nodes that should NOT match 0398 * @param string $message 0399 */ 0400 public function assertNotQueryCount($path, $count, $message = '') 0401 { 0402 $this->_incrementAssertionCount(); 0403 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0404 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0405 $content = $this->response->outputBody(); 0406 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0407 $constraint->fail($path, $message); 0408 } 0409 } 0410 0411 /** 0412 * Assert against DOM selection; should contain at least this number of nodes 0413 * 0414 * @param string $path CSS selector path 0415 * @param string $count Minimum number of nodes that should match 0416 * @param string $message 0417 */ 0418 public function assertQueryCountMin($path, $count, $message = '') 0419 { 0420 $this->_incrementAssertionCount(); 0421 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0422 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0423 $content = $this->response->outputBody(); 0424 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0425 $constraint->fail($path, $message); 0426 } 0427 } 0428 0429 /** 0430 * Assert against DOM selection; should contain no more than this number of nodes 0431 * 0432 * @param string $path CSS selector path 0433 * @param string $count Maximum number of nodes that should match 0434 * @param string $message 0435 */ 0436 public function assertQueryCountMax($path, $count, $message = '') 0437 { 0438 $this->_incrementAssertionCount(); 0439 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0440 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0441 $content = $this->response->outputBody(); 0442 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0443 $constraint->fail($path, $message); 0444 } 0445 } 0446 0447 /** 0448 * Register XPath namespaces 0449 * 0450 * @param array $xpathNamespaces 0451 */ 0452 public function registerXpathNamespaces($xpathNamespaces) 0453 { 0454 $this->_xpathNamespaces = $xpathNamespaces; 0455 } 0456 0457 /** 0458 * Assert against XPath selection 0459 * 0460 * @param string $path XPath path 0461 * @param string $message 0462 */ 0463 public function assertXpath($path, $message = '') 0464 { 0465 $this->_incrementAssertionCount(); 0466 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0467 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0468 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0469 $content = $this->response->outputBody(); 0470 if (!$constraint->evaluate($content, __FUNCTION__)) { 0471 $constraint->fail($path, $message); 0472 } 0473 } 0474 0475 /** 0476 * Assert against XPath selection 0477 * 0478 * @param string $path XPath path 0479 * @param string $message 0480 */ 0481 public function assertNotXpath($path, $message = '') 0482 { 0483 $this->_incrementAssertionCount(); 0484 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0485 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0486 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0487 $content = $this->response->outputBody(); 0488 if (!$constraint->evaluate($content, __FUNCTION__)) { 0489 $constraint->fail($path, $message); 0490 } 0491 } 0492 0493 /** 0494 * Assert against XPath selection; node should contain content 0495 * 0496 * @param string $path XPath path 0497 * @param string $match content that should be contained in matched nodes 0498 * @param string $message 0499 */ 0500 public function assertXpathContentContains($path, $match, $message = '') 0501 { 0502 $this->_incrementAssertionCount(); 0503 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0504 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0505 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0506 $content = $this->response->outputBody(); 0507 if (!$constraint->evaluate($content, __FUNCTION__, $match)) { 0508 $constraint->fail($path, $message); 0509 } 0510 } 0511 0512 /** 0513 * Assert against XPath selection; node should NOT contain content 0514 * 0515 * @param string $path XPath path 0516 * @param string $match content that should NOT be contained in matched nodes 0517 * @param string $message 0518 */ 0519 public function assertNotXpathContentContains($path, $match, $message = '') 0520 { 0521 $this->_incrementAssertionCount(); 0522 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0523 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0524 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0525 $content = $this->response->outputBody(); 0526 if (!$constraint->evaluate($content, __FUNCTION__, $match)) { 0527 $constraint->fail($path, $message); 0528 } 0529 } 0530 0531 /** 0532 * Assert against XPath selection; node should match content 0533 * 0534 * @param string $path XPath path 0535 * @param string $pattern Pattern that should be contained in matched nodes 0536 * @param string $message 0537 */ 0538 public function assertXpathContentRegex($path, $pattern, $message = '') 0539 { 0540 $this->_incrementAssertionCount(); 0541 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0542 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0543 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0544 $content = $this->response->outputBody(); 0545 if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) { 0546 $constraint->fail($path, $message); 0547 } 0548 } 0549 0550 /** 0551 * Assert against XPath selection; node should NOT match content 0552 * 0553 * @param string $path XPath path 0554 * @param string $pattern pattern that should NOT be contained in matched nodes 0555 * @param string $message 0556 */ 0557 public function assertNotXpathContentRegex($path, $pattern, $message = '') 0558 { 0559 $this->_incrementAssertionCount(); 0560 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0561 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0562 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0563 $content = $this->response->outputBody(); 0564 if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) { 0565 $constraint->fail($path, $message); 0566 } 0567 } 0568 0569 /** 0570 * Assert against XPath selection; should contain exact number of nodes 0571 * 0572 * @param string $path XPath path 0573 * @param string $count Number of nodes that should match 0574 * @param string $message 0575 */ 0576 public function assertXpathCount($path, $count, $message = '') 0577 { 0578 $this->_incrementAssertionCount(); 0579 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0580 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0581 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0582 $content = $this->response->outputBody(); 0583 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0584 $constraint->fail($path, $message); 0585 } 0586 } 0587 0588 /** 0589 * Assert against XPath selection; should NOT contain exact number of nodes 0590 * 0591 * @param string $path XPath path 0592 * @param string $count Number of nodes that should NOT match 0593 * @param string $message 0594 */ 0595 public function assertNotXpathCount($path, $count, $message = '') 0596 { 0597 $this->_incrementAssertionCount(); 0598 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0599 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0600 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0601 $content = $this->response->outputBody(); 0602 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0603 $constraint->fail($path, $message); 0604 } 0605 } 0606 0607 /** 0608 * Assert against XPath selection; should contain at least this number of nodes 0609 * 0610 * @param string $path XPath path 0611 * @param string $count Minimum number of nodes that should match 0612 * @param string $message 0613 */ 0614 public function assertXpathCountMin($path, $count, $message = '') 0615 { 0616 $this->_incrementAssertionCount(); 0617 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0618 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0619 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0620 $content = $this->response->outputBody(); 0621 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0622 $constraint->fail($path, $message); 0623 } 0624 } 0625 0626 /** 0627 * Assert against XPath selection; should contain no more than this number of nodes 0628 * 0629 * @param string $path XPath path 0630 * @param string $count Maximum number of nodes that should match 0631 * @param string $message 0632 */ 0633 public function assertXpathCountMax($path, $count, $message = '') 0634 { 0635 $this->_incrementAssertionCount(); 0636 // require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php'; 0637 $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path); 0638 $constraint->registerXpathNamespaces($this->_xpathNamespaces); 0639 $content = $this->response->outputBody(); 0640 if (!$constraint->evaluate($content, __FUNCTION__, $count)) { 0641 $constraint->fail($path, $message); 0642 } 0643 } 0644 0645 /** 0646 * Assert that response is a redirect 0647 * 0648 * @param string $message 0649 */ 0650 public function assertRedirect($message = '') 0651 { 0652 $this->_incrementAssertionCount(); 0653 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0654 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0655 $response = $this->response; 0656 if (!$constraint->evaluate($response, __FUNCTION__)) { 0657 $constraint->fail($response, $message); 0658 } 0659 } 0660 0661 /** 0662 * Assert that response is NOT a redirect 0663 * 0664 * @param string $message 0665 */ 0666 public function assertNotRedirect($message = '') 0667 { 0668 $this->_incrementAssertionCount(); 0669 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0670 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0671 $response = $this->response; 0672 if (!$constraint->evaluate($response, __FUNCTION__)) { 0673 $constraint->fail($response, $message); 0674 } 0675 } 0676 0677 /** 0678 * Assert that response redirects to given URL 0679 * 0680 * @param string $url 0681 * @param string $message 0682 */ 0683 public function assertRedirectTo($url, $message = '') 0684 { 0685 $this->_incrementAssertionCount(); 0686 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0687 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0688 $response = $this->response; 0689 if (!$constraint->evaluate($response, __FUNCTION__, $url)) { 0690 $constraint->fail($response, $message); 0691 } 0692 } 0693 0694 /** 0695 * Assert that response does not redirect to given URL 0696 * 0697 * @param string $url 0698 * @param string $message 0699 */ 0700 public function assertNotRedirectTo($url, $message = '') 0701 { 0702 $this->_incrementAssertionCount(); 0703 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0704 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0705 $response = $this->response; 0706 if (!$constraint->evaluate($response, __FUNCTION__, $url)) { 0707 $constraint->fail($response, $message); 0708 } 0709 } 0710 0711 /** 0712 * Assert that redirect location matches pattern 0713 * 0714 * @param string $pattern 0715 * @param string $message 0716 */ 0717 public function assertRedirectRegex($pattern, $message = '') 0718 { 0719 $this->_incrementAssertionCount(); 0720 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0721 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0722 $response = $this->response; 0723 if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) { 0724 $constraint->fail($response, $message); 0725 } 0726 } 0727 0728 /** 0729 * Assert that redirect location does not match pattern 0730 * 0731 * @param string $pattern 0732 * @param string $message 0733 */ 0734 public function assertNotRedirectRegex($pattern, $message = '') 0735 { 0736 $this->_incrementAssertionCount(); 0737 // require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php'; 0738 $constraint = new Zend_Test_PHPUnit_Constraint_Redirect(); 0739 $response = $this->response; 0740 if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) { 0741 $constraint->fail($response, $message); 0742 } 0743 } 0744 0745 /** 0746 * Assert response code 0747 * 0748 * @param int $code 0749 * @param string $message 0750 */ 0751 public function assertResponseCode($code, $message = '') 0752 { 0753 $this->_incrementAssertionCount(); 0754 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0755 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0756 $response = $this->response; 0757 if (!$constraint->evaluate($response, __FUNCTION__, $code)) { 0758 $constraint->fail($response, $message); 0759 } 0760 } 0761 0762 /** 0763 * Assert response code 0764 * 0765 * @param int $code 0766 * @param string $message 0767 */ 0768 public function assertNotResponseCode($code, $message = '') 0769 { 0770 $this->_incrementAssertionCount(); 0771 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0772 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0773 $constraint->setNegate(true); 0774 $response = $this->response; 0775 if (!$constraint->evaluate($response, __FUNCTION__, $code)) { 0776 $constraint->fail($response, $message); 0777 } 0778 } 0779 0780 /** 0781 * Assert response header exists 0782 * 0783 * @param string $header 0784 * @param string $message 0785 */ 0786 public function assertHeader($header, $message = '') 0787 { 0788 $this->_incrementAssertionCount(); 0789 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0790 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0791 $response = $this->response; 0792 if (!$constraint->evaluate($response, __FUNCTION__, $header)) { 0793 $constraint->fail($response, $message); 0794 } 0795 } 0796 0797 /** 0798 * Assert response header does not exist 0799 * 0800 * @param string $header 0801 * @param string $message 0802 */ 0803 public function assertNotHeader($header, $message = '') 0804 { 0805 $this->_incrementAssertionCount(); 0806 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0807 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0808 $constraint->setNegate(true); 0809 $response = $this->response; 0810 if (!$constraint->evaluate($response, __FUNCTION__, $header)) { 0811 $constraint->fail($response, $message); 0812 } 0813 } 0814 0815 /** 0816 * Assert response header exists and contains the given string 0817 * 0818 * @param string $header 0819 * @param string $match 0820 * @param string $message 0821 */ 0822 public function assertHeaderContains($header, $match, $message = '') 0823 { 0824 $this->_incrementAssertionCount(); 0825 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0826 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0827 $response = $this->response; 0828 if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) { 0829 $constraint->fail($response, $message); 0830 } 0831 } 0832 0833 /** 0834 * Assert response header does not exist and/or does not contain the given string 0835 * 0836 * @param string $header 0837 * @param string $match 0838 * @param string $message 0839 */ 0840 public function assertNotHeaderContains($header, $match, $message = '') 0841 { 0842 $this->_incrementAssertionCount(); 0843 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0844 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0845 $constraint->setNegate(true); 0846 $response = $this->response; 0847 if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) { 0848 $constraint->fail($response, $message); 0849 } 0850 } 0851 0852 /** 0853 * Assert response header exists and matches the given pattern 0854 * 0855 * @param string $header 0856 * @param string $pattern 0857 * @param string $message 0858 */ 0859 public function assertHeaderRegex($header, $pattern, $message = '') 0860 { 0861 $this->_incrementAssertionCount(); 0862 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0863 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0864 $response = $this->response; 0865 if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) { 0866 $constraint->fail($response, $message); 0867 } 0868 } 0869 0870 /** 0871 * Assert response header does not exist and/or does not match the given regex 0872 * 0873 * @param string $header 0874 * @param string $pattern 0875 * @param string $message 0876 */ 0877 public function assertNotHeaderRegex($header, $pattern, $message = '') 0878 { 0879 $this->_incrementAssertionCount(); 0880 // require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php'; 0881 $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader(); 0882 $constraint->setNegate(true); 0883 $response = $this->response; 0884 if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) { 0885 $constraint->fail($response, $message); 0886 } 0887 } 0888 0889 /** 0890 * Assert that the last handled request used the given module 0891 * 0892 * @param string $module 0893 * @param string $message 0894 */ 0895 public function assertModule($module, $message = '') 0896 { 0897 $this->_incrementAssertionCount(); 0898 if ($module != $this->request->getModuleName()) { 0899 $msg = sprintf('Failed asserting last module used <"%s"> was "%s"', 0900 $this->request->getModuleName(), 0901 $module 0902 ); 0903 if (!empty($message)) { 0904 $msg = $message . "\n" . $msg; 0905 } 0906 $this->fail($msg); 0907 } 0908 } 0909 0910 /** 0911 * Assert that the last handled request did NOT use the given module 0912 * 0913 * @param string $module 0914 * @param string $message 0915 */ 0916 public function assertNotModule($module, $message = '') 0917 { 0918 $this->_incrementAssertionCount(); 0919 if ($module == $this->request->getModuleName()) { 0920 $msg = sprintf('Failed asserting last module used was NOT "%s"', $module); 0921 if (!empty($message)) { 0922 $msg = $message . "\n" . $msg; 0923 } 0924 $this->fail($msg); 0925 } 0926 } 0927 0928 /** 0929 * Assert that the last handled request used the given controller 0930 * 0931 * @param string $controller 0932 * @param string $message 0933 */ 0934 public function assertController($controller, $message = '') 0935 { 0936 $this->_incrementAssertionCount(); 0937 if ($controller != $this->request->getControllerName()) { 0938 $msg = sprintf('Failed asserting last controller used <"%s"> was "%s"', 0939 $this->request->getControllerName(), 0940 $controller 0941 ); 0942 if (!empty($message)) { 0943 $msg = $message . "\n" . $msg; 0944 } 0945 $this->fail($msg); 0946 } 0947 } 0948 0949 /** 0950 * Assert that the last handled request did NOT use the given controller 0951 * 0952 * @param string $controller 0953 * @param string $message 0954 */ 0955 public function assertNotController($controller, $message = '') 0956 { 0957 $this->_incrementAssertionCount(); 0958 if ($controller == $this->request->getControllerName()) { 0959 $msg = sprintf('Failed asserting last controller used <"%s"> was NOT "%s"', 0960 $this->request->getControllerName(), 0961 $controller 0962 ); 0963 if (!empty($message)) { 0964 $msg = $message . "\n" . $msg; 0965 } 0966 $this->fail($msg); 0967 } 0968 } 0969 0970 /** 0971 * Assert that the last handled request used the given action 0972 * 0973 * @param string $action 0974 * @param string $message 0975 */ 0976 public function assertAction($action, $message = '') 0977 { 0978 $this->_incrementAssertionCount(); 0979 if ($action != $this->request->getActionName()) { 0980 $msg = sprintf('Failed asserting last action used <"%s"> was "%s"', $this->request->getActionName(), $action); 0981 if (!empty($message)) { 0982 $msg = $message . "\n" . $msg; 0983 } 0984 $this->fail($msg); 0985 } 0986 } 0987 0988 /** 0989 * Assert that the last handled request did NOT use the given action 0990 * 0991 * @param string $action 0992 * @param string $message 0993 */ 0994 public function assertNotAction($action, $message = '') 0995 { 0996 $this->_incrementAssertionCount(); 0997 if ($action == $this->request->getActionName()) { 0998 $msg = sprintf('Failed asserting last action used <"%s"> was NOT "%s"', $this->request->getActionName(), $action); 0999 if (!empty($message)) { 1000 $msg = $message . "\n" . $msg; 1001 } 1002 $this->fail($msg); 1003 } 1004 } 1005 1006 /** 1007 * Assert that the specified route was used 1008 * 1009 * @param string $route 1010 * @param string $message 1011 */ 1012 public function assertRoute($route, $message = '') 1013 { 1014 $this->_incrementAssertionCount(); 1015 $router = $this->frontController->getRouter(); 1016 if ($route != $router->getCurrentRouteName()) { 1017 $msg = sprintf('Failed asserting matched route was "%s", actual route is %s', 1018 $route, 1019 $router->getCurrentRouteName() 1020 ); 1021 if (!empty($message)) { 1022 $msg = $message . "\n" . $msg; 1023 } 1024 $this->fail($msg); 1025 } 1026 } 1027 1028 /** 1029 * Assert that the route matched is NOT as specified 1030 * 1031 * @param string $route 1032 * @param string $message 1033 */ 1034 public function assertNotRoute($route, $message = '') 1035 { 1036 $this->_incrementAssertionCount(); 1037 $router = $this->frontController->getRouter(); 1038 if ($route == $router->getCurrentRouteName()) { 1039 $msg = sprintf('Failed asserting route matched was NOT "%s"', $route); 1040 if (!empty($message)) { 1041 $msg = $message . "\n" . $msg; 1042 } 1043 $this->fail($msg); 1044 } 1045 } 1046 1047 /** 1048 * Retrieve front controller instance 1049 * 1050 * @return Zend_Controller_Front 1051 */ 1052 public function getFrontController() 1053 { 1054 if (null === $this->_frontController) { 1055 $this->_frontController = Zend_Controller_Front::getInstance(); 1056 } 1057 return $this->_frontController; 1058 } 1059 1060 /** 1061 * Retrieve test case request object 1062 * 1063 * @return Zend_Controller_Request_HttpTestCase 1064 */ 1065 public function getRequest() 1066 { 1067 if (null === $this->_request) { 1068 // require_once 'Zend/Controller/Request/HttpTestCase.php'; 1069 $this->_request = new Zend_Controller_Request_HttpTestCase; 1070 } 1071 return $this->_request; 1072 } 1073 1074 /** 1075 * Retrieve test case response object 1076 * 1077 * @return Zend_Controller_Response_HttpTestCase 1078 */ 1079 public function getResponse() 1080 { 1081 if (null === $this->_response) { 1082 // require_once 'Zend/Controller/Response/HttpTestCase.php'; 1083 $this->_response = new Zend_Controller_Response_HttpTestCase; 1084 } 1085 return $this->_response; 1086 } 1087 1088 /** 1089 * Retrieve DOM query object 1090 * 1091 * @return Zend_Dom_Query 1092 */ 1093 public function getQuery() 1094 { 1095 if (null === $this->_query) { 1096 // require_once 'Zend/Dom/Query.php'; 1097 $this->_query = new Zend_Dom_Query; 1098 } 1099 return $this->_query; 1100 } 1101 1102 /** 1103 * URL Helper 1104 * 1105 * @param array $urlOptions 1106 * @param string $name 1107 * @param bool $reset 1108 * @param bool $encode 1109 * @throws Exception 1110 * @throws Zend_Controller_Router_Exception 1111 * @return string 1112 */ 1113 public function url($urlOptions = array(), $name = null, $reset = false, $encode = true) 1114 { 1115 $frontController = $this->getFrontController(); 1116 $router = $frontController->getRouter(); 1117 if (!$router instanceof Zend_Controller_Router_Rewrite) { 1118 throw new Exception('This url helper utility function only works when the router is of type Zend_Controller_Router_Rewrite'); 1119 } 1120 if (count($router->getRoutes()) == 0) { 1121 $router->addDefaultRoutes(); 1122 } 1123 return $router->assemble($urlOptions, $name, $reset, $encode); 1124 } 1125 1126 /** 1127 * Urlize options 1128 * 1129 * @param array $urlOptions 1130 * @param bool $actionControllerModuleOnly 1131 * @return mixed 1132 */ 1133 public function urlizeOptions($urlOptions, $actionControllerModuleOnly = true) 1134 { 1135 $ccToDash = new Zend_Filter_Word_CamelCaseToDash(); 1136 foreach ($urlOptions as $n => $v) { 1137 if (in_array($n, array('action', 'controller', 'module'))) { 1138 $urlOptions[$n] = $ccToDash->filter($v); 1139 } 1140 } 1141 return $urlOptions; 1142 } 1143 1144 /** 1145 * Increment assertion count 1146 */ 1147 protected function _incrementAssertionCount() 1148 { 1149 $stack = debug_backtrace(); 1150 foreach ($stack as $step) { 1151 if (isset($step['object']) 1152 && $step['object'] instanceof PHPUnit_Framework_TestCase 1153 ) { 1154 if (version_compare(PHPUnit_Runner_Version::id(), '3.3.0', 'lt')) { 1155 break; 1156 } elseif (version_compare(PHPUnit_Runner_Version::id(), '3.3.3', 'lt')) { 1157 $step['object']->incrementAssertionCounter(); 1158 } else { 1159 $step['object']->addToAssertionCount(1); 1160 } 1161 break; 1162 } 1163 } 1164 } 1165 }