ActionStackTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Controller_Plugin_ActionStackTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  25. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Plugin_ActionStackTest::main");
  26. }
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/Controller/Plugin/ActionStack.php';
  30. require_once 'Zend/Controller/Request/Simple.php';
  31. require_once 'Zend/Registry.php';
  32. /**
  33. * Test class for Zend_Controller_Plugin_ActionStack.
  34. *
  35. * @category Zend
  36. * @package Zend_Controller
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Controller
  41. * @group Zend_Controller_Plugin
  42. */
  43. class Zend_Controller_Plugin_ActionStackTest extends PHPUnit_Framework_TestCase
  44. {
  45. public $key = 'Zend_Controller_Plugin_ActionStack';
  46. public $registry;
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @access public
  51. * @static
  52. */
  53. public static function main()
  54. {
  55. require_once "PHPUnit/TextUI/TestRunner.php";
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Plugin_ActionStackTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. /**
  60. * Sets up the fixture, for example, open a network connection.
  61. * This method is called before a test is executed.
  62. *
  63. * @return void
  64. */
  65. public function setUp()
  66. {
  67. $this->removeRegistryEntry();
  68. $this->registry = Zend_Registry::getInstance();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. protected function tearDown()
  77. {
  78. $this->removeRegistryEntry();
  79. }
  80. /**
  81. * Ensure registry is clean
  82. *
  83. * @return void
  84. */
  85. public function removeRegistryEntry()
  86. {
  87. $registry = Zend_Registry::getInstance();
  88. if (isset($registry[$this->key])) {
  89. unset($registry[$this->key]);
  90. }
  91. }
  92. public function testConstructorCreatesRegistryEntry()
  93. {
  94. $registry = Zend_Registry::getInstance();
  95. $this->assertFalse(isset($registry[$this->key]));
  96. $plugin = new Zend_Controller_Plugin_ActionStack();
  97. $key = $plugin->getRegistryKey();
  98. $this->assertTrue(isset($registry[$key]));
  99. }
  100. public function testKeyPassedToConstructorUsedAsRegistryKey()
  101. {
  102. $this->key = $key = 'foobar';
  103. $registry = Zend_Registry::getInstance();
  104. $this->assertFalse(isset($registry[$key]));
  105. $plugin = new Zend_Controller_Plugin_ActionStack(null, $key);
  106. $this->assertTrue(isset($registry[$key]));
  107. }
  108. public function testRegistryPassedToConstructorUsedByPlugin()
  109. {
  110. $registry = new Zend_Controller_Plugin_ActionStack_Registry();
  111. $plugin = new Zend_Controller_Plugin_ActionStack($registry);
  112. $registered = $plugin->getRegistry();
  113. $this->assertNotSame($this->registry, $registered);
  114. $this->assertSame($registry, $registered);
  115. }
  116. /**
  117. * @return void
  118. */
  119. public function testRegistryAccessorsWork()
  120. {
  121. $registry = new Zend_Controller_Plugin_ActionStack_Registry();
  122. $plugin = new Zend_Controller_Plugin_ActionStack();
  123. $original = $plugin->getRegistry();
  124. $plugin->setRegistry($registry);
  125. $registered = $plugin->getRegistry();
  126. $this->assertSame($registry, $registered);
  127. $this->assertNotSame($original, $registered);
  128. }
  129. public function testRegistryKeyHasDefaultValue()
  130. {
  131. $plugin = new Zend_Controller_Plugin_ActionStack();
  132. $key = $plugin->getRegistryKey();
  133. $this->assertNotNull($key);
  134. $this->assertEquals($this->key, $key);
  135. }
  136. /**
  137. * @return void
  138. */
  139. public function testRegistryKeyAccessorsWork()
  140. {
  141. $plugin = new Zend_Controller_Plugin_ActionStack();
  142. $plugin->setRegistryKey('foobar');
  143. $key = $plugin->getRegistryKey();
  144. $this->assertEquals('foobar', $key);
  145. }
  146. /**
  147. * @return void
  148. */
  149. public function testGetStackInitiallyReturnsEmptyArray()
  150. {
  151. $plugin = new Zend_Controller_Plugin_ActionStack();
  152. $stack = $plugin->getStack();
  153. $this->assertTrue(is_array($stack));
  154. $this->assertTrue(empty($stack));
  155. }
  156. /**
  157. * @return void
  158. */
  159. public function testPushStackAppendsToStack()
  160. {
  161. $plugin = new Zend_Controller_Plugin_ActionStack();
  162. $request1 = new Zend_Controller_Request_Simple();
  163. $plugin->pushStack($request1);
  164. $received = $plugin->getStack();
  165. $this->assertTrue(is_array($received));
  166. $this->assertEquals(1, count($received));
  167. $this->assertSame($request1, $received[0]);
  168. $request2 = new Zend_Controller_Request_Simple();
  169. $plugin->pushStack($request2);
  170. $received = $plugin->getStack();
  171. $this->assertTrue(is_array($received));
  172. $this->assertEquals(2, count($received));
  173. $this->assertSame($request2, $received[1]);
  174. $this->assertSame($request1, $received[0]);
  175. }
  176. public function getNewRequest()
  177. {
  178. $request = new Zend_Controller_Request_Simple();
  179. $request->setActionName('baz')
  180. ->setControllerName('bar')
  181. ->setModuleName('foo');
  182. return $request;
  183. }
  184. /**
  185. * @return void
  186. */
  187. public function testPopStackPullsFromEndOfStack()
  188. {
  189. $plugin = new Zend_Controller_Plugin_ActionStack();
  190. $request1 = $this->getNewRequest();
  191. $request2 = $this->getNewRequest();
  192. $request3 = $this->getNewRequest();
  193. $plugin->pushStack($request1)
  194. ->pushStack($request2)
  195. ->pushStack($request3);
  196. $stack = $plugin->getStack();
  197. $this->assertEquals(3, count($stack));
  198. $received = $plugin->popStack();
  199. $stack = $plugin->getStack();
  200. $this->assertSame($request3, $received);
  201. $this->assertEquals(2, count($stack));
  202. }
  203. public function testPopEmptyStackReturnsFalse()
  204. {
  205. $plugin = new Zend_Controller_Plugin_ActionStack();
  206. $received = $plugin->popStack();
  207. $this->assertFalse($received);
  208. }
  209. public function testPopStackPopsMultipleItemsWhenRequestActionEmpty()
  210. {
  211. $plugin = new Zend_Controller_Plugin_ActionStack();
  212. $request1 = $this->getNewRequest();
  213. $request2 = new Zend_Controller_Request_Simple();
  214. $plugin->pushStack($request1)
  215. ->pushStack($request2);
  216. $stack = $plugin->getStack();
  217. $this->assertEquals(2, count($stack));
  218. $received = $plugin->popStack();
  219. $stack = $plugin->getStack();
  220. $this->assertSame($request1, $received);
  221. $this->assertEquals(0, count($stack));
  222. }
  223. public function testPopStackPopulatesControllerAndModuleFromRequestIfEmpty()
  224. {
  225. $plugin = new Zend_Controller_Plugin_ActionStack();
  226. $request = $this->getNewRequest();
  227. $plugin->setRequest($request);
  228. $request1 = new Zend_Controller_Request_Simple();
  229. $request1->setActionName('blah');
  230. $plugin->pushStack($request1);
  231. $next = $plugin->popStack();
  232. $this->assertTrue($next instanceof Zend_Controller_Request_Simple);
  233. $this->assertEquals($request1->getActionName(), $next->getActionName());
  234. $this->assertEquals($request->getControllerName(), $next->getControllerName());
  235. $this->assertEquals($request->getModuleName(), $next->getModuleName());
  236. }
  237. public function testForwardResetsInternalRequestStateFromGivenRequest()
  238. {
  239. $plugin = new Zend_Controller_Plugin_ActionStack();
  240. $request = new Zend_Controller_Request_Simple();
  241. $plugin->setRequest($request);
  242. $next = $this->getNewRequest();
  243. $plugin->forward($next);
  244. $this->assertEquals($next->getActionName(), $request->getActionName());
  245. $this->assertEquals($next->getControllerName(), $request->getControllerName());
  246. $this->assertEquals($next->getModuleName(), $request->getModuleName());
  247. $this->assertFalse($request->isDispatched());
  248. }
  249. public function testForwardResetsRequestParamsIfFlagSet()
  250. {
  251. $plugin = new Zend_Controller_Plugin_ActionStack();
  252. $request = $this->getNewRequest();
  253. $params = array('foo' => 'bar','baz'=>'bat');
  254. $request->setParams($params);
  255. $plugin->setRequest($request);
  256. $this->assertEquals($params,$plugin->getRequest()->getParams());
  257. $next = $this->getNewRequest();
  258. $plugin->forward($next);
  259. $this->assertEquals($params,$plugin->getRequest()->getParams());
  260. $plugin->setClearRequestParams(true);
  261. $next = $this->getNewRequest();
  262. $plugin->forward($next);
  263. $this->assertEquals(array(),$plugin->getRequest()->getParams());
  264. }
  265. /**
  266. * @return void
  267. */
  268. public function testPostDispatchResetsInternalRequestFromLastRequestOnStack()
  269. {
  270. $plugin = new Zend_Controller_Plugin_ActionStack();
  271. $request = new Zend_Controller_Request_Simple();
  272. $request->setDispatched(true);
  273. $plugin->setRequest($request);
  274. $request1 = $this->getNewRequest();
  275. $request2 = $this->getNewRequest();
  276. $request3 = $this->getNewRequest();
  277. $request3->setActionName('foobar')
  278. ->setControllerName('bazbat')
  279. ->setModuleName('bogus');
  280. $plugin->pushStack($request1)
  281. ->pushStack($request2)
  282. ->pushStack($request3);
  283. $plugin->postDispatch($request);
  284. $this->assertEquals($request3->getActionName(), $request->getActionName());
  285. $this->assertEquals($request3->getControllerName(), $request->getControllerName());
  286. $this->assertEquals($request3->getModuleName(), $request->getModuleName());
  287. $this->assertFalse($request->isDispatched());
  288. }
  289. public function testPostDispatchDoesNothingWithEmptyStack()
  290. {
  291. $plugin = new Zend_Controller_Plugin_ActionStack();
  292. $request = $this->getNewRequest();
  293. $request->setDispatched(true);
  294. $clone = clone $request;
  295. $plugin->postDispatch($request);
  296. $this->assertEquals($clone->getActionName(), $request->getActionName());
  297. $this->assertEquals($clone->getControllerName(), $request->getControllerName());
  298. $this->assertEquals($clone->getModuleName(), $request->getModuleName());
  299. $this->assertTrue($request->isDispatched());
  300. }
  301. public function testPostDispatchDoesNothingWithStackThatEvaluatesToEmpty()
  302. {
  303. $plugin = new Zend_Controller_Plugin_ActionStack();
  304. $request = new Zend_Controller_Request_Simple();
  305. $request->setDispatched(true);
  306. $plugin->setRequest($request);
  307. $request1 = new Zend_Controller_Request_Simple();
  308. $request2 = new Zend_Controller_Request_Simple();
  309. $request3 = new Zend_Controller_Request_Simple();
  310. $plugin->pushStack($request1)
  311. ->pushStack($request2)
  312. ->pushStack($request3);
  313. $clone = clone $request;
  314. $plugin->postDispatch($request);
  315. $this->assertEquals($clone->getActionName(), $request->getActionName());
  316. $this->assertEquals($clone->getControllerName(), $request->getControllerName());
  317. $this->assertEquals($clone->getModuleName(), $request->getModuleName());
  318. $this->assertTrue($request->isDispatched());
  319. }
  320. public function testPostDispatchDoesNothingWithExistingForwardRequest()
  321. {
  322. $plugin = new Zend_Controller_Plugin_ActionStack();
  323. $request = new Zend_Controller_Request_Simple();
  324. $request->setDispatched(false);
  325. $plugin->setRequest($request);
  326. $request1 = new Zend_Controller_Request_Simple();
  327. $request2 = new Zend_Controller_Request_Simple();
  328. $request3 = new Zend_Controller_Request_Simple();
  329. $plugin->pushStack($request1)
  330. ->pushStack($request2)
  331. ->pushStack($request3);
  332. $plugin->postDispatch($request);
  333. $stack = $plugin->getStack();
  334. $this->assertEquals(3, count($stack));
  335. }
  336. }
  337. class Zend_Controller_Plugin_ActionStack_Registry extends Zend_Registry
  338. {
  339. protected static $_registryClassName = 'Zend_Controller_Plugin_ActionStack_Registry';
  340. }
  341. // Call Zend_Controller_Plugin_ActionStackTest::main() if this source file is executed directly.
  342. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Plugin_ActionStackTest::main") {
  343. Zend_Controller_Plugin_ActionStackTest::main();
  344. }