PluginTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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::$_mvcInstance = null;
  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. }
  57. public function testConstructorWithLayoutObject()
  58. {
  59. $layout = new Zend_Layout(array('mvcEnabled' => false));
  60. $plugin = new Zend_Layout_Controller_Plugin_Layout($layout);
  61. $this->assertSame($layout, $plugin->getLayout());
  62. }
  63. public function testGetLayoutReturnsNullWithNoLayoutPresent()
  64. {
  65. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  66. $this->assertNull($plugin->getLayout());
  67. }
  68. public function testLayoutAccessorsWork()
  69. {
  70. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  71. $this->assertNull($plugin->getLayout());
  72. $layout = new Zend_Layout(array('mvcEnabled' => false));
  73. $plugin->setlayout($layout);
  74. $this->assertSame($layout, $plugin->getLayout());
  75. }
  76. public function testGetLayoutReturnsLayoutObjectWhenPulledFromPluginBroker()
  77. {
  78. $layout = Zend_Layout::startMvc();
  79. $front = Zend_Controller_Front::getInstance();
  80. $this->assertTrue($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'));
  81. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  82. $this->assertSame($layout, $plugin->getLayout());
  83. }
  84. public function testPostDispatchRendersLayout()
  85. {
  86. $front = Zend_Controller_Front::getInstance();
  87. $request = new Zend_Controller_Request_Simple();
  88. $response = new Zend_Controller_Response_Cli();
  89. $request->setDispatched(true);
  90. $response->setBody('Application content');
  91. $front->setRequest($request)
  92. ->setResponse($response);
  93. $layout = Zend_Layout::startMvc();
  94. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  95. ->setLayout('plugin.phtml')
  96. ->disableInflector();
  97. $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
  98. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  99. $plugin->setResponse($response);
  100. $helper->postDispatch();
  101. $plugin->postDispatch($request);
  102. $body = $response->getBody();
  103. $this->assertContains('Application content', $body, $body);
  104. $this->assertContains('Site Layout', $body, $body);
  105. }
  106. public function testPostDispatchDoesNotRenderLayoutWhenForwardDetected()
  107. {
  108. $front = Zend_Controller_Front::getInstance();
  109. $request = new Zend_Controller_Request_Simple();
  110. $response = new Zend_Controller_Response_Cli();
  111. $request->setDispatched(false);
  112. $response->setBody('Application content');
  113. $front->setRequest($request)
  114. ->setResponse($response);
  115. $layout = Zend_Layout::startMvc();
  116. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  117. ->setLayout('plugin.phtml')
  118. ->disableInflector();
  119. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  120. $plugin->setResponse($response);
  121. $plugin->postDispatch($request);
  122. $body = $response->getBody();
  123. $this->assertContains('Application content', $body);
  124. $this->assertNotContains('Site Layout', $body);
  125. }
  126. public function testPostDispatchDoesNotRenderLayoutWhenLayoutDisabled()
  127. {
  128. $front = Zend_Controller_Front::getInstance();
  129. $request = new Zend_Controller_Request_Simple();
  130. $response = new Zend_Controller_Response_Cli();
  131. $request->setDispatched(true);
  132. $response->setBody('Application content');
  133. $front->setRequest($request)
  134. ->setResponse($response);
  135. $layout = Zend_Layout::startMvc();
  136. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  137. ->setLayout('plugin.phtml')
  138. ->disableInflector()
  139. ->disableLayout();
  140. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  141. $plugin->setResponse($response);
  142. $plugin->postDispatch($request);
  143. $body = $response->getBody();
  144. $this->assertContains('Application content', $body);
  145. $this->assertNotContains('Site Layout', $body);
  146. }
  147. }
  148. /**
  149. * Zend_Layout extension to allow resetting MVC instance
  150. */
  151. class Zend_Layout_PluginTest_Layout extends Zend_Layout
  152. {
  153. public static $_mvcInstance;
  154. }
  155. // Call Zend_Layout_PluginTest::main() if this source file is executed directly.
  156. if (PHPUnit_MAIN_METHOD == "Zend_Layout_PluginTest::main") {
  157. Zend_Layout_PluginTest::main();
  158. }