2
0

PluginTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_Layout
  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_LayoutTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Layout_PluginTest::main");
  25. }
  26. require_once dirname(dirname(dirname(__FILE__))) . '/TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/Layout/Controller/Plugin/Layout.php';
  30. require_once 'Zend/Layout.php';
  31. require_once 'Zend/Controller/Front.php';
  32. require_once 'Zend/Controller/Action/HelperBroker.php';
  33. require_once 'Zend/Controller/Request/Simple.php';
  34. require_once 'Zend/Controller/Response/Cli.php';
  35. /**
  36. * Test class for Zend_Layout_Controller_Plugin_Layout
  37. *
  38. * @category Zend
  39. * @package Zend_Layout
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Layout
  44. */
  45. class Zend_Layout_PluginTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. require_once "PHPUnit/TextUI/TestRunner.php";
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_Layout_PluginTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. /**
  59. * Sets up the fixture, for example, open a network connection.
  60. * This method is called before a test is executed.
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. Zend_Controller_Front::getInstance()->resetInstance();
  67. Zend_Layout_PluginTest_Layout::resetMvcInstance();
  68. if (Zend_Controller_Action_HelperBroker::hasHelper('Layout')) {
  69. Zend_Controller_Action_HelperBroker::removeHelper('Layout');
  70. }
  71. if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  72. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  73. }
  74. }
  75. /**
  76. * Tears down the fixture, for example, close a network connection.
  77. * This method is called after a test is executed.
  78. *
  79. * @return void
  80. */
  81. public function tearDown()
  82. {
  83. Zend_Layout::resetMvcInstance();
  84. }
  85. public function testConstructorWithLayoutObject()
  86. {
  87. $layout = new Zend_Layout(array('mvcEnabled' => false));
  88. $plugin = new Zend_Layout_Controller_Plugin_Layout($layout);
  89. $this->assertSame($layout, $plugin->getLayout());
  90. }
  91. public function testGetLayoutReturnsNullWithNoLayoutPresent()
  92. {
  93. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  94. $this->assertNull($plugin->getLayout());
  95. }
  96. public function testLayoutAccessorsWork()
  97. {
  98. $plugin = new Zend_Layout_Controller_Plugin_Layout();
  99. $this->assertNull($plugin->getLayout());
  100. $layout = new Zend_Layout(array('mvcEnabled' => false));
  101. $plugin->setlayout($layout);
  102. $this->assertSame($layout, $plugin->getLayout());
  103. }
  104. public function testGetLayoutReturnsLayoutObjectWhenPulledFromPluginBroker()
  105. {
  106. $layout = Zend_Layout::startMvc();
  107. $front = Zend_Controller_Front::getInstance();
  108. $this->assertTrue($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'));
  109. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  110. $this->assertSame($layout, $plugin->getLayout());
  111. }
  112. public function testPostDispatchRendersLayout()
  113. {
  114. $front = Zend_Controller_Front::getInstance();
  115. $request = new Zend_Controller_Request_Simple();
  116. $response = new Zend_Controller_Response_Cli();
  117. $request->setDispatched(true);
  118. $response->setBody('Application content');
  119. $front->setRequest($request)
  120. ->setResponse($response);
  121. $layout = Zend_Layout::startMvc();
  122. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  123. ->setLayout('plugin.phtml')
  124. ->disableInflector();
  125. $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
  126. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  127. $plugin->setResponse($response);
  128. $helper->postDispatch();
  129. $plugin->postDispatch($request);
  130. $body = $response->getBody();
  131. $this->assertContains('Application content', $body, $body);
  132. $this->assertContains('Site Layout', $body, $body);
  133. }
  134. public function testPostDispatchDoesNotRenderLayoutWhenForwardDetected()
  135. {
  136. $front = Zend_Controller_Front::getInstance();
  137. $request = new Zend_Controller_Request_Simple();
  138. $response = new Zend_Controller_Response_Cli();
  139. $request->setDispatched(false);
  140. $response->setBody('Application content');
  141. $front->setRequest($request)
  142. ->setResponse($response);
  143. $layout = Zend_Layout::startMvc();
  144. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  145. ->setLayout('plugin.phtml')
  146. ->disableInflector();
  147. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  148. $plugin->setResponse($response);
  149. $plugin->postDispatch($request);
  150. $body = $response->getBody();
  151. $this->assertContains('Application content', $body);
  152. $this->assertNotContains('Site Layout', $body);
  153. }
  154. public function testPostDispatchDoesNotRenderLayoutWhenLayoutDisabled()
  155. {
  156. $front = Zend_Controller_Front::getInstance();
  157. $request = new Zend_Controller_Request_Simple();
  158. $response = new Zend_Controller_Response_Cli();
  159. $request->setDispatched(true);
  160. $response->setBody('Application content');
  161. $front->setRequest($request)
  162. ->setResponse($response);
  163. $layout = Zend_Layout::startMvc();
  164. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  165. ->setLayout('plugin.phtml')
  166. ->disableInflector()
  167. ->disableLayout();
  168. $plugin = $front->getPlugin('Zend_Layout_Controller_Plugin_Layout');
  169. $plugin->setResponse($response);
  170. $plugin->postDispatch($request);
  171. $body = $response->getBody();
  172. $this->assertContains('Application content', $body);
  173. $this->assertNotContains('Site Layout', $body);
  174. }
  175. }
  176. /**
  177. * Zend_Layout extension to allow resetting MVC instance
  178. */
  179. class Zend_Layout_PluginTest_Layout extends Zend_Layout
  180. {
  181. public static function resetMvcInstance()
  182. {
  183. self::$_mvcInstance = null;
  184. }
  185. }
  186. // Call Zend_Layout_PluginTest::main() if this source file is executed directly.
  187. if (PHPUnit_MAIN_METHOD == "Zend_Layout_PluginTest::main") {
  188. Zend_Layout_PluginTest::main();
  189. }