123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <?php
- declare(strict_types=1);
- namespace DiDom\Tests;
- use DiDom\Element;
- use DiDom\StyleAttribute;
- use DOMComment;
- use DOMText;
- use InvalidArgumentException;
- class StyleAttributeTest extends TestCase
- {
- public function testConstructorWithTextNode()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The element must contain DOMElement node.');
- $element = new Element(new DOMText('foo'));
- new StyleAttribute($element);
- }
- public function testConstructorWithCommentNode()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('The element must contain DOMElement node.');
- $element = new Element(new DOMComment('foo'));
- new StyleAttribute($element);
- }
- public function testSetProperty()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
- $styleAttribute->setProperty('font-size', '16px');
- $this->assertEquals('color: blue; border: 1px solid black; font-size: 16px', $element->getAttribute('style'));
- }
- public function testSetMultiplePropertiesWithInvalidPropertyName()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Property name must be a string, integer given.');
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $styleAttribute->setMultipleProperties([
- 'width' => '50px',
- 'height',
- ]);
- }
- public function testSetMultiplePropertiesWithInvalidPropertyValue()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Property value must be a string, NULL given.');
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $styleAttribute->setMultipleProperties([
- 'width' => '50px',
- 'height' => null,
- ]);
- }
- public function testSetMultipleProperties()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
- $styleAttribute->setMultipleProperties([
- 'font-size' => '16px',
- 'font-family' => 'Times',
- ]);
- $this->assertEquals('color: blue; border: 1px solid black; font-size: 16px; font-family: Times', $element->getAttribute('style'));
- }
- /**
- * @param string $styleString
- * @param string $propertyName
- * @param string $expectedResult
- *
- * @dataProvider getPropertyDataProvider
- */
- public function testGetProperty($styleString, $propertyName, $expectedResult)
- {
- $element = new Element('div', null, [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($expectedResult, $styleAttribute->getProperty($propertyName));
- }
- public function getPropertyDataProvider(): array
- {
- return [
- [
- 'color: blue; font-size: 16px; border: 1px solid black',
- 'font-size',
- '16px',
- ],
- [
- 'background-image: url(https://example.com/image.jpg); background-repeat: no-repeat',
- 'background-image',
- 'url(https://example.com/image.jpg)',
- ],
- [
- 'color: blue; font-size: 16px; border: 1px solid black;',
- 'foo',
- null,
- ],
- ];
- }
- public function testGetPropertyWithDefaultValue()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertNull($styleAttribute->getProperty('font-size'));
- $this->assertEquals('16px', $styleAttribute->getProperty('font-size', '16px'));
- }
- public function testGetMultiplePropertiesWithInvalidPropertyName()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Property name must be a string, NULL given.');
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $styleAttribute->getMultipleProperties(['color', null]);
- }
- /**
- * @param string $styleString
- * @param array $propertyNames
- * @param string $expectedResult
- *
- * @dataProvider getMultiplePropertiesDataProvider
- */
- public function testGetMultipleProperties($styleString, $propertyNames, $expectedResult)
- {
- $element = new Element('div', null, [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($expectedResult, $styleAttribute->getMultipleProperties($propertyNames));
- }
- public function getMultiplePropertiesDataProvider()
- {
- return [
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- ['font-size'],
- [
- 'font-size' => '16px',
- ],
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- ['font-size', 'border'],
- [
- 'font-size' => '16px',
- 'border' => '1px solid black',
- ],
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- ['font-size', 'border', 'width'],
- [
- 'font-size' => '16px',
- 'border' => '1px solid black',
- ],
- ],
- ];
- }
- /**
- * @param string $styleString
- * @param string $expectedResult
- *
- * @dataProvider getAllPropertiesDataProvider
- */
- public function testGetAllProperties($styleString, $expectedResult)
- {
- $element = new Element('div', null, [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($expectedResult, $styleAttribute->getAllProperties());
- }
- public function getAllPropertiesDataProvider(): array
- {
- return [
- [
- '',
- [],
- ],
- [
- 'color: blue; font-size: 16px; border: 1px solid black',
- [
- 'color' => 'blue',
- 'font-size' => '16px',
- 'border' => '1px solid black',
- ],
- ],
- [
- 'color: blue; font-size: 16px; border: 1px solid black',
- [
- 'color' => 'blue',
- 'font-size' => '16px',
- 'border' => '1px solid black',
- ],
- ],
- ];
- }
- public function testGetAllPropertiesAfterEmptyStyleAttribute()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals(['color' => 'blue'], $styleAttribute->getAllProperties());
- $element->setAttribute('style', '');
- $this->assertEquals([], $styleAttribute->getAllProperties());
- }
- public function testHasProperty()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertTrue($styleAttribute->hasProperty('color'));
- $this->assertFalse($styleAttribute->hasProperty('width'));
- }
- public function testRemoveProperty()
- {
- $styleString = 'color: blue; font-size: 16px; border: 1px solid black';
- $element = new Element('span', 'foo', [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($styleString, $element->getAttribute('style'));
- $styleAttribute->removeProperty('font-size');
- $this->assertEquals('color: blue; border: 1px solid black', $element->getAttribute('style'));
- }
- public function testRemoveMultiplePropertiesWithInvalidPropertyName()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Property name must be a string, NULL given.');
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $styleAttribute->removeMultipleProperties(['color', null]);
- }
- /**
- * @param string $styleString
- * @param array $propertyNames
- * @param string $expectedResult
- *
- * @dataProvider removeMultiplePropertiesDataProvider
- */
- public function testRemoveMultipleProperties($styleString, $propertyNames, $expectedResult)
- {
- $element = new Element('div', null, [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($styleString, $element->getAttribute('style'));
- $styleAttribute->removeMultipleProperties($propertyNames);
- $this->assertEquals($expectedResult, $element->getAttribute('style'));
- }
- public function removeMultiplePropertiesDataProvider(): array
- {
- return [
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size',
- ],
- 'color: blue; font-family: Times; border: 1px solid black',
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size', 'border',
- ],
- 'color: blue; font-family: Times',
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size', 'border', 'width',
- ],
- 'color: blue; font-family: Times',
- ],
- ];
- }
- public function testRemoveAllPropertiesWithInvalidPropertyName()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Property name must be a string, NULL given.');
- $element = new Element('div', null, [
- 'style' => 'color: blue; border: 1px solid black',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $styleAttribute->removeAllProperties(['color', null]);
- }
- /**
- * @param string $styleString
- * @param array $exclusions
- * @param string $expectedResult
- *
- * @dataProvider removeAllPropertiesDataProvider
- */
- public function testRemoveAllProperties($styleString, $exclusions, $expectedResult)
- {
- $element = new Element('div', null, [
- 'style' => $styleString,
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertEquals($styleString, $element->getAttribute('style'));
- $styleAttribute->removeAllProperties($exclusions);
- $this->assertEquals($expectedResult, $element->getAttribute('style'));
- }
- public function removeAllPropertiesDataProvider(): array
- {
- return [
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size',
- ],
- 'font-size: 16px',
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size', 'border',
- ],
- 'font-size: 16px; border: 1px solid black',
- ],
- [
- 'color: blue; font-size: 16px; font-family: Times; border: 1px solid black',
- [
- 'font-size', 'border', 'width',
- ],
- 'font-size: 16px; border: 1px solid black',
- ],
- ];
- }
- public function testGetElement()
- {
- $element = new Element('div', null, [
- 'style' => 'color: blue; font-size: 16px',
- ]);
- $styleAttribute = new StyleAttribute($element);
- $this->assertSame($element, $styleAttribute->getElement());
- }
- }
|