| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?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_Controller
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_Controller_ActionTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Controller_ActionTest::main");
- }
- require_once 'Zend/Controller/Action.php';
- require_once 'Zend/Controller/Action/Helper/Redirector.php';
- require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
- require_once 'Zend/Controller/Request/Http.php';
- require_once 'Zend/Controller/Response/Cli.php';
- /**
- * @category Zend
- * @package Zend_Controller
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Controller
- * @group Zend_Controller_Action
- */
- class Zend_Controller_ActionTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Runs the test methods of this class.
- *
- * @access public
- * @static
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_ActionTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- public function setUp()
- {
- Zend_Controller_Action_HelperBroker::resetHelpers();
- $front = Zend_Controller_Front::getInstance();
- $front->resetInstance();
- $front->setControllerDirectory('.', 'default');
- $this->_controller = new Zend_Controller_ActionTest_TestController(
- new Zend_Controller_Request_Http(),
- new Zend_Controller_Response_Cli(),
- array(
- 'foo' => 'bar',
- 'bar' => 'baz'
- )
- );
- $redirector = $this->_controller->getHelper('redirector');
- $redirector->setExit(false);
- }
- public function tearDown()
- {
- unset($this->_controller);
- }
- public function testInit()
- {
- $this->assertEquals('bar', $this->_controller->initArgs['foo']);
- $this->assertEquals('baz', $this->_controller->initArgs['bar']);
- }
- public function testPreRun()
- {
- $this->_controller->preDispatch();
- $this->assertNotContains('Prerun ran', $this->_controller->getResponse()->getBody());
- $this->_controller->getRequest()->setParam('prerun', true);
- $this->_controller->preDispatch();
- $this->assertContains('Prerun ran', $this->_controller->getResponse()->getBody());
- }
- public function testPostRun()
- {
- $this->_controller->postDispatch();
- $this->assertNotContains('Postrun ran', $this->_controller->getResponse()->getBody());
- $this->_controller->getRequest()->setParam('postrun', true);
- $this->_controller->postDispatch();
- $this->assertContains('Postrun ran', $this->_controller->getResponse()->getBody());
- }
- public function testGetRequest()
- {
- $this->assertTrue($this->_controller->getRequest() instanceof Zend_Controller_Request_Abstract);
- }
- public function testGetResponse()
- {
- $this->assertTrue($this->_controller->getResponse() instanceof Zend_Controller_Response_Abstract);
- }
- public function testGetInvokeArgs()
- {
- $expected = array('foo' => 'bar', 'bar' => 'baz');
- $this->assertSame($expected, $this->_controller->getInvokeArgs());
- }
- public function testGetInvokeArg()
- {
- $this->assertSame('bar', $this->_controller->getInvokeArg('foo'));
- $this->assertSame('baz', $this->_controller->getInvokeArg('bar'));
- }
- public function testForwardActionOnly()
- {
- $this->_controller->forward('forwarded');
- $this->assertEquals('forwarded', $this->_controller->getRequest()->getActionName());
- $this->assertFalse($this->_controller->getRequest()->isDispatched());
- }
- public function testForwardActionKeepsController()
- {
- $request = $this->_controller->getRequest();
- $request->setControllerName('foo')
- ->setActionName('bar');
- $this->_controller->forward('forwarded');
- $this->assertEquals('forwarded', $request->getActionName());
- $this->assertEquals('foo', $request->getControllerName());
- $this->assertFalse($request->isDispatched());
- }
- public function testForwardActionAndController()
- {
- $request = $this->_controller->getRequest();
- $request->setControllerName('foo')
- ->setActionName('bar');
- $this->_controller->forward('forwarded', 'bar');
- $this->assertEquals('forwarded', $request->getActionName());
- $this->assertEquals('bar', $request->getControllerName());
- $this->assertFalse($request->isDispatched());
- }
- public function testForwardActionControllerAndModule()
- {
- $request = $this->_controller->getRequest();
- $request->setControllerName('foo')
- ->setActionName('bar')
- ->setModuleName('admin');
- $this->_controller->forward('forwarded', 'bar');
- $this->assertEquals('forwarded', $request->getActionName());
- $this->assertEquals('bar', $request->getControllerName());
- $this->assertEquals('admin', $request->getModuleName());
- $this->assertFalse($request->isDispatched());
- }
- public function testForwardCanSetParams()
- {
- $request = $this->_controller->getRequest();
- $request->setParams(array('admin' => 'batman'));
- $this->_controller->forward('forwarded', null, null, array('foo' => 'bar'));
- $this->assertEquals('forwarded', $request->getActionName());
- $received = $request->getParams();
- $this->assertTrue(isset($received['foo']));
- $this->assertEquals('bar', $received['foo']);
- $this->assertFalse($request->isDispatched());
- }
- public function testRun()
- {
- $response = $this->_controller->run();
- $body = $response->getBody();
- $this->assertContains('In the index action', $body, var_export($this->_controller->getRequest(), 1));
- $this->assertNotContains('Prerun ran', $body, $body);
- }
- public function testRun2()
- {
- $this->_controller->getRequest()->setActionName('bar');
- try {
- $response = $this->_controller->run();
- $this->fail('Should not be able to call bar as action');
- } catch (Exception $e) {
- //success!
- }
- }
- public function testRun3()
- {
- $this->_controller->getRequest()->setActionName('foo');
- $response = $this->_controller->run();
- $this->assertContains('In the foo action', $response->getBody());
- $this->assertNotContains('Prerun ran', $this->_controller->getResponse()->getBody());
- }
- public function testHasParam()
- {
- $request = $this->_controller->getRequest();
- $request->setParam('foo', 'bar');
- $request->setParam('baz', 'bal');
- $this->assertTrue($this->_controller->hasParam('foo'));
- $this->assertTrue($this->_controller->hasParam('baz'));
- }
- public function testSetParam()
- {
- $this->_controller->setParam('foo', 'bar');
- $params = $this->_controller->getAllParams();
- $this->assertTrue(isset($params['foo']));
- $this->assertEquals('bar', $params['foo']);
- }
- /**
- * @group ZF-5163
- */
- public function testGetParamForZeroValues()
- {
- $this->_controller->setParam('foo', 'bar');
- $this->_controller->setParam('bar', 0);
- $this->_controller->setParam('baz', null);
- $this->assertEquals('bar', $this->_controller->getParam('foo', -1));
- $this->assertEquals(0, $this->_controller->getParam('bar', -1));
- $this->assertEquals(-1, $this->_controller->getParam('baz', -1));
- }
- /**
- * @group ZF-9179
- */
- public function testGetParamForEmptyString()
- {
- $this->_controller->setParam('lang', '');
- $this->assertEquals('en', $this->_controller->getParam('lang', 'en'));
- }
- public function testGetParams()
- {
- $this->_controller->setParam('foo', 'bar');
- $this->_controller->setParam('bar', 'baz');
- $this->_controller->setParam('boo', 'bah');
- $params = $this->_controller->getAllParams();
- $this->assertEquals('bar', $params['foo']);
- $this->assertEquals('baz', $params['bar']);
- $this->assertEquals('bah', $params['boo']);
- }
- public function testRedirect()
- {
- $response = $this->_controller->getResponse();
- $response->headersSentThrowsException = false;
- $this->_controller->redirect('/baz/foo');
- $this->_controller->redirect('/foo/bar');
- $headers = $response->getHeaders();
- $found = 0;
- $url = '';
- foreach ($headers as $header) {
- if ('Location' == $header['name']) {
- ++$found;
- $url = $header['value'];
- break;
- }
- }
- $this->assertEquals(1, $found);
- $this->assertContains('/foo/bar', $url);
- }
- public function testInitView()
- {
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController(
- new Zend_Controller_Request_Http(),
- new Zend_Controller_Response_Cli()
- );
- $view = $controller->initView();
- $this->assertTrue($view instanceof Zend_View);
- $scriptPath = $view->getScriptPaths();
- $this->assertTrue(is_array($scriptPath));
- $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'scripts/', $scriptPath[0]);
- }
- public function testRender()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('index');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->indexAction();
- $this->assertContains('In the index action view', $response->getBody());
- }
- public function testRenderByName()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('test');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->testAction();
- $this->assertContains('In the index action view', $response->getBody());
- }
- public function testRenderOutsideControllerSubdir()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('site');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->siteAction();
- $this->assertContains('In the sitewide view', $response->getBody());
- }
- public function testRenderNamedSegment()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('name');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->nameAction();
- $this->assertContains('In the name view', $response->getBody('name'));
- }
- public function testRenderNormalizesScriptName()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('foo.bar')
- ->setActionName('baz_bat');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'FooBarController.php';
- $controller = new FooBarController($request, $response);
- $controller->bazBatAction();
- $this->assertContains('Inside foo-bar/baz-bat.phtml', $response->getBody());
- }
- public function testGetViewScript()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('test');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $script = $controller->getViewScript();
- $this->assertContains('view' . DIRECTORY_SEPARATOR . 'test.phtml', $script);
- $script = $controller->getViewScript('foo');
- $this->assertContains('view' . DIRECTORY_SEPARATOR . 'foo.phtml', $script);
- }
- public function testGetViewScriptDoesNotOverwriteNoControllerFlagWhenNullPassed()
- {
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('test');
- $response = new Zend_Controller_Response_Cli();
- $controller = new ViewController($request, $response);
- $this->assertSame($viewRenderer->getActionController(), $controller);
- $viewRenderer->setNoController(true);
- $this->assertTrue($viewRenderer->getNoController());
- $script = $controller->getViewScript();
- $this->assertTrue($viewRenderer->getNoController());
- }
- public function testRenderScript()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('script');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->scriptAction();
- $this->assertContains('Inside custom/renderScript.php', $response->getBody());
- }
- public function testRenderScriptToNamedResponseSegment()
- {
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('script-name');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->scriptNameAction();
- $this->assertContains('Inside custom/renderScript.php', $response->getBody('foo'));
- }
- public function testGetHelper()
- {
- $redirector = $this->_controller->getHelper('redirector');
- $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Abstract);
- $this->assertTrue($redirector instanceof Zend_Controller_Action_Helper_Redirector);
- }
- public function testGetHelperCopy()
- {
- $redirector = $this->_controller->getHelper('redirector');
- $copy = $this->_controller->getHelperCopy('redirector');
- $this->assertNotSame($redirector, $copy);
- $this->assertTrue($copy instanceof Zend_Controller_Action_Helper_Redirector);
- }
- public function testViewInjectionUsingViewRenderer()
- {
- Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('script');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $this->assertNotNull($controller->view);
- }
- public function testRenderUsingViewRenderer()
- {
- Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
- $request = new Zend_Controller_Request_Http();
- $request->setControllerName('view')
- ->setActionName('script');
- $response = new Zend_Controller_Response_Cli();
- Zend_Controller_Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'ViewController.php';
- $controller = new ViewController($request, $response);
- $controller->scriptAction();
- $this->assertContains('Inside custom/renderScript.php', $response->getBody());
- }
- public function testMissingActionExceptionsDifferFromMissingMethods()
- {
- try {
- $this->_controller->bogusAction();
- $this->fail('Invalid action should throw exception');
- } catch (Zend_Controller_Exception $e) {
- $this->assertRegexp('/^Action.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
- $this->assertContains('bogus', $e->getMessage());
- $this->assertNotContains('bogusAction', $e->getMessage());
- $this->assertEquals(404, $e->getCode());
- }
- try {
- $this->_controller->bogus();
- $this->fail('Invalid method should throw exception');
- } catch (Zend_Controller_Exception $e) {
- $this->assertRegexp('/^Method.*?(does not exist and was not trapped in __call\(\))$/', $e->getMessage());
- $this->assertContains('bogus', $e->getMessage());
- $this->assertEquals(500, $e->getCode());
- }
- }
- }
- class Zend_Controller_ActionTest_TestController extends Zend_Controller_Action
- {
- public $initArgs = array();
- public function init()
- {
- $this->initArgs['foo'] = $this->getInvokeArg('foo');
- $this->initArgs['bar'] = $this->getInvokeArg('bar');
- }
- public function preDispatch()
- {
- if (false !== ($param = $this->_getParam('prerun', false))) {
- $this->getResponse()->appendBody("Prerun ran\n");
- }
- }
- public function postDispatch()
- {
- if (false !== ($param = $this->_getParam('postrun', false))) {
- $this->getResponse()->appendBody("Postrun ran\n");
- }
- }
- public function noRouteAction()
- {
- return $this->indexAction();
- }
- public function indexAction()
- {
- $this->getResponse()->appendBody("In the index action\n");
- }
- public function fooAction()
- {
- $this->getResponse()->appendBody("In the foo action\n");
- }
- public function bar()
- {
- $this->getResponse()->setBody("Should never see this\n");
- }
- }
- // Call Zend_Controller_ActionTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Controller_ActionTest::main") {
- Zend_Controller_ActionTest::main();
- }
|