ViewTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_Application
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_ViewTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. require_once 'Zend/Application/Resource/View.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Application
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Application
  37. */
  38. class Zend_Application_Resource_ViewTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  43. $result = PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function setUp()
  46. {
  47. // Store original autoloaders
  48. $this->loaders = spl_autoload_functions();
  49. if (!is_array($this->loaders)) {
  50. // spl_autoload_functions does not return empty array when no
  51. // autoloaders registered...
  52. $this->loaders = array();
  53. }
  54. Zend_Loader_Autoloader::resetInstance();
  55. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  56. $this->application = new Zend_Application('testing');
  57. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  58. $this->bootstrap = new ZfAppBootstrap($this->application);
  59. Zend_Controller_Action_HelperBroker::resetHelpers();
  60. }
  61. public function tearDown()
  62. {
  63. // Restore original autoloaders
  64. $loaders = spl_autoload_functions();
  65. foreach ($loaders as $loader) {
  66. spl_autoload_unregister($loader);
  67. }
  68. foreach ($this->loaders as $loader) {
  69. spl_autoload_register($loader);
  70. }
  71. // Reset autoloader instance so it doesn't affect other tests
  72. Zend_Loader_Autoloader::resetInstance();
  73. }
  74. public function testInitializationInitializesViewObject()
  75. {
  76. $resource = new Zend_Application_Resource_View(array());
  77. $resource->init();
  78. $this->assertTrue($resource->getView() instanceof Zend_View);
  79. }
  80. public function testInitializationInjectsViewIntoViewRenderer()
  81. {
  82. $resource = new Zend_Application_Resource_View(array());
  83. $resource->init();
  84. $view = $resource->getView();
  85. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  86. $this->assertSame($view, $viewRenderer->view);
  87. }
  88. public function testOptionsPassedToResourceAreUsedToSetViewState()
  89. {
  90. $options = array(
  91. 'scriptPath' => dirname(__FILE__),
  92. );
  93. require_once 'Zend/Application/Resource/View.php';
  94. $resource = new Zend_Application_Resource_View($options);
  95. $resource->init();
  96. $view = $resource->getView();
  97. $paths = $view->getScriptPaths();
  98. $this->assertContains(dirname(__FILE__) . '/', $paths, var_export($paths, 1));
  99. }
  100. public function testDoctypeIsSet()
  101. {
  102. $options = array('doctype' => 'XHTML1_FRAMESET');
  103. $resource = new Zend_Application_Resource_View($options);
  104. $resource->init();
  105. $view = $resource->getView();
  106. $this->assertEquals('XHTML1_FRAMESET', $view->doctype()->getDoctype());
  107. }
  108. /**
  109. * @group ZF-10343
  110. */
  111. public function testContentTypeIsSet()
  112. {
  113. $contentType = 'text/html; charset=UTF-8';
  114. $options = array('contentType' => $contentType);
  115. $resource = new Zend_Application_Resource_View($options);
  116. $headMetaHelper = $resource->init()->headMeta();
  117. $actual = null;
  118. $container = $headMetaHelper->getContainer();
  119. foreach ($container as $item) {
  120. if ('Content-Type' == $item->{$item->type}) {
  121. $actual = $item->content;
  122. break;
  123. }
  124. }
  125. $this->assertEquals($contentType, $actual);
  126. Zend_View_Helper_Placeholder_Registry::getRegistry()
  127. ->deleteContainer('Zend_View_Helper_HeadMeta');
  128. }
  129. /**
  130. * @group ZF-10343
  131. */
  132. public function testSetMetaCharsetForHtml5()
  133. {
  134. $charset = 'UTF-8';
  135. $options = array(
  136. 'doctype' => 'HTML5',
  137. 'charset' => $charset,
  138. );
  139. $resource = new Zend_Application_Resource_View($options);
  140. $view = $resource->init();
  141. $headMetaHelper = $view->headMeta();
  142. $actual = null;
  143. $container = $headMetaHelper->getContainer();
  144. foreach ($container as $item) {
  145. if ('charset' == $item->type) {
  146. $actual = $item->charset;
  147. break;
  148. }
  149. }
  150. $this->assertTrue($view->doctype()->isHtml5());
  151. $this->assertEquals($charset, $actual);
  152. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  153. $registry->deleteContainer('Zend_View_Helper_HeadMeta');
  154. $registry->deleteContainer('Zend_View_Helper_Doctype');
  155. }
  156. /**
  157. * @group ZF-10343
  158. */
  159. public function testSetMetaCharsetShouldOnlyAvailableForHtml5()
  160. {
  161. $charset = 'UTF-8';
  162. $options = array(
  163. 'doctype' => 'XHTML1_STRICT',
  164. 'charset' => $charset,
  165. );
  166. $resource = new Zend_Application_Resource_View($options);
  167. $view = $resource->init();
  168. $headMetaHelper = $view->headMeta();
  169. $actual = null;
  170. $container = $headMetaHelper->getContainer();
  171. foreach ($container as $item) {
  172. if ('charset' == $item->type) {
  173. $actual = $item->charset;
  174. break;
  175. }
  176. }
  177. $this->assertFalse($view->doctype()->isHtml5());
  178. $this->assertNull($actual);
  179. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  180. $registry->deleteContainer('Zend_View_Helper_HeadMeta');
  181. $registry->deleteContainer('Zend_View_Helper_Doctype');
  182. }
  183. /**
  184. * @group ZF-10042
  185. */
  186. public function testAssignmentsAreSet()
  187. {
  188. $options = array(
  189. 'assign' => array(
  190. 'foo' => 'barbapapa',
  191. 'bar' => 'barbazoo',
  192. )
  193. );
  194. $resource = new Zend_Application_Resource_View($options);
  195. $view = $resource->init();
  196. $this->assertEquals('barbapapa', $view->foo);
  197. $this->assertEquals('barbazoo', $view->bar);
  198. }
  199. /**
  200. * @group ZF-11579
  201. */
  202. public function testViewResourceDoesNotReinjectViewRenderer()
  203. {
  204. require_once dirname(__FILE__) . '/TestAsset/ViewRenderer.php';
  205. $viewRenderer = new Zend_Application_Resource_TestAsset_ViewRenderer();
  206. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  207. $resource = new Zend_Application_Resource_View(array('encoding' => 'UTF-8'));
  208. $view = $resource->init();
  209. $this->assertSame($view, $viewRenderer->view);
  210. }
  211. }
  212. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_ViewTest::main') {
  213. Zend_Application_Resource_ViewTest::main();
  214. }