| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?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_Plugin_ActionStackTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Controller_Plugin_ActionStackTest::main");
- }
- require_once 'Zend/Controller/Plugin/ActionStack.php';
- require_once 'Zend/Controller/Request/Simple.php';
- require_once 'Zend/Registry.php';
- /**
- * Test class for Zend_Controller_Plugin_ActionStack.
- *
- * @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_Plugin
- */
- class Zend_Controller_Plugin_ActionStackTest extends PHPUnit_Framework_TestCase
- {
- public $key = 'Zend_Controller_Plugin_ActionStack';
- public $registry;
- /**
- * Runs the test methods of this class.
- *
- * @access public
- * @static
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Plugin_ActionStackTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Sets up the fixture, for example, open a network connection.
- * This method is called before a test is executed.
- *
- * @return void
- */
- public function setUp()
- {
- $this->removeRegistryEntry();
- $this->registry = Zend_Registry::getInstance();
- }
- /**
- * Tears down the fixture, for example, close a network connection.
- * This method is called after a test is executed.
- *
- * @return void
- */
- protected function tearDown()
- {
- $this->removeRegistryEntry();
- }
- /**
- * Ensure registry is clean
- *
- * @return void
- */
- public function removeRegistryEntry()
- {
- $registry = Zend_Registry::getInstance();
- if (isset($registry[$this->key])) {
- unset($registry[$this->key]);
- }
- }
- public function testConstructorCreatesRegistryEntry()
- {
- $registry = Zend_Registry::getInstance();
- $this->assertFalse(isset($registry[$this->key]));
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $key = $plugin->getRegistryKey();
- $this->assertTrue(isset($registry[$key]));
- }
- public function testKeyPassedToConstructorUsedAsRegistryKey()
- {
- $this->key = $key = 'foobar';
- $registry = Zend_Registry::getInstance();
- $this->assertFalse(isset($registry[$key]));
- $plugin = new Zend_Controller_Plugin_ActionStack(null, $key);
- $this->assertTrue(isset($registry[$key]));
- }
- public function testRegistryPassedToConstructorUsedByPlugin()
- {
- $registry = new Zend_Controller_Plugin_ActionStack_Registry();
- $plugin = new Zend_Controller_Plugin_ActionStack($registry);
- $registered = $plugin->getRegistry();
- $this->assertNotSame($this->registry, $registered);
- $this->assertSame($registry, $registered);
- }
- /**
- * @return void
- */
- public function testRegistryAccessorsWork()
- {
- $registry = new Zend_Controller_Plugin_ActionStack_Registry();
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $original = $plugin->getRegistry();
- $plugin->setRegistry($registry);
- $registered = $plugin->getRegistry();
- $this->assertSame($registry, $registered);
- $this->assertNotSame($original, $registered);
- }
- public function testRegistryKeyHasDefaultValue()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $key = $plugin->getRegistryKey();
- $this->assertNotNull($key);
- $this->assertEquals($this->key, $key);
- }
- /**
- * @return void
- */
- public function testRegistryKeyAccessorsWork()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $plugin->setRegistryKey('foobar');
- $key = $plugin->getRegistryKey();
- $this->assertEquals('foobar', $key);
- }
- /**
- * @return void
- */
- public function testGetStackInitiallyReturnsEmptyArray()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $stack = $plugin->getStack();
- $this->assertTrue(is_array($stack));
- $this->assertTrue(empty($stack));
- }
- /**
- * @return void
- */
- public function testPushStackAppendsToStack()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request1 = new Zend_Controller_Request_Simple();
- $plugin->pushStack($request1);
- $received = $plugin->getStack();
- $this->assertTrue(is_array($received));
- $this->assertEquals(1, count($received));
- $this->assertSame($request1, $received[0]);
- $request2 = new Zend_Controller_Request_Simple();
- $plugin->pushStack($request2);
- $received = $plugin->getStack();
- $this->assertTrue(is_array($received));
- $this->assertEquals(2, count($received));
- $this->assertSame($request2, $received[1]);
- $this->assertSame($request1, $received[0]);
- }
- public function getNewRequest()
- {
- $request = new Zend_Controller_Request_Simple();
- $request->setActionName('baz')
- ->setControllerName('bar')
- ->setModuleName('foo');
- return $request;
- }
- /**
- * @return void
- */
- public function testPopStackPullsFromEndOfStack()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request1 = $this->getNewRequest();
- $request2 = $this->getNewRequest();
- $request3 = $this->getNewRequest();
- $plugin->pushStack($request1)
- ->pushStack($request2)
- ->pushStack($request3);
- $stack = $plugin->getStack();
- $this->assertEquals(3, count($stack));
- $received = $plugin->popStack();
- $stack = $plugin->getStack();
- $this->assertSame($request3, $received);
- $this->assertEquals(2, count($stack));
- }
- public function testPopEmptyStackReturnsFalse()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $received = $plugin->popStack();
- $this->assertFalse($received);
- }
- public function testPopStackPopsMultipleItemsWhenRequestActionEmpty()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request1 = $this->getNewRequest();
- $request2 = new Zend_Controller_Request_Simple();
- $plugin->pushStack($request1)
- ->pushStack($request2);
- $stack = $plugin->getStack();
- $this->assertEquals(2, count($stack));
- $received = $plugin->popStack();
- $stack = $plugin->getStack();
- $this->assertSame($request1, $received);
- $this->assertEquals(0, count($stack));
- }
- public function testPopStackPopulatesControllerAndModuleFromRequestIfEmpty()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = $this->getNewRequest();
- $plugin->setRequest($request);
- $request1 = new Zend_Controller_Request_Simple();
- $request1->setActionName('blah');
- $plugin->pushStack($request1);
- $next = $plugin->popStack();
- $this->assertTrue($next instanceof Zend_Controller_Request_Simple);
- $this->assertEquals($request1->getActionName(), $next->getActionName());
- $this->assertEquals($request->getControllerName(), $next->getControllerName());
- $this->assertEquals($request->getModuleName(), $next->getModuleName());
- }
- public function testForwardResetsInternalRequestStateFromGivenRequest()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = new Zend_Controller_Request_Simple();
- $plugin->setRequest($request);
- $next = $this->getNewRequest();
- $plugin->forward($next);
- $this->assertEquals($next->getActionName(), $request->getActionName());
- $this->assertEquals($next->getControllerName(), $request->getControllerName());
- $this->assertEquals($next->getModuleName(), $request->getModuleName());
- $this->assertFalse($request->isDispatched());
- }
- public function testForwardResetsRequestParamsIfFlagSet()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = $this->getNewRequest();
- $params = array('foo' => 'bar','baz'=>'bat');
- $request->setParams($params);
- $plugin->setRequest($request);
- $this->assertEquals($params,$plugin->getRequest()->getParams());
- $next = $this->getNewRequest();
- $plugin->forward($next);
- $this->assertEquals($params,$plugin->getRequest()->getParams());
- $plugin->setClearRequestParams(true);
- $next = $this->getNewRequest();
- $plugin->forward($next);
- $this->assertEquals(array(),$plugin->getRequest()->getParams());
- }
- /**
- * @return void
- */
- public function testPostDispatchResetsInternalRequestFromLastRequestOnStack()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = new Zend_Controller_Request_Simple();
- $request->setDispatched(true);
- $plugin->setRequest($request);
- $request1 = $this->getNewRequest();
- $request2 = $this->getNewRequest();
- $request3 = $this->getNewRequest();
- $request3->setActionName('foobar')
- ->setControllerName('bazbat')
- ->setModuleName('bogus');
- $plugin->pushStack($request1)
- ->pushStack($request2)
- ->pushStack($request3);
- $plugin->postDispatch($request);
- $this->assertEquals($request3->getActionName(), $request->getActionName());
- $this->assertEquals($request3->getControllerName(), $request->getControllerName());
- $this->assertEquals($request3->getModuleName(), $request->getModuleName());
- $this->assertFalse($request->isDispatched());
- }
- public function testPostDispatchDoesNothingWithEmptyStack()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = $this->getNewRequest();
- $request->setDispatched(true);
- $clone = clone $request;
- $plugin->postDispatch($request);
- $this->assertEquals($clone->getActionName(), $request->getActionName());
- $this->assertEquals($clone->getControllerName(), $request->getControllerName());
- $this->assertEquals($clone->getModuleName(), $request->getModuleName());
- $this->assertTrue($request->isDispatched());
- }
- public function testPostDispatchDoesNothingWithStackThatEvaluatesToEmpty()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = new Zend_Controller_Request_Simple();
- $request->setDispatched(true);
- $plugin->setRequest($request);
- $request1 = new Zend_Controller_Request_Simple();
- $request2 = new Zend_Controller_Request_Simple();
- $request3 = new Zend_Controller_Request_Simple();
- $plugin->pushStack($request1)
- ->pushStack($request2)
- ->pushStack($request3);
- $clone = clone $request;
- $plugin->postDispatch($request);
- $this->assertEquals($clone->getActionName(), $request->getActionName());
- $this->assertEquals($clone->getControllerName(), $request->getControllerName());
- $this->assertEquals($clone->getModuleName(), $request->getModuleName());
- $this->assertTrue($request->isDispatched());
- }
- public function testPostDispatchDoesNothingWithExistingForwardRequest()
- {
- $plugin = new Zend_Controller_Plugin_ActionStack();
- $request = new Zend_Controller_Request_Simple();
- $request->setDispatched(false);
- $plugin->setRequest($request);
- $request1 = new Zend_Controller_Request_Simple();
- $request2 = new Zend_Controller_Request_Simple();
- $request3 = new Zend_Controller_Request_Simple();
- $plugin->pushStack($request1)
- ->pushStack($request2)
- ->pushStack($request3);
- $plugin->postDispatch($request);
- $stack = $plugin->getStack();
- $this->assertEquals(3, count($stack));
- }
- }
- class Zend_Controller_Plugin_ActionStack_Registry extends Zend_Registry
- {
- protected static $_registryClassName = 'Zend_Controller_Plugin_ActionStack_Registry';
- }
- // Call Zend_Controller_Plugin_ActionStackTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Controller_Plugin_ActionStackTest::main") {
- Zend_Controller_Plugin_ActionStackTest::main();
- }
|