ActionStackTest.php 13 KB

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