ActionStackTest.php 7.5 KB

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