ActionStackTest.php 13 KB

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