ActionStackTest.php 7.3 KB

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