HeadStyleTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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_View
  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_View_Helper_HeadStyleTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadStyleTest::main");
  25. }
  26. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. /** Zend_View_Helper_HeadStyle */
  30. require_once 'Zend/View/Helper/HeadStyle.php';
  31. /** Zend_View_Helper_Placeholder_Registry */
  32. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  33. /** Zend_Registry */
  34. require_once 'Zend/Registry.php';
  35. /**
  36. * Test class for Zend_View_Helper_HeadStyle.
  37. *
  38. * @category Zend
  39. * @package Zend_View
  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_View
  44. * @group Zend_View_Helper
  45. */
  46. class Zend_View_Helper_HeadStyleTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * @var Zend_View_Helper_HeadStyle
  50. */
  51. public $helper;
  52. /**
  53. * @var string
  54. */
  55. public $basePath;
  56. /**
  57. * Runs the test methods of this class.
  58. *
  59. * @return void
  60. */
  61. public static function main()
  62. {
  63. require_once "PHPUnit/TextUI/TestRunner.php";
  64. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadStyleTest");
  65. $result = PHPUnit_TextUI_TestRunner::run($suite);
  66. }
  67. /**
  68. * Sets up the fixture, for example, open a network connection.
  69. * This method is called before a test is executed.
  70. *
  71. * @return void
  72. */
  73. public function setUp()
  74. {
  75. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  76. if (Zend_Registry::isRegistered($regKey)) {
  77. $registry = Zend_Registry::getInstance();
  78. unset($registry[$regKey]);
  79. }
  80. $this->basePath = dirname(__FILE__) . '/_files/modules';
  81. $this->helper = new Zend_View_Helper_HeadStyle();
  82. }
  83. /**
  84. * Tears down the fixture, for example, close a network connection.
  85. * This method is called after a test is executed.
  86. *
  87. * @return void
  88. */
  89. public function tearDown()
  90. {
  91. unset($this->helper);
  92. }
  93. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  94. {
  95. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  96. if ($registry->containerExists('Zend_View_Helper_HeadStyle')) {
  97. $registry->deleteContainer('Zend_View_Helper_HeadStyle');
  98. }
  99. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadStyle'));
  100. $helper = new Zend_View_Helper_HeadStyle();
  101. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadStyle'));
  102. }
  103. public function testHeadStyleReturnsObjectInstance()
  104. {
  105. $placeholder = $this->helper->headStyle();
  106. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadStyle);
  107. }
  108. public function testAppendPrependAndSetThrowExceptionsWhenNonStyleValueProvided()
  109. {
  110. try {
  111. $this->helper->append('foo');
  112. $this->fail('Non-style value should not append');
  113. } catch (Zend_View_Exception $e) { }
  114. try {
  115. $this->helper->offsetSet(5, 'foo');
  116. $this->fail('Non-style value should not offsetSet');
  117. } catch (Zend_View_Exception $e) { }
  118. try {
  119. $this->helper->prepend('foo');
  120. $this->fail('Non-style value should not prepend');
  121. } catch (Zend_View_Exception $e) { }
  122. try {
  123. $this->helper->set('foo');
  124. $this->fail('Non-style value should not set');
  125. } catch (Zend_View_Exception $e) { }
  126. }
  127. public function testOverloadAppendStyleAppendsStyleToStack()
  128. {
  129. $string = 'a {}';
  130. for ($i = 0; $i < 3; ++$i) {
  131. $string .= PHP_EOL . 'a {}';
  132. $this->helper->appendStyle($string);
  133. $values = $this->helper->getArrayCopy();
  134. $this->assertEquals($i + 1, count($values));
  135. $item = $values[$i];
  136. $this->assertTrue($item instanceof stdClass);
  137. $this->assertObjectHasAttribute('content', $item);
  138. $this->assertObjectHasAttribute('attributes', $item);
  139. $this->assertEquals($string, $item->content);
  140. }
  141. }
  142. public function testOverloadPrependStylePrependsStyleToStack()
  143. {
  144. $string = 'a {}';
  145. for ($i = 0; $i < 3; ++$i) {
  146. $string .= PHP_EOL . 'a {}';
  147. $this->helper->prependStyle($string);
  148. $values = $this->helper->getArrayCopy();
  149. $this->assertEquals($i + 1, count($values));
  150. $item = array_shift($values);
  151. $this->assertTrue($item instanceof stdClass);
  152. $this->assertObjectHasAttribute('content', $item);
  153. $this->assertObjectHasAttribute('attributes', $item);
  154. $this->assertEquals($string, $item->content);
  155. }
  156. }
  157. public function testOverloadSetOversitesStack()
  158. {
  159. $string = 'a {}';
  160. for ($i = 0; $i < 3; ++$i) {
  161. $this->helper->appendStyle($string);
  162. $string .= PHP_EOL . 'a {}';
  163. }
  164. $this->helper->setStyle($string);
  165. $values = $this->helper->getArrayCopy();
  166. $this->assertEquals(1, count($values));
  167. $item = array_shift($values);
  168. $this->assertTrue($item instanceof stdClass);
  169. $this->assertObjectHasAttribute('content', $item);
  170. $this->assertObjectHasAttribute('attributes', $item);
  171. $this->assertEquals($string, $item->content);
  172. }
  173. public function testCanBuildStyleTagsWithAttributes()
  174. {
  175. $this->helper->setStyle('a {}', array(
  176. 'lang' => 'us_en',
  177. 'title' => 'foo',
  178. 'media' => 'projection',
  179. 'dir' => 'rtol',
  180. 'bogus' => 'unused'
  181. ));
  182. $value = $this->helper->getValue();
  183. $this->assertObjectHasAttribute('attributes', $value);
  184. $attributes = $value->attributes;
  185. $this->assertTrue(isset($attributes['lang']));
  186. $this->assertTrue(isset($attributes['title']));
  187. $this->assertTrue(isset($attributes['media']));
  188. $this->assertTrue(isset($attributes['dir']));
  189. $this->assertTrue(isset($attributes['bogus']));
  190. $this->assertEquals('us_en', $attributes['lang']);
  191. $this->assertEquals('foo', $attributes['title']);
  192. $this->assertEquals('projection', $attributes['media']);
  193. $this->assertEquals('rtol', $attributes['dir']);
  194. $this->assertEquals('unused', $attributes['bogus']);
  195. }
  196. public function testRenderedStyleTagsContainHtmlEscaping()
  197. {
  198. $this->helper->setStyle('a {}', array(
  199. 'lang' => 'us_en',
  200. 'title' => 'foo',
  201. 'media' => 'screen',
  202. 'dir' => 'rtol',
  203. 'bogus' => 'unused'
  204. ));
  205. $value = $this->helper->toString();
  206. $this->assertContains('<!--' . PHP_EOL, $value);
  207. $this->assertContains(PHP_EOL . '-->', $value);
  208. }
  209. public function testRenderedStyleTagsContainsDefaultMedia()
  210. {
  211. $this->helper->setStyle('a {}', array(
  212. ));
  213. $value = $this->helper->toString();
  214. $this->assertRegexp('#<style [^>]*?media="screen"#', $value, $value);
  215. }
  216. public function testHeadStyleProxiesProperly()
  217. {
  218. $style1 = 'a {}';
  219. $style2 = 'a {}' . PHP_EOL . 'h1 {}';
  220. $style3 = 'a {}' . PHP_EOL . 'h2 {}';
  221. $this->helper->headStyle($style1, 'SET')
  222. ->headStyle($style2, 'PREPEND')
  223. ->headStyle($style3, 'APPEND');
  224. $this->assertEquals(3, count($this->helper));
  225. $values = $this->helper->getArrayCopy();
  226. $this->assertTrue((strstr($values[0]->content, $style2)) ? true : false);
  227. $this->assertTrue((strstr($values[1]->content, $style1)) ? true : false);
  228. $this->assertTrue((strstr($values[2]->content, $style3)) ? true : false);
  229. }
  230. public function testToStyleGeneratesValidHtml()
  231. {
  232. $style1 = 'a {}';
  233. $style2 = 'body {}' . PHP_EOL . 'h1 {}';
  234. $style3 = 'div {}' . PHP_EOL . 'li {}';
  235. $this->helper->headStyle($style1, 'SET')
  236. ->headStyle($style2, 'PREPEND')
  237. ->headStyle($style3, 'APPEND');
  238. $html = $this->helper->toString();
  239. $doc = new DOMDocument;
  240. $dom = $doc->loadHtml($html);
  241. $this->assertTrue(($dom !== false));
  242. $styles = substr_count($html, '<style type="text/css"');
  243. $this->assertEquals(3, $styles);
  244. $styles = substr_count($html, '</style>');
  245. $this->assertEquals(3, $styles);
  246. $this->assertContains($style3, $html);
  247. $this->assertContains($style2, $html);
  248. $this->assertContains($style1, $html);
  249. }
  250. public function testCapturingCapturesToObject()
  251. {
  252. $this->helper->captureStart();
  253. echo 'foobar';
  254. $this->helper->captureEnd();
  255. $values = $this->helper->getArrayCopy();
  256. $this->assertEquals(1, count($values));
  257. $item = array_shift($values);
  258. $this->assertContains('foobar', $item->content);
  259. }
  260. public function testOverloadingOffsetSetWritesToSpecifiedIndex()
  261. {
  262. $this->helper->offsetSetStyle(100, 'foobar');
  263. $values = $this->helper->getArrayCopy();
  264. $this->assertEquals(1, count($values));
  265. $this->assertTrue(isset($values[100]));
  266. $item = $values[100];
  267. $this->assertContains('foobar', $item->content);
  268. }
  269. public function testInvalidMethodRaisesException()
  270. {
  271. try {
  272. $this->helper->bogusMethod();
  273. $this->fail('Invalid method should raise exception');
  274. } catch (Zend_View_Exception $e) { }
  275. }
  276. public function testTooFewArgumentsRaisesException()
  277. {
  278. try {
  279. $this->helper->appendStyle();
  280. $this->fail('Too few arguments should raise exception');
  281. } catch (Zend_View_Exception $e) { }
  282. }
  283. public function testIndentationIsHonored()
  284. {
  285. $this->helper->setIndent(4);
  286. $this->helper->appendStyle('
  287. a {
  288. display: none;
  289. }');
  290. $this->helper->appendStyle('
  291. h1 {
  292. font-weight: bold
  293. }');
  294. $string = $this->helper->toString();
  295. $scripts = substr_count($string, ' <style');
  296. $this->assertEquals(2, $scripts);
  297. $this->assertContains(' <!--', $string);
  298. $this->assertContains(' a {', $string);
  299. $this->assertContains(' h1 {', $string);
  300. $this->assertContains(' display', $string);
  301. $this->assertContains(' font-weight', $string);
  302. $this->assertContains(' }', $string);
  303. }
  304. public function testSerialCapturingWorks()
  305. {
  306. $this->helper->headStyle()->captureStart();
  307. echo "Captured text";
  308. $this->helper->headStyle()->captureEnd();
  309. try {
  310. $this->helper->headStyle()->captureStart();
  311. } catch (Zend_View_Exception $e) {
  312. $this->fail('Serial capturing should work');
  313. }
  314. $this->helper->headStyle()->captureEnd();
  315. }
  316. public function testNestedCapturingFails()
  317. {
  318. $this->helper->headStyle()->captureStart();
  319. echo "Captured text";
  320. try {
  321. $this->helper->headStyle()->captureStart();
  322. $this->helper->headStyle()->captureEnd();
  323. $this->fail('Nested capturing should fail');
  324. } catch (Zend_View_Exception $e) {
  325. $this->helper->headStyle()->captureEnd();
  326. $this->assertContains('Cannot nest', $e->getMessage());
  327. }
  328. $this->helper->headStyle()->captureEnd();
  329. }
  330. public function testMediaAttributeAsArray()
  331. {
  332. $this->helper->setIndent(4);
  333. $this->helper->appendStyle('
  334. a {
  335. display: none;
  336. }', array('media' => array('screen', 'projection')));
  337. $string = $this->helper->toString();
  338. $scripts = substr_count($string, ' <style');
  339. $this->assertEquals(1, $scripts);
  340. $this->assertContains(' <!--', $string);
  341. $this->assertContains(' a {', $string);
  342. $this->assertContains(' media="screen,projection"', $string);
  343. }
  344. public function testMediaAttributeAsCommaSeperatedString()
  345. {
  346. $this->helper->setIndent(4);
  347. $this->helper->appendStyle('
  348. a {
  349. display: none;
  350. }', array('media' => 'screen,projection'));
  351. $string = $this->helper->toString();
  352. $scripts = substr_count($string, ' <style');
  353. $this->assertEquals(1, $scripts);
  354. $this->assertContains(' <!--', $string);
  355. $this->assertContains(' a {', $string);
  356. $this->assertContains(' media="screen,projection"', $string);
  357. }
  358. public function testConditionalScript()
  359. {
  360. $this->helper->appendStyle('
  361. a {
  362. display: none;
  363. }', array('media' => 'screen,projection', 'conditional' => 'lt IE 7'));
  364. $test = $this->helper->toString();
  365. $this->assertContains('<!--[if lt IE 7]>', $test);
  366. }
  367. /**
  368. * @issue ZF-5435
  369. */
  370. public function testContainerMaintainsCorrectOrderOfItems()
  371. {
  372. $this->helper->offsetSetStyle(10, '
  373. a {
  374. display: none;
  375. }');
  376. $this->helper->offsetSetStyle(5, '
  377. h1 {
  378. font-weight: bold
  379. }');
  380. $test = $this->helper->toString();
  381. $expected = '<style type="text/css" media="screen">
  382. <!--
  383. h1 {
  384. font-weight: bold
  385. }
  386. -->
  387. </style>
  388. <style type="text/css" media="screen">
  389. <!--
  390. a {
  391. display: none;
  392. }
  393. -->
  394. </style>';
  395. $this->assertEquals($expected, $test);
  396. }
  397. }
  398. // Call Zend_View_Helper_HeadStyleTest::main() if this source file is executed directly.
  399. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadStyleTest::main") {
  400. Zend_View_Helper_HeadStyleTest::main();
  401. }