2
0

ActionStackTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // Call Zend_Controller_Action_Helper_ActionStackTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  5. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_ActionStackTest::main");
  6. }
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Controller/Action/Helper/ActionStack.php';
  10. require_once 'Zend/Controller/Front.php';
  11. require_once 'Zend/Controller/Request/Simple.php';
  12. /**
  13. * Test class for Zend_Controller_Action_Helper_ActionStack.
  14. */
  15. class Zend_Controller_Action_Helper_ActionStackTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var Zend_Controller_Front
  19. */
  20. public $front;
  21. /**
  22. * @var Zend_Controller_Request_Http
  23. */
  24. public $request;
  25. /**
  26. * Runs the test methods of this class.
  27. *
  28. * @access public
  29. * @static
  30. */
  31. public static function main()
  32. {
  33. require_once "PHPUnit/TextUI/TestRunner.php";
  34. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_ActionStackTest");
  35. $result = PHPUnit_TextUI_TestRunner::run($suite);
  36. }
  37. /**
  38. * Sets up the fixture, for example, open a network connection.
  39. * This method is called before a test is executed.
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. $this->front = Zend_Controller_Front::getInstance();
  46. $this->front->resetInstance();
  47. $this->request = new Zend_Controller_Request_Http();
  48. $this->front->setRequest($this->request);
  49. }
  50. /**
  51. * Tears down the fixture, for example, close a network connection.
  52. * This method is called after a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function tearDown()
  57. {
  58. }
  59. public function testConstructorInstantiatesPluginIfNotPresent()
  60. {
  61. $this->assertFalse($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  62. $helper = new Zend_Controller_Action_Helper_ActionStack();
  63. $this->assertTrue($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  64. }
  65. public function testConstructorUsesExistingPluginWhenPresent()
  66. {
  67. $plugin = new Zend_Controller_Plugin_ActionStack();
  68. $this->front->registerPlugin($plugin);
  69. $helper = new Zend_Controller_Action_Helper_ActionStack();
  70. $this->assertTrue($this->front->hasPlugin('Zend_Controller_Plugin_ActionStack'));
  71. $registered = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  72. $this->assertSame($plugin, $registered);
  73. }
  74. public function testPushStackPushesToPluginStack()
  75. {
  76. $helper = new Zend_Controller_Action_Helper_ActionStack();
  77. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  78. $request = new Zend_Controller_Request_Simple();
  79. $request->setModuleName('foo')
  80. ->setControllerName('bar')
  81. ->setActionName('baz');
  82. $helper->pushStack($request);
  83. $next = $plugin->popStack();
  84. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  85. $this->assertEquals($request->getModuleName(), $next->getModuleName());
  86. $this->assertEquals($request->getControllerName(), $next->getControllerName());
  87. $this->assertEquals($request->getActionName(), $next->getActionName());
  88. $this->assertFalse($next->isDispatched());
  89. }
  90. public function testActionToStackPushesNewRequestToPluginStack()
  91. {
  92. $helper = new Zend_Controller_Action_Helper_ActionStack();
  93. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  94. $helper->actionToStack('baz', 'bar', 'foo');
  95. $next = $plugin->popStack();
  96. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  97. $this->assertEquals('foo', $next->getModuleName());
  98. $this->assertEquals('bar', $next->getControllerName());
  99. $this->assertEquals('baz', $next->getActionName());
  100. $this->assertFalse($next->isDispatched());
  101. }
  102. public function testPassingRequestToActionToStackPushesRequestToPluginStack()
  103. {
  104. $helper = new Zend_Controller_Action_Helper_ActionStack();
  105. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  106. $request = new Zend_Controller_Request_Simple();
  107. $request->setModuleName('foo')
  108. ->setControllerName('bar')
  109. ->setActionName('baz');
  110. $helper->actionToStack($request);
  111. $next = $plugin->popStack();
  112. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  113. $this->assertEquals($request->getModuleName(), $next->getModuleName());
  114. $this->assertEquals($request->getControllerName(), $next->getControllerName());
  115. $this->assertEquals($request->getActionName(), $next->getActionName());
  116. $this->assertFalse($next->isDispatched());
  117. }
  118. public function testDirectProxiesToActionToStack()
  119. {
  120. $helper = new Zend_Controller_Action_Helper_ActionStack();
  121. /** FC should be reseted to test ActionStack with a really blank FC */
  122. $this->front->resetInstance();
  123. try{
  124. $helper->direct('baz', 'bar', 'foo');
  125. $this->fail('Zend_Controller_Action_Exception should be thrown');
  126. }catch(Zend_Exception $e){
  127. $this->assertType('Zend_Controller_Action_Exception',
  128. $e,
  129. 'Zend_Controller_Action_Exception expected, '.get_class($e).' caught');
  130. }
  131. }
  132. public function testCannotStackActionIfNoRequestAvailable()
  133. {
  134. $helper = new Zend_Controller_Action_Helper_ActionStack();
  135. $plugin = $this->front->getPlugin('Zend_Controller_Plugin_ActionStack');
  136. $helper->direct('baz', 'bar', 'foo');
  137. $next = $plugin->popStack();
  138. $this->assertTrue($next instanceof Zend_Controller_Request_Abstract);
  139. $this->assertEquals('foo', $next->getModuleName());
  140. $this->assertEquals('bar', $next->getControllerName());
  141. $this->assertEquals('baz', $next->getActionName());
  142. $this->assertFalse($next->isDispatched());
  143. }
  144. }
  145. // Call Zend_Controller_Action_Helper_ActionStackTest::main() if this source file is executed directly.
  146. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_ActionStackTest::main") {
  147. Zend_Controller_Action_Helper_ActionStackTest::main();
  148. }