PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/FluentDOM/StyleTest.php

http://github.com/ThomasWeinert/FluentDOM
PHP | 348 lines | 204 code | 29 blank | 115 comment | 3 complexity | 53f1ac9c21ffabeb3cee307894c1134d MD5 | raw file
  1. <?php
  2. /**
  3. * Collection of test for the FluentDOMStyle class supporting PHP 5.2
  4. *
  5. * @version $Id$
  6. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  7. * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
  8. *
  9. * @package FluentDOM
  10. * @subpackage Tests
  11. */
  12. /**
  13. * load necessary files
  14. */
  15. require_once (dirname(__FILE__).'/../FluentDOMTestCase.php');
  16. require_once(dirname(__FILE__).'/../../src/FluentDOM/Style.php');
  17. /**
  18. * Test class for FluentDOMStyle.
  19. *
  20. * @package FluentDOM
  21. * @subpackage Tests
  22. */
  23. class FluentDOMStyleTest extends FluentDOMTestCase {
  24. const HTML = '
  25. <html>
  26. <body>
  27. <div style="text-align: left;">First</div>
  28. <div style="text-align: right;">Second</div>
  29. <div>Third</div>
  30. </body>
  31. </html>
  32. ';
  33. /**
  34. * @group GlobalFunctions
  35. */
  36. public function testFunction() {
  37. $fd = FluentDOMStyle();
  38. $this->assertTrue($fd instanceof FluentDOMStyle);
  39. }
  40. /**
  41. * @group GlobalFunctions
  42. */
  43. public function testFunctionWithContent() {
  44. $dom = new DOMDocument();
  45. $node = $dom->appendChild($dom->createElement('html'));
  46. $fd = FluentDOMStyle($node);
  47. $this->assertTrue($fd instanceof FluentDOMStyle);
  48. $this->assertEquals('html', $fd->document->documentElement->nodeName);
  49. }
  50. /**
  51. * @group ManipulationCSS
  52. * @covers FluentDOMStyle::__get
  53. */
  54. public function testPropertyCssGet() {
  55. $fd = $this->getFluentDOMStyleFixture('<sample style="test: success"/>', '/*');
  56. $css = $fd->css;
  57. $this->assertInstanceOf('FluentDOMCss', $css);
  58. $this->assertAttributeSame(
  59. $fd, '_fd', $css
  60. );
  61. }
  62. /**
  63. * @group ManipulationCSS
  64. * @covers FluentDOMStyle::__set
  65. */
  66. public function testPropertyCssSetWithArray() {
  67. $fd = $this->getFluentDOMStyleFixture('<sample/>', '/*');
  68. $fd->css = array('foo' => '1', 'bar' => '2');
  69. $this->assertEquals(
  70. '<sample style="bar: 2; foo: 1;"/>',
  71. $fd->document->saveXml($fd->document->documentElement)
  72. );
  73. }
  74. /**
  75. * @group ManipulationCSS
  76. * @covers FluentDOMStyle::__set
  77. */
  78. public function testPropertyCssSetWithFluentDOMCss() {
  79. $fd = $this->getFluentDOMStyleFixture('<sample/>', '/*');
  80. $fd->css = new FluentDOMCssProperties('foo: 1; bar: 2;');
  81. $this->assertEquals(
  82. '<sample style="bar: 2; foo: 1;"/>',
  83. $fd->document->saveXml($fd->document->documentElement)
  84. );
  85. }
  86. /**
  87. * @group ManipulationCSS
  88. * @covers FluentDOMStyle::css
  89. */
  90. public function testCssRead() {
  91. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  92. $this->assertTrue($fd instanceof FluentDOMStyle);
  93. $this->assertEquals('left', $fd->css('text-align'));
  94. }
  95. /**
  96. * @group ManipulationCSS
  97. * @covers FluentDOMStyle::css
  98. */
  99. public function testCssReadWithInvalidProperty() {
  100. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  101. $this->assertTrue($fd instanceof FluentDOMStyle);
  102. $this->assertEquals(NULL, $fd->css('---'));
  103. }
  104. /**
  105. * @group ManipulationCSS
  106. * @covers FluentDOMStyle::css
  107. */
  108. public function testCssReadOnEmpty() {
  109. $fd = $this->getFluentDOMStyleFixture(self::HTML);
  110. $this->assertTrue($fd instanceof FluentDOMStyle);
  111. $this->assertEquals(NULL, $fd->css('text-align'));
  112. }
  113. /**
  114. * @group ManipulationCSS
  115. * @covers FluentDOMStyle::css
  116. */
  117. public function testCssReadOnTextNodes() {
  118. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div')->contents()->andSelf();
  119. $this->assertTrue(count($fd) > 3);
  120. $this->assertEquals('left', $fd->css('text-align'));
  121. }
  122. /**
  123. * @group ManipulationCSS
  124. * @covers FluentDOMStyle::css
  125. */
  126. public function testCssWriteWithString() {
  127. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  128. $this->assertTrue($fd instanceof FluentDOMStyle);
  129. $fd->css('text-align', 'center');
  130. $this->assertEquals('text-align: center;', $fd->eq(0)->attr('style'));
  131. $this->assertEquals('text-align: center;', $fd->eq(1)->attr('style'));
  132. }
  133. /**
  134. * @group ManipulationCSS
  135. * @covers FluentDOMStyle::css
  136. */
  137. public function testCssWriteWithArray() {
  138. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  139. $this->assertTrue($fd instanceof FluentDOMStyle);
  140. $fd->css(
  141. array(
  142. 'text-align' => 'center',
  143. 'color' => 'black'
  144. )
  145. );
  146. $this->assertEquals('color: black; text-align: center;', $fd->eq(0)->attr('style'));
  147. $this->assertEquals('color: black; text-align: center;', $fd->eq(1)->attr('style'));
  148. }
  149. /**
  150. * @group ManipulationCSS
  151. * @covers FluentDOMStyle::css
  152. */
  153. public function testCssWriteWithCallback() {
  154. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  155. $this->assertTrue($fd instanceof FluentDOMStyle);
  156. $fd->css('text-align', array($this, 'callbackTestCssWriteWithCallback'));
  157. $this->assertEquals('text-align: right;', $fd->eq(0)->attr('style'));
  158. $this->assertEquals('text-align: left;', $fd->eq(1)->attr('style'));
  159. }
  160. /**
  161. * @group ManipulationCSS
  162. * @covers FluentDOMStyle::css
  163. */
  164. public function testCssWriteWithInvalidPropertySyntax() {
  165. try {
  166. $this->getFluentDOMStyleFixture(self::HTML, '//div')->css('---', '');
  167. $this->fail('An expected exception has not been raised.');
  168. } catch (InvalidArgumentException $expected) {
  169. }
  170. }
  171. /**
  172. * @group ManipulationCSS
  173. * @covers FluentDOMStyle::css
  174. */
  175. public function testCssWriteWithInvalidPropertyType() {
  176. try {
  177. $this->getFluentDOMStyleFixture(self::HTML, '//div')->css(23, '');
  178. $this->fail('An expected exception has not been raised.');
  179. } catch (InvalidArgumentException $expected) {
  180. }
  181. }
  182. /**
  183. * @group ManipulationCSS
  184. * @covers FluentDOMStyle::css
  185. */
  186. public function testCssWriteWithInvalidPropertyInArray() {
  187. try {
  188. $this->getFluentDOMStyleFixture(self::HTML, '//div')->css(array('---' => ''));
  189. $this->fail('An expected exception has not been raised.');
  190. } catch (InvalidArgumentException $expected) {
  191. }
  192. }
  193. /**
  194. * @group ManipulationCSS
  195. * @covers FluentDOMStyle::css
  196. */
  197. public function testCssRemoveProperty() {
  198. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  199. $fd->css('text-align', '');
  200. $this->assertFalse($fd[0]->hasAttribute('style'));
  201. }
  202. /**
  203. * @group ManipulationCSS
  204. * @covers FluentDOMStyle::css
  205. */
  206. public function testCssRemoveProperties() {
  207. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  208. $fd->css(
  209. array(
  210. 'text-align' => '',
  211. 'font-weight' => ''
  212. )
  213. );
  214. $this->assertFalse($fd[0]->hasAttribute('style'));
  215. }
  216. /**
  217. * @group ManipulationCSS
  218. * @covers FluentDOMStyle::css
  219. */
  220. public function testCssSortPropertiesName() {
  221. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  222. $fd->css(
  223. array(
  224. 'padding' => '0em',
  225. 'margin' => '1em'
  226. )
  227. );
  228. $expect = 'margin: 1em; padding: 0em;';
  229. $this->assertEquals($expect, $fd[2]->getAttribute('style'));
  230. }
  231. /**
  232. * @group ManipulationCSS
  233. * @covers FluentDOMStyle::css
  234. */
  235. public function testCssSortPropertiesLevels() {
  236. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  237. $fd->css(
  238. array(
  239. 'border' => '1px solid red',
  240. 'border-top-color' => 'black',
  241. 'border-top' => '2px solid blue'
  242. )
  243. );
  244. $expect = 'border: 1px solid red; border-top: 2px solid blue; border-top-color: black;';
  245. $this->assertEquals($expect, $fd[2]->getAttribute('style'));
  246. }
  247. /**
  248. * @group ManipulationCSS
  249. * @covers FluentDOMStyle::css
  250. */
  251. public function testCssSortPropertiesPrefix() {
  252. $fd = $this->getFluentDOMStyleFixture(self::HTML, '//div');
  253. $fd->css(
  254. array(
  255. '-moz-opacity' => 30,
  256. '-o-opacity' => 30,
  257. 'opacity' => 30
  258. )
  259. );
  260. $expect = 'opacity: 30; -moz-opacity: 30; -o-opacity: 30;';
  261. $this->assertEquals($expect, $fd[2]->getAttribute('style'));
  262. }
  263. /**
  264. * @covers FluentDOMStyle::__set
  265. * @covers FluentDOMStyle::__get
  266. */
  267. public function testPropertyInheritance() {
  268. $fd = new FluentDOMStyle();
  269. $fd->dynamicPropertyName = 23;
  270. $this->assertEquals(23, $fd->dynamicPropertyName);
  271. }
  272. /*
  273. * Callbacks
  274. */
  275. /**
  276. * @uses testCssWriteWithCallback()
  277. */
  278. public function callbackTestCssWriteWithCallback($node, $index, $value) {
  279. switch ($value) {
  280. case 'left' :
  281. return 'right';
  282. case 'right' :
  283. return 'left';
  284. default :
  285. return 'center';
  286. }
  287. }
  288. /**
  289. * Fixtures
  290. */
  291. /**
  292. * Get FluentDOMStyle instance with loaded html document using a mock loader
  293. *
  294. * @return FluentDOMStyle
  295. */
  296. protected function getFluentDOMStyleFixture($string = NULL, $xpath = NULL) {
  297. $fd = new FluentDOMStyle();
  298. if (!empty($string)) {
  299. $dom = new DOMDocument();
  300. $dom->loadXML($string);
  301. $loader = $this->getMock('FluentDOMLoader');
  302. $loader
  303. ->expects($this->once())
  304. ->method('load')
  305. ->with($this->equalTo('mocked'))
  306. ->will($this->returnValue($dom));
  307. $fd->setLoaders(array($loader));
  308. $fd->load('mocked');
  309. if (!empty($xpath)) {
  310. $query = new DOMXPath($dom);
  311. $nodes = $query->evaluate($xpath);
  312. $fd = $fd->spawn();
  313. $fd->push($nodes);
  314. }
  315. }
  316. return $fd;
  317. }
  318. }
  319. ?>