|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sabberworm\CSS\Tests\Unit\RuleSet; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sabberworm\CSS\CSSElement; |
| 9 | +use Sabberworm\CSS\Position\Positionable; |
| 10 | +use Sabberworm\CSS\Parsing\ParserState; |
| 11 | +use Sabberworm\CSS\Property\Selector; |
| 12 | +use Sabberworm\CSS\RuleSet\DeclarationBlock; |
| 13 | +use Sabberworm\CSS\Settings; |
| 14 | +use TRegx\PhpUnit\DataProviders\DataProvider; |
| 15 | + |
| 16 | +/** |
| 17 | + * @covers \Sabberworm\CSS\RuleSet\DeclarationBlock |
| 18 | + */ |
| 19 | +final class DeclarationBlockTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var DeclarationBlock |
| 23 | + */ |
| 24 | + private $subject; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->subject = new DeclarationBlock(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @test |
| 33 | + */ |
| 34 | + public function implementsCSSElement(): void |
| 35 | + { |
| 36 | + self::assertInstanceOf(CSSElement::class, $this->subject); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @test |
| 41 | + */ |
| 42 | + public function implementsPositionable(): void |
| 43 | + { |
| 44 | + self::assertInstanceOf(Positionable::class, $this->subject); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return array<non-empty-string, array{0: non-empty-string}> |
| 49 | + */ |
| 50 | + public static function provideSelector(): array |
| 51 | + { |
| 52 | + return [ |
| 53 | + 'type' => ['body'], |
| 54 | + 'class' => ['.teapot'], |
| 55 | + 'type & class' => ['img.teapot'], |
| 56 | + 'id' => ['#my-mug'], |
| 57 | + 'type & id' => ['h2#my-mug'], |
| 58 | + 'pseudo-class' => [':hover'], |
| 59 | + 'type & pseudo-class' => ['a:hover'], |
| 60 | + '`not`' => [':not(#your-mug)'], |
| 61 | + 'pseudo-element' => ['::before'], |
| 62 | + 'attribute with `"`' => ['[alt="{}()[]\\"\'"]'], |
| 63 | + 'attribute with `\'`' => ['[alt=\'{}()[]"\\\'\']'], |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @test |
| 69 | + * |
| 70 | + * @param non-empty-string $selector |
| 71 | + * |
| 72 | + * @dataProvider provideSelector |
| 73 | + */ |
| 74 | + public function parsesSingleSelector(string $selector): void |
| 75 | + { |
| 76 | + $subject = DeclarationBlock::parse(new ParserState($selector . ' {}', Settings::create())); |
| 77 | + |
| 78 | + self::assertInstanceOf(DeclarationBlock::class, $subject); |
| 79 | + self::assertSame([$selector], self::getSelectorsAsStrings($subject)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return DataProvider<non-empty-string, array{0: non-empty-string, 1: non-empty-string}> |
| 84 | + */ |
| 85 | + public static function provideTwoSelectors(): DataProvider |
| 86 | + { |
| 87 | + return DataProvider::cross(self::provideSelector(), self::provideSelector()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @test |
| 92 | + * |
| 93 | + * @param non-empty-string $firstSelector |
| 94 | + * @param non-empty-string $secondSelector |
| 95 | + * |
| 96 | + * @dataProvider provideTwoSelectors |
| 97 | + */ |
| 98 | + public function parsesTwoCommaSeparatedSelectors(string $firstSelector, string $secondSelector): void |
| 99 | + { |
| 100 | + $joinedSelectors = $firstSelector . ', ' . $secondSelector; |
| 101 | + |
| 102 | + $subject = DeclarationBlock::parse(new ParserState($joinedSelectors . ' {}', Settings::create())); |
| 103 | + |
| 104 | + self::assertInstanceOf(DeclarationBlock::class, $subject); |
| 105 | + self::assertSame([$firstSelector, $secondSelector], self::getSelectorsAsStrings($subject)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array<string> |
| 110 | + */ |
| 111 | + private static function getSelectorsAsStrings(DeclarationBlock $declarationBlock): array |
| 112 | + { |
| 113 | + return \array_map( |
| 114 | + static function (Selector $selectorObject): string { |
| 115 | + return $selectorObject->getSelector(); |
| 116 | + }, |
| 117 | + $declarationBlock->getSelectors() |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments