ActionStackTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-2012 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_Action_Helper_ActionStackTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_ActionStackTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/Helper/ActionStack.php';
  27. require_once 'Zend/Controller/Front.php';
  28. require_once 'Zend/Controller/Request/Simple.php';
  29. /**
  30. * Test class for Zend_Controller_Action_Helper_ActionStack.
  31. *
  32. * @category Zend
  33. * @package Zend_Controller
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2012 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_Action
  39. * @group Zend_Controller_Action_Helper
  40. */
  41. class Zend_Controller_Action_Helper_ActionStackTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * @var Zend_Controller_Front
  45. */
  46. public $front;
  47. /**
  48. * @var Zend_Controller_Request_Http
  49. */
  50. public $request;
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @access public
  55. * @static
  56. */
  57. public static function main()
  58. {
  59. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_ActionStackTest");
  60. $result = PHPUnit_TextUI_TestRunner::run($suite);
  61. }
  62. /**
  63. * Sets up the fixture, for example, open a network connection.
  64. * This method is called before a test is executed.
  65. *
  66. * @return void
  67. */
  68. public function setUp()
  69. {
  70. $this->front = Zend_Controller_Front::getInstance();
  71. $this->front->resetInstance();
  72. $this->request = new Zend_Controller_Request_Http();
  73. $this->front->setRequest($this->request);
  74. }
  75. /**
  76. * Tears down the fixture, for example, close a network connection.
  77. * This method is called after a test is executed.
  78. *
  79. * @return void
  80. */
  81. public function tearDown()
  82. {
  83. }
  84. public function testConstructorInstantiatesPluginIfNotPresent()
  85. {
  86. $this->assertFalse($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  87. $helper = new Zend_Controller_Action_Helper_ActionStack();
  88. $this->assertTrue($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  89. }
  90. public function testConstructorUsesExistingPluginWhenPresent()
  91. {
  92. $plugin = new Zend_Controller_Plugin_ActionStack();
  93. $this->front->registerPlugin($plugin);
  94. $helper = new Zend_Controller_Action_Helper_ActionStack();
  95. $this->assertTrue($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  96. $registered = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  97. $this->assertSame($plugin, $registered);
  98. }
  99. public function testPushStackPushesToPluginStack()
  100. {
  101. $helper = new Zend_Controller_Action_Helper_ActionStack();
  102. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  103. $request = new Zend_Controller_Request_Simple();
  104. $request->setModuleName('foo')
  105. ->setControllerName('bar')
  106. ->setActionName('baz');
  107. $helper->pushStack($request);
  108. $next = $plugin->popStack();
  109. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  110. $this->assertEquals($request->getModuleName(), $next->getModuleName());
  111. $this->assertEquals($request->getControllerName(), $next->getControllerName());
  112. $this->assertEquals($request->getActionName(), $next->getActionName());
  113. $this->assertFalse($next->isDispatched());
  114. }
  115. public function testActionToStackPushesNewRequestToPluginStack()
  116. {
  117. $helper = new Zend_Controller_Action_Helper_ActionStack();
  118. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  119. $helper->actionToStack('baz', 'bar', 'foo');
  120. $next = $plugin->popStack();
  121. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  122. $this->assertEquals('foo', $next->getModuleName());
  123. $this->assertEquals('bar', $next->getControllerName());
  124. $this->assertEquals('baz', $next->getActionName());
  125. $this->assertFalse($next->isDispatched());
  126. }
  127. public function testPassingRequestToActionToStackPushesRequestToPluginStack()
  128. {
  129. $helper = new Zend_Controller_Action_Helper_ActionStack();
  130. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  131. $request = new Zend_Controller_Request_Simple();
  132. $request->setModuleName('foo')
  133. ->setControllerName('bar')
  134. ->setActionName('baz');
  135. $helper->actionToStack($request);
  136. $next = $plugin->popStack();
  137. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  138. $this->assertEquals($request->getModuleName(), $next->getModuleName());
  139. $this->assertEquals($request->getControllerName(), $next->getControllerName());
  140. $this->assertEquals($request->getActionName(), $next->getActionName());
  141. $this->assertFalse($next->isDispatched());
  142. }
  143. public function testDirectProxiesToActionToStack()
  144. {
  145. $helper = new Zend_Controller_Action_Helper_ActionStack();
  146. /** FC should be reseted to test ActionStack with a really blank FC */
  147. $this->front->resetInstance();
  148. try {
  149. $helper->direct('baz', 'bar', 'foo');
  150. $this->fail('Zend_Controller_Action_Exception should be thrown');
  151. } catch (Zend_Exception $e) {
  152. $this->assertTrue(
  153. $e instanceof Zend_Controller_Action_Exception,
  154. 'Zend_Controller_Action_Exception expected, ' . get_class($e)
  155. . ' caught'
  156. );
  157. }
  158. }
  159. public function testCannotStackActionIfNoRequestAvailable()
  160. {
  161. $helper = new Zend_Controller_Action_Helper_ActionStack();
  162. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  163. $helper->direct('baz', 'bar', 'foo');
  164. $next = $plugin->popStack();
  165. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  166. $this->assertEquals('foo', $next->getModuleName());
  167. $this->assertEquals('bar', $next->getControllerName());
  168. $this->assertEquals('baz', $next->getActionName());
  169. $this->assertFalse($next->isDispatched());
  170. }
  171. }
  172. // Call Zend_Controller_Action_Helper_ActionStackTest::main() if this source file is executed directly.
  173. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_ActionStackTest::main") {
  174. Zend_Controller_Action_Helper_ActionStackTest::main();
  175. }