StyleAttributeTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. declare(strict_types=1);
  3. namespace DiDom\Tests;
  4. use DiDom\Element;
  5. use DiDom\StyleAttribute;
  6. use DOMComment;
  7. use DOMText;
  8. use InvalidArgumentException;
  9. class StyleAttributeTest extends TestCase
  10. {
  11. public function testConstructorWithTextNode()
  12. {
  13. $this->expectException(InvalidArgumentException::class);
  14. $this->expectExceptionMessage('The element must contain DOMElement node.');
  15. $element = new Element(new DOMText('foo'));
  16. new StyleAttribute($element);
  17. }
  18. public function testConstructorWithCommentNode()
  19. {
  20. $this->expectException(InvalidArgumentException::class);
  21. $this->expectExceptionMessage('The element must contain DOMElement node.');
  22. $element = new Element(new DOMComment('foo'));
  23. new StyleAttribute($element);
  24. }
  25. public function testSetProperty()
  26. {
  27. $element = new Element('div', null, [
  28. 'style' => 'color: blue; border: 1px solid black',
  29. ]);
  30. $styleAttribute = new StyleAttribute($element);
  31. $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
  32. $styleAttribute->setProperty('font-size', '16px');
  33. $this->assertEquals('color: blue; border: 1px solid black; font-size: 16px', $element->getAttribute('style'));
  34. }
  35. public function testSetMultiplePropertiesWithInvalidPropertyName()
  36. {
  37. $this->expectException(InvalidArgumentException::class);
  38. $this->expectExceptionMessage('Property name must be a string, integer given.');
  39. $element = new Element('div', null, [
  40. 'style' => 'color: blue; border: 1px solid black',
  41. ]);
  42. $styleAttribute = new StyleAttribute($element);
  43. $styleAttribute->setMultipleProperties([
  44. 'width' => '50px',
  45. 'height',
  46. ]);
  47. }
  48. public function testSetMultiplePropertiesWithInvalidPropertyValue()
  49. {
  50. $this->expectException(InvalidArgumentException::class);
  51. $this->expectExceptionMessage('Property value must be a string, NULL given.');
  52. $element = new Element('div', null, [
  53. 'style' => 'color: blue; border: 1px solid black',
  54. ]);
  55. $styleAttribute = new StyleAttribute($element);
  56. $styleAttribute->setMultipleProperties([
  57. 'width' => '50px',
  58. 'height' => null,
  59. ]);
  60. }
  61. public function testSetMultipleProperties()
  62. {
  63. $element = new Element('div', null, [
  64. 'style' => 'color: blue; border: 1px solid black',
  65. ]);
  66. $styleAttribute = new StyleAttribute($element);
  67. $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
  68. $styleAttribute->setMultipleProperties([
  69. 'font-size' => '16px',
  70. 'font-family' => 'Times',
  71. ]);
  72. $this->assertEquals('color: blue; border: 1px solid black; font-size: 16px; font-family: Times', $element->getAttribute('style'));
  73. }
  74. /**
  75. * @param string $styleString
  76. * @param string $propertyName
  77. * @param string $expectedResult
  78. *
  79. * @dataProvider getPropertyDataProvider
  80. */
  81. public function testGetProperty($styleString, $propertyName, $expectedResult)
  82. {
  83. $element = new Element('div', null, [
  84. 'style' => $styleString,
  85. ]);
  86. $styleAttribute = new StyleAttribute($element);
  87. $this->assertEquals($expectedResult, $styleAttribute->getProperty($propertyName));
  88. }
  89. public function getPropertyDataProvider(): array
  90. {
  91. return [
  92. [
  93. 'color: blue; font-size: 16px; border: 1px solid black',
  94. 'font-size',
  95. '16px',
  96. ],
  97. [
  98. 'background-image: url(https://example.com/image.jpg); background-repeat: no-repeat',
  99. 'background-image',
  100. 'url(https://example.com/image.jpg)',
  101. ],
  102. [
  103. 'color: blue; font-size: 16px; border: 1px solid black;',
  104. 'foo',
  105. null,
  106. ],
  107. ];
  108. }
  109. public function testGetPropertyWithDefaultValue()
  110. {
  111. $element = new Element('div', null, [
  112. 'style' => 'color: blue',
  113. ]);
  114. $styleAttribute = new StyleAttribute($element);
  115. $this->assertNull($styleAttribute->getProperty('font-size'));
  116. $this->assertEquals('16px', $styleAttribute->getProperty('font-size', '16px'));
  117. }
  118. public function testGetMultiplePropertiesWithInvalidPropertyName()
  119. {
  120. $this->expectException(InvalidArgumentException::class);
  121. $this->expectExceptionMessage('Property name must be a string, NULL given.');
  122. $element = new Element('div', null, [
  123. 'style' => 'color: blue; border: 1px solid black',
  124. ]);
  125. $styleAttribute = new StyleAttribute($element);
  126. $styleAttribute->getMultipleProperties(['color', null]);
  127. }
  128. /**
  129. * @param string $styleString
  130. * @param array $propertyNames
  131. * @param string $expectedResult
  132. *
  133. * @dataProvider getMultiplePropertiesDataProvider
  134. */
  135. public function testGetMultipleProperties($styleString, $propertyNames, $expectedResult)
  136. {
  137. $element = new Element('div', null, [
  138. 'style' => $styleString,
  139. ]);
  140. $styleAttribute = new StyleAttribute($element);
  141. $this->assertEquals($expectedResult, $styleAttribute->getMultipleProperties($propertyNames));
  142. }
  143. public function getMultiplePropertiesDataProvider()
  144. {
  145. return [
  146. [
  147. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  148. ['font-size'],
  149. [
  150. 'font-size' => '16px',
  151. ],
  152. ],
  153. [
  154. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  155. ['font-size', 'border'],
  156. [
  157. 'font-size' => '16px',
  158. 'border' => '1px solid black',
  159. ],
  160. ],
  161. [
  162. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  163. ['font-size', 'border', 'width'],
  164. [
  165. 'font-size' => '16px',
  166. 'border' => '1px solid black',
  167. ],
  168. ],
  169. ];
  170. }
  171. /**
  172. * @param string $styleString
  173. * @param string $expectedResult
  174. *
  175. * @dataProvider getAllPropertiesDataProvider
  176. */
  177. public function testGetAllProperties($styleString, $expectedResult)
  178. {
  179. $element = new Element('div', null, [
  180. 'style' => $styleString,
  181. ]);
  182. $styleAttribute = new StyleAttribute($element);
  183. $this->assertEquals($expectedResult, $styleAttribute->getAllProperties());
  184. }
  185. public function getAllPropertiesDataProvider(): array
  186. {
  187. return [
  188. [
  189. '',
  190. [],
  191. ],
  192. [
  193. 'color: blue; font-size: 16px; border: 1px solid black',
  194. [
  195. 'color' => 'blue',
  196. 'font-size' => '16px',
  197. 'border' => '1px solid black',
  198. ],
  199. ],
  200. [
  201. 'color: blue; font-size: 16px; border: 1px solid black',
  202. [
  203. 'color' => 'blue',
  204. 'font-size' => '16px',
  205. 'border' => '1px solid black',
  206. ],
  207. ],
  208. ];
  209. }
  210. public function testGetAllPropertiesAfterEmptyStyleAttribute()
  211. {
  212. $element = new Element('div', null, [
  213. 'style' => 'color: blue',
  214. ]);
  215. $styleAttribute = new StyleAttribute($element);
  216. $this->assertEquals(['color' => 'blue'], $styleAttribute->getAllProperties());
  217. $element->setAttribute('style', '');
  218. $this->assertEquals([], $styleAttribute->getAllProperties());
  219. }
  220. public function testHasProperty()
  221. {
  222. $element = new Element('div', null, [
  223. 'style' => 'color: blue; border: 1px solid black',
  224. ]);
  225. $styleAttribute = new StyleAttribute($element);
  226. $this->assertTrue($styleAttribute->hasProperty('color'));
  227. $this->assertFalse($styleAttribute->hasProperty('width'));
  228. }
  229. public function testRemoveProperty()
  230. {
  231. $styleString = 'color: blue; font-size: 16px; border: 1px solid black';
  232. $element = new Element('span', 'foo', [
  233. 'style' => $styleString,
  234. ]);
  235. $styleAttribute = new StyleAttribute($element);
  236. $this->assertEquals($styleString, $element->getAttribute('style'));
  237. $styleAttribute->removeProperty('font-size');
  238. $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
  239. }
  240. public function testRemoveMultiplePropertiesWithInvalidPropertyName()
  241. {
  242. $this->expectException(InvalidArgumentException::class);
  243. $this->expectExceptionMessage('Property name must be a string, NULL given.');
  244. $element = new Element('div', null, [
  245. 'style' => 'color: blue; border: 1px solid black',
  246. ]);
  247. $styleAttribute = new StyleAttribute($element);
  248. $styleAttribute->removeMultipleProperties(['color', null]);
  249. }
  250. /**
  251. * @param string $styleString
  252. * @param array $propertyNames
  253. * @param string $expectedResult
  254. *
  255. * @dataProvider removeMultiplePropertiesDataProvider
  256. */
  257. public function testRemoveMultipleProperties($styleString, $propertyNames, $expectedResult)
  258. {
  259. $element = new Element('div', null, [
  260. 'style' => $styleString,
  261. ]);
  262. $styleAttribute = new StyleAttribute($element);
  263. $this->assertEquals($styleString, $element->getAttribute('style'));
  264. $styleAttribute->removeMultipleProperties($propertyNames);
  265. $this->assertEquals($expectedResult, $element->getAttribute('style'));
  266. }
  267. public function removeMultiplePropertiesDataProvider(): array
  268. {
  269. return [
  270. [
  271. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  272. [
  273. 'font-size',
  274. ],
  275. 'color: blue; font-family: Times; border: 1px solid black',
  276. ],
  277. [
  278. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  279. [
  280. 'font-size', 'border',
  281. ],
  282. 'color: blue; font-family: Times',
  283. ],
  284. [
  285. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  286. [
  287. 'font-size', 'border', 'width',
  288. ],
  289. 'color: blue; font-family: Times',
  290. ],
  291. ];
  292. }
  293. public function testRemoveAllPropertiesWithInvalidPropertyName()
  294. {
  295. $this->expectException(InvalidArgumentException::class);
  296. $this->expectExceptionMessage('Property name must be a string, NULL given.');
  297. $element = new Element('div', null, [
  298. 'style' => 'color: blue; border: 1px solid black',
  299. ]);
  300. $styleAttribute = new StyleAttribute($element);
  301. $styleAttribute->removeAllProperties(['color', null]);
  302. }
  303. /**
  304. * @param string $styleString
  305. * @param array $exclusions
  306. * @param string $expectedResult
  307. *
  308. * @dataProvider removeAllPropertiesDataProvider
  309. */
  310. public function testRemoveAllProperties($styleString, $exclusions, $expectedResult)
  311. {
  312. $element = new Element('div', null, [
  313. 'style' => $styleString,
  314. ]);
  315. $styleAttribute = new StyleAttribute($element);
  316. $this->assertEquals($styleString, $element->getAttribute('style'));
  317. $styleAttribute->removeAllProperties($exclusions);
  318. $this->assertEquals($expectedResult, $element->getAttribute('style'));
  319. }
  320. public function removeAllPropertiesDataProvider(): array
  321. {
  322. return [
  323. [
  324. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  325. [
  326. 'font-size',
  327. ],
  328. 'font-size: 16px',
  329. ],
  330. [
  331. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  332. [
  333. 'font-size', 'border',
  334. ],
  335. 'font-size: 16px; border: 1px solid black',
  336. ],
  337. [
  338. 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
  339. [
  340. 'font-size', 'border', 'width',
  341. ],
  342. 'font-size: 16px; border: 1px solid black',
  343. ],
  344. ];
  345. }
  346. public function testGetElement()
  347. {
  348. $element = new Element('div', null, [
  349. 'style' => 'color: blue; font-size: 16px',
  350. ]);
  351. $styleAttribute = new StyleAttribute($element);
  352. $this->assertSame($element, $styleAttribute->getElement());
  353. }
  354. }