/library/Test/PHPUnit/ControllerTestCase.php
PHP | 1182 lines | 588 code | 76 blank | 518 comment | 76 complexity | 2d54a618bce9d5b12fd23bd0505c6a1d MD5 | raw file
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Test
- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id: ControllerTestCase.php 22291 2010-05-25 15:52:09Z bradley.holt $
- */
- /** @see PHPUnit_Framework_TestCase */
- #require_once 'PHPUnit/Framework/TestCase.php';
- /** @see PHPUnit_Runner_Version */
- #require_once 'PHPUnit/Runner/Version.php';
- /** @see Zend_Controller_Front */
- #require_once 'Zend/Controller/Front.php';
- /** @see Zend_Controller_Action_HelperBroker */
- #require_once 'Zend/Controller/Action/HelperBroker.php';
- /** @see Zend_Layout */
- #require_once 'Zend/Layout.php';
- /** @see Zend_Session */
- #require_once 'Zend/Session.php';
- /** @see Zend_Registry */
- #require_once 'Zend/Registry.php';
- /**
- * Functional testing scaffold for MVC applications
- *
- * @uses PHPUnit_Framework_TestCase
- * @category Zend
- * @package Zend_Test
- * @subpackage PHPUnit
- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase
- {
- /**
- * @var mixed Bootstrap file path or callback
- */
- public $bootstrap;
- /**
- * @var Zend_Controller_Front
- */
- protected $_frontController;
- /**
- * @var Zend_Dom_Query
- */
- protected $_query;
- /**
- * @var Zend_Controller_Request_Abstract
- */
- protected $_request;
- /**
- * @var Zend_Controller_Response_Abstract
- */
- protected $_response;
- /**
- * XPath namespaces
- * @var array
- */
- protected $_xpathNamespaces = array();
- /**
- * Overloading: prevent overloading to special properties
- *
- * @param string $name
- * @param mixed $value
- * @return void
- */
- public function __set($name, $value)
- {
- if (in_array($name, array('request', 'response', 'frontController'))) {
- #require_once 'Zend/Exception.php';
- throw new Zend_Exception(sprintf('Setting %s object manually is not allowed', $name));
- }
- $this->$name = $value;
- }
- /**
- * Overloading for common properties
- *
- * Provides overloading for request, response, and frontController objects.
- *
- * @param mixed $name
- * @return void
- */
- public function __get($name)
- {
- switch ($name) {
- case 'request':
- return $this->getRequest();
- case 'response':
- return $this->getResponse();
- case 'frontController':
- return $this->getFrontController();
- }
- return null;
- }
- /**
- * Set up MVC app
- *
- * Calls {@link bootstrap()} by default
- *
- * @return void
- */
- protected function setUp()
- {
- $this->bootstrap();
- }
- /**
- * Bootstrap the front controller
- *
- * Resets the front controller, and then bootstraps it.
- *
- * If {@link $bootstrap} is a callback, executes it; if it is a file, it include's
- * it. When done, sets the test case request and response objects into the
- * front controller.
- *
- * @return void
- */
- final public function bootstrap()
- {
- $this->reset();
- if (null !== $this->bootstrap) {
- if ($this->bootstrap instanceof Zend_Application) {
- $this->bootstrap->bootstrap();
- $this->_frontController = $this->bootstrap->getBootstrap()->getResource('frontcontroller');
- } elseif (is_callable($this->bootstrap)) {
- call_user_func($this->bootstrap);
- } elseif (is_string($this->bootstrap)) {
- #require_once 'Zend/Loader.php';
- if (Zend_Loader::isReadable($this->bootstrap)) {
- include $this->bootstrap;
- }
- }
- }
- $this->frontController
- ->setRequest($this->getRequest())
- ->setResponse($this->getResponse());
- }
- /**
- * Dispatch the MVC
- *
- * If a URL is provided, sets it as the request URI in the request object.
- * Then sets test case request and response objects in front controller,
- * disables throwing exceptions, and disables returning the response.
- * Finally, dispatches the front controller.
- *
- * @param string|null $url
- * @return void
- */
- public function dispatch($url = null)
- {
- // redirector should not exit
- $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
- $redirector->setExit(false);
- // json helper should not exit
- $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
- $json->suppressExit = true;
- $request = $this->getRequest();
- if (null !== $url) {
- $request->setRequestUri($url);
- }
- $request->setPathInfo(null);
- $controller = $this->getFrontController();
- $this->frontController
- ->setRequest($request)
- ->setResponse($this->getResponse())
- ->throwExceptions(false)
- ->returnResponse(false);
- if ($this->bootstrap instanceof Zend_Application) {
- $this->bootstrap->run();
- } else {
- $this->frontController->dispatch();
- }
- }
- /**
- * Reset MVC state
- *
- * Creates new request/response objects, resets the front controller
- * instance, and resets the action helper broker.
- *
- * @todo Need to update Zend_Layout to add a resetInstance() method
- * @return void
- */
- public function reset()
- {
- $_SESSION = array();
- $_GET = array();
- $_POST = array();
- $_COOKIE = array();
- $this->resetRequest();
- $this->resetResponse();
- Zend_Layout::resetMvcInstance();
- Zend_Controller_Action_HelperBroker::resetHelpers();
- $this->frontController->resetInstance();
- Zend_Session::$_unitTestEnabled = true;
- }
- /**
- * Rest all view placeholders
- *
- * @return void
- */
- protected function _resetPlaceholders()
- {
- $registry = Zend_Registry::getInstance();
- $remove = array();
- foreach ($registry as $key => $value) {
- if (strstr($key, '_View_')) {
- $remove[] = $key;
- }
- }
- foreach ($remove as $key) {
-