2
0

ViewTest.php 6.8 KB

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