|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sabberworm\CSS\Tests\Unit\Property\Selector; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sabberworm\CSS\Property\Selector\SpecificityCalculator; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers \Sabberworm\CSS\Property\Selector\SpecificityCalculator |
| 12 | + */ |
| 13 | +final class SpecificityCalculatorTest extends TestCase |
| 14 | +{ |
| 15 | + protected function tearDown(): void |
| 16 | + { |
| 17 | + SpecificityCalculator::clearCache(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @return array<string, array{0: non-empty-string, 1: int<0, max>}> |
| 22 | + */ |
| 23 | + public static function provideSelectorsAndSpecificities(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'element' => ['a', 1], |
| 27 | + 'element and descendant with pseudo-selector' => ['ol li::before', 3], |
| 28 | + 'class' => ['.highlighted', 10], |
| 29 | + 'element with class' => ['li.green', 11], |
| 30 | + 'class with pseudo-selector' => ['.help:hover', 20], |
| 31 | + 'ID' => ['#file', 100], |
| 32 | + 'ID and descendant class' => ['#test .help', 110], |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @test |
| 38 | + * |
| 39 | + * @param non-empty-string $selector |
| 40 | + * @param int<0, max> $expectedSpecificity |
| 41 | + * |
| 42 | + * @dataProvider provideSelectorsAndSpecificities |
| 43 | + */ |
| 44 | + public function calculateReturnsSpecificityForProvidedSelector( |
| 45 | + string $selector, |
| 46 | + int $expectedSpecificity |
| 47 | + ): void { |
| 48 | + self::assertSame($expectedSpecificity, SpecificityCalculator::calculate($selector)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @test |
| 53 | + * |
| 54 | + * @param non-empty-string $selector |
| 55 | + * @param int<0, max> $expectedSpecificity |
| 56 | + * |
| 57 | + * @dataProvider provideSelectorsAndSpecificities |
| 58 | + */ |
| 59 | + public function calculateAfterClearingCacheReturnsSpecificityForProvidedSelector( |
| 60 | + string $selector, |
| 61 | + int $expectedSpecificity |
| 62 | + ): void { |
| 63 | + SpecificityCalculator::clearCache(); |
| 64 | + |
| 65 | + self::assertSame($expectedSpecificity, SpecificityCalculator::calculate($selector)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @test |
| 70 | + */ |
| 71 | + public function calculateCalledTwoTimesReturnsSameSpecificityForProvidedSelector(): void |
| 72 | + { |
| 73 | + $selector = '#test .help'; |
| 74 | + |
| 75 | + $firstResult = SpecificityCalculator::calculate($selector); |
| 76 | + $secondResult = SpecificityCalculator::calculate($selector); |
| 77 | + |
| 78 | + self::assertSame($firstResult, $secondResult); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @test |
| 83 | + */ |
| 84 | + public function calculateCalledReturnsSameSpecificityForProvidedSelectorBeforeAndAfterClearingCache(): void |
| 85 | + { |
| 86 | + $selector = '#test .help'; |
| 87 | + |
| 88 | + $firstResult = SpecificityCalculator::calculate($selector); |
| 89 | + SpecificityCalculator::clearCache(); |
| 90 | + $secondResult = SpecificityCalculator::calculate($selector); |
| 91 | + |
| 92 | + self::assertSame($firstResult, $secondResult); |
| 93 | + } |
| 94 | +} |
0 commit comments