HeadStyleTest.php 13 KB

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