ClassAttributeTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. declare(strict_types=1);
  3. namespace DiDom\Tests;
  4. use DiDom\Element;
  5. use DiDom\ClassAttribute;
  6. use DOMComment;
  7. use DOMText;
  8. use InvalidArgumentException;
  9. class ClassAttributeTest 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 ClassAttribute($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 ClassAttribute($element);
  24. }
  25. public function testAdd()
  26. {
  27. // without class attribute
  28. $element = new Element('div', null);
  29. $classAttribute = new ClassAttribute($element);
  30. $this->assertEquals(null, $element->getAttribute('class'));
  31. $classAttribute->add('foo');
  32. $this->assertEquals('foo', $element->getAttribute('class'));
  33. // with empty class attribute
  34. $element = new Element('div', null, [
  35. 'class' => '',
  36. ]);
  37. $classAttribute = new ClassAttribute($element);
  38. $this->assertEquals('', $element->getAttribute('class'));
  39. $classAttribute->add('foo');
  40. $this->assertEquals('foo', $element->getAttribute('class'));
  41. // class attribute with spaces
  42. $element = new Element('div', null, [
  43. 'class' => ' ',
  44. ]);
  45. $classAttribute = new ClassAttribute($element);
  46. $this->assertEquals(' ', $element->getAttribute('class'));
  47. $classAttribute->add('foo');
  48. $this->assertEquals('foo', $element->getAttribute('class'));
  49. // with not empty class attribute
  50. $element = new Element('div', null, [
  51. 'class' => 'foo bar',
  52. ]);
  53. $classAttribute = new ClassAttribute($element);
  54. $this->assertEquals('foo bar', $element->getAttribute('class'));
  55. $classAttribute->add('baz');
  56. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  57. // with existing class name
  58. $element = new Element('div', null, [
  59. 'class' => 'foo bar baz',
  60. ]);
  61. $classAttribute = new ClassAttribute($element);
  62. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  63. $classAttribute->add('bar');
  64. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  65. }
  66. public function testAddMultipleWithInvalidClassName()
  67. {
  68. $this->expectException(InvalidArgumentException::class);
  69. $this->expectExceptionMessage('Class name must be a string, NULL given.');
  70. $element = new Element('div', null, [
  71. 'class' => 'foo',
  72. ]);
  73. $classAttribute = new ClassAttribute($element);
  74. $classAttribute->addMultiple(['bar', null]);
  75. }
  76. public function testAddMultiple()
  77. {
  78. $element = new Element('div', null, [
  79. 'class' => 'foo',
  80. ]);
  81. $classAttribute = new ClassAttribute($element);
  82. $this->assertEquals('foo', $element->getAttribute('class'));
  83. $classAttribute->addMultiple([
  84. 'bar', 'baz',
  85. ]);
  86. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  87. }
  88. public function testGetAll()
  89. {
  90. // without class attribute
  91. $element = new Element('div', null);
  92. $classAttribute = new ClassAttribute($element);
  93. $this->assertEquals([], $classAttribute->getAll());
  94. // with empty class attribute
  95. $element = new Element('div', null, [
  96. 'class' => '',
  97. ]);
  98. $classAttribute = new ClassAttribute($element);
  99. $this->assertEquals([], $classAttribute->getAll());
  100. // class attribute with spaces
  101. $element = new Element('div', null, [
  102. 'class' => ' ',
  103. ]);
  104. $classAttribute = new ClassAttribute($element);
  105. $this->assertEquals([], $classAttribute->getAll());
  106. // one class
  107. $element = new Element('div', null, [
  108. 'class' => 'foo',
  109. ]);
  110. $classAttribute = new ClassAttribute($element);
  111. $this->assertEquals(['foo'], $classAttribute->getAll());
  112. // several classes
  113. $element = new Element('div', null, [
  114. 'class' => 'foo bar baz',
  115. ]);
  116. $classAttribute = new ClassAttribute($element);
  117. $this->assertEquals(['foo', 'bar', 'baz'], $classAttribute->getAll());
  118. // with multiple spaces between class names
  119. $element = new Element('div', null, [
  120. 'class' => 'foo bar baz',
  121. ]);
  122. $classAttribute = new ClassAttribute($element);
  123. $this->assertEquals(['foo', 'bar', 'baz'], $classAttribute->getAll());
  124. }
  125. public function testGetAllPropertiesAfterEmptyClassAttribute()
  126. {
  127. $element = new Element('div', null, [
  128. 'class' => 'foo bar baz',
  129. ]);
  130. $classAttribute = new ClassAttribute($element);
  131. $this->assertEquals(['foo', 'bar', 'baz'], $classAttribute->getAll());
  132. $element->setAttribute('class', '');
  133. $this->assertEquals([], $classAttribute->getAll());
  134. }
  135. public function testContains()
  136. {
  137. $element = new Element('div', null, [
  138. 'class' => 'foo bar',
  139. ]);
  140. $classAttribute = new ClassAttribute($element);
  141. $this->assertTrue($classAttribute->contains('foo'));
  142. $this->assertTrue($classAttribute->contains('bar'));
  143. $this->assertFalse($classAttribute->contains('baz'));
  144. }
  145. public function testRemove()
  146. {
  147. $element = new Element('div', null, [
  148. 'class' => 'foo bar baz',
  149. ]);
  150. $classAttribute = new ClassAttribute($element);
  151. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  152. $classAttribute->remove('bar');
  153. $this->assertEquals('foo baz', $element->getAttribute('class'));
  154. // with nonexistent class name
  155. $element = new Element('div', null, [
  156. 'class' => 'foo bar',
  157. ]);
  158. $classAttribute = new ClassAttribute($element);
  159. $this->assertEquals('foo bar', $element->getAttribute('class'));
  160. $classAttribute->remove('baz');
  161. $this->assertEquals('foo bar', $element->getAttribute('class'));
  162. }
  163. public function testRemoveMultipleWithInvalidClassName()
  164. {
  165. $this->expectException(InvalidArgumentException::class);
  166. $this->expectExceptionMessage('Class name must be a string, NULL given.');
  167. $element = new Element('div', null, [
  168. 'class' => 'foo bar baz',
  169. ]);
  170. $classAttribute = new ClassAttribute($element);
  171. $classAttribute->removeMultiple(['foo', null]);
  172. }
  173. public function testRemoveMultiple()
  174. {
  175. $element = new Element('div', null, [
  176. 'class' => 'foo bar baz',
  177. ]);
  178. $classAttribute = new ClassAttribute($element);
  179. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  180. $classAttribute->removeMultiple(['foo', 'bar']);
  181. $this->assertEquals('baz', $element->getAttribute('class'));
  182. // with nonexistent class name
  183. $element = new Element('div', null, [
  184. 'class' => 'foo bar baz',
  185. ]);
  186. $classAttribute = new ClassAttribute($element);
  187. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  188. $classAttribute->removeMultiple(['bar', 'qux']);
  189. $this->assertEquals('foo baz', $element->getAttribute('class'));
  190. }
  191. public function testRemoveAllWithInvalidClassName()
  192. {
  193. $this->expectException(InvalidArgumentException::class);
  194. $this->expectExceptionMessage('Class name must be a string, NULL given.');
  195. $element = new Element('div', null, [
  196. 'class' => 'foo bar baz',
  197. ]);
  198. $classAttribute = new ClassAttribute($element);
  199. $classAttribute->removeAll(['foo', null]);
  200. }
  201. public function testRemoveAll()
  202. {
  203. $element = new Element('div', null, [
  204. 'class' => 'foo bar baz',
  205. ]);
  206. $classAttribute = new ClassAttribute($element);
  207. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  208. $classAttribute->removeAll();
  209. $this->assertEquals('', $element->getAttribute('class'));
  210. }
  211. public function testRemoveAllWithExclusions()
  212. {
  213. $element = new Element('div', null, [
  214. 'class' => 'foo bar baz',
  215. ]);
  216. $classAttribute = new ClassAttribute($element);
  217. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  218. $classAttribute->removeAll(['bar']);
  219. $this->assertEquals('bar', $element->getAttribute('class'));
  220. // with nonexistent class name
  221. $element = new Element('div', null, [
  222. 'class' => 'foo bar baz',
  223. ]);
  224. $classAttribute = new ClassAttribute($element);
  225. $this->assertEquals('foo bar baz', $element->getAttribute('class'));
  226. $classAttribute->removeAll(['bar', 'qux']);
  227. $this->assertEquals('bar', $element->getAttribute('class'));
  228. }
  229. public function testGetElement()
  230. {
  231. $element = new Element('div', null, [
  232. 'class' => 'foo bar baz',
  233. ]);
  234. $classAttribute = new ClassAttribute($element);
  235. $this->assertSame($element, $classAttribute->getElement());
  236. }
  237. }