PluginTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // Call Zend_LayoutTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Layout_PluginTest::main");
  5. }
  6. require_once dirname(dirname(dirname(__FILE__))) . '/TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Layout/Controller/Plugin/Layout.php';
  10. require_once 'Zend/Layout.php';
  11. require_once 'Zend/Controller/Front.php';
  12. require_once 'Zend/Controller/Action/HelperBroker.php';
  13. require_once 'Zend/Controller/Request/Simple.php';
  14. require_once 'Zend/Controller/Response/Cli.php';
  15. /**
  16. * Test class for Zend_Layout_Controller_Plugin_Layout
  17. */
  18. class Zend_Layout_PluginTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Runs the test methods of this class.
  22. *
  23. * @return void
  24. */
  25. public static function main()
  26. {
  27. require_once "PHPUnit/TextUI/TestRunner.php";
  28. $suite = new PHPUnit_Framework_TestSuite("Zend_Layout_PluginTest");
  29. $result = PHPUnit_TextUI_TestRunner::run($suite);
  30. }
  31. /**
  32. * Sets up the fixture, for example, open a network connection.
  33. * This method is called before a test is executed.
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. Zend_Controller_Front::getInstance()->resetInstance();
  40. Zend_Layout_PluginTest_Layout::resetMvcInstance();
  41. if (Zend_Controller_Action_HelperBroker::hasHelper('Layout')) {
  42. Zend_Controller_Action_HelperBroker::removeHelper('Layout');
  43. }
  44. if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  45. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  46. }
  47. }
  48. /**
  49. * Tears down the fixture, for example, close a network connection.
  50. * This method is called after a test is executed.
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. Zend_Layout::resetMvcInstance();
  57. }
  58. public function testConstructorWithLayoutObject()
  59. {
  60. $layout = new Zend_Layout(array('mvcEnabled' => false));
  61. $plugin = new Zend_Layout_Controller_Plugin_Layout($layout);
  62. $this->assertSame($layout, $plugin->getLayout());
  63. }
  64. public function testGetLayoutReturnsNullWithNoLayoutPresent()
  65. {
  66. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  67. $this->assertNull($plugin->getLayout());
  68. }
  69. public function testLayoutAccessorsWork()
  70. {
  71. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  72. $this->assertNull($plugin->getLayout());
  73. $layout = new Zend_Layout(array('mvcEnabled' => false));
  74. $plugin->setlayout($layout);
  75. $this->assertSame($layout, $plugin->getLayout());
  76. }
  77. public function testGetLayoutReturnsLayoutObjectWhenPulledFromPluginBroker()
  78. {
  79. $layout = Zend_Layout::startMvc();
  80. $front = Zend_Controller_Front::getInstance();
  81. $this->assertTrue($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'));
  82. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  83. $this->assertSame($layout, $plugin->getLayout());
  84. }
  85. public function testPostDispatchRendersLayout()
  86. {
  87. $front = Zend_Controller_Front::getInstance();
  88. $request = new Zend_Controller_Request_Simple();
  89. $response = new Zend_Controller_Response_Cli();
  90. $request->setDispatched(true);
  91. $response->setBody('Application content');
  92. $front->setRequest($request)
  93. ->setResponse($response);
  94. $layout = Zend_Layout::startMvc();
  95. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  96. ->setLayout('plugin.phtml')
  97. ->disableInflector();
  98. $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
  99. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  100. $plugin->setResponse($response);
  101. $helper->postDispatch();
  102. $plugin->postDispatch($request);
  103. $body = $response->getBody();
  104. $this->assertContains('Application content', $body, $body);
  105. $this->assertContains('Site Layout', $body, $body);
  106. }
  107. public function testPostDispatchDoesNotRenderLayoutWhenForwardDetected()
  108. {
  109. $front = Zend_Controller_Front::getInstance();
  110. $request = new Zend_Controller_Request_Simple();
  111. $response = new Zend_Controller_Response_Cli();
  112. $request->setDispatched(false);
  113. $response->setBody('Application content');
  114. $front->setRequest($request)
  115. ->setResponse($response);
  116. $layout = Zend_Layout::startMvc();
  117. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  118. ->setLayout('plugin.phtml')
  119. ->disableInflector();
  120. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  121. $plugin->setResponse($response);
  122. $plugin->postDispatch($request);
  123. $body = $response->getBody();
  124. $this->assertContains('Application content', $body);
  125. $this->assertNotContains('Site Layout', $body);
  126. }
  127. public function testPostDispatchDoesNotRenderLayoutWhenLayoutDisabled()
  128. {
  129. $front = Zend_Controller_Front::getInstance();
  130. $request = new Zend_Controller_Request_Simple();
  131. $response = new Zend_Controller_Response_Cli();
  132. $request->setDispatched(true);
  133. $response->setBody('Application content');
  134. $front->setRequest($request)
  135. ->setResponse($response);
  136. $layout = Zend_Layout::startMvc();
  137. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  138. ->setLayout('plugin.phtml')
  139. ->disableInflector()
  140. ->disableLayout();
  141. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  142. $plugin->setResponse($response);
  143. $plugin->postDispatch($request);
  144. $body = $response->getBody();
  145. $this->assertContains('Application content', $body);
  146. $this->assertNotContains('Site Layout', $body);
  147. }
  148. }
  149. /**
  150. * Zend_Layout extension to allow resetting MVC instance
  151. */
  152. class Zend_Layout_PluginTest_Layout extends Zend_Layout
  153. {
  154. public static function resetMvcInstance()
  155. {
  156. self::$_mvcInstance = null;
  157. }
  158. }
  159. // Call Zend_Layout_PluginTest::main() if this source file is executed directly.
  160. if (PHPUnit_MAIN_METHOD == "Zend_Layout_PluginTest::main") {
  161. Zend_Layout_PluginTest::main();
  162. }