diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 535469d6..f2969599 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -222,12 +222,6 @@ parameters: count: 1 path: ../src/Property/Import.php - - - message: '#^Parameters should have "string" types as the only types passed to this method$#' - identifier: typePerfect.narrowPublicClassMethodParamType - count: 1 - path: ../src/Property/Selector.php - - message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#' identifier: method.notFound diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 9ff7b283..7a66f852 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -246,30 +246,6 @@ public function unicodeRangeParsing(): void public function specificity(): void { $document = self::parsedStructureForFile('specificity'); - $declarationBlocks = $document->getAllDeclarationBlocks(); - $declarationBlock = $declarationBlocks[0]; - $selectors = $declarationBlock->getSelectors(); - foreach ($selectors as $selector) { - switch ($selector->getSelector()) { - case '#test .help': - self::assertSame(110, $selector->getSpecificity()); - break; - case '#file': - self::assertSame(100, $selector->getSpecificity()); - break; - case '.help:hover': - self::assertSame(20, $selector->getSpecificity()); - break; - case 'ol li::before': - self::assertSame(3, $selector->getSpecificity()); - break; - case 'li.green': - self::assertSame(11, $selector->getSpecificity()); - break; - default: - self::fail('specificity: untested selector ' . $selector->getSelector()); - } - } self::assertEquals([new Selector('#test .help', true)], $document->getSelectorsBySpecificity('> 100')); self::assertEquals( [new Selector('#test .help', true), new Selector('#file', true)], diff --git a/tests/Unit/Property/SelectorTest.php b/tests/Unit/Property/SelectorTest.php index d7d5dfc2..0d030d94 100644 --- a/tests/Unit/Property/SelectorTest.php +++ b/tests/Unit/Property/SelectorTest.php @@ -35,4 +35,56 @@ public function setSelectorOverwritesSelectorProvidedToConstructor(): void self::assertSame($selector, $subject->getSelector()); } + + /** + * @return array}> + */ + public static function provideSelectorsAndSpecificities(): array + { + return [ + 'element' => ['a', 1], + 'element and descendant with pseudo-selector' => ['ol li::before', 3], + 'class' => ['.highlighted', 10], + 'element with class' => ['li.green', 11], + 'class with pseudo-selector' => ['.help:hover', 20], + 'ID' => ['#file', 100], + 'ID and descendant class' => ['#test .help', 110], + ]; + } + + /** + * @test + * + * @param non-empty-string $selector + * @param int<0, max> $expectedSpecificity + * + * @dataProvider provideSelectorsAndSpecificities + */ + public function getSpecificityByDefaultReturnsSpecificityOfSelectorProvidedToConstructor( + string $selector, + int $expectedSpecificity + ): void { + $subject = new Selector($selector); + + self::assertSame($expectedSpecificity, $subject->getSpecificity()); + } + + /** + * @test + * + * @param non-empty-string $selector + * @param int<0, max> $expectedSpecificity + * + * @dataProvider provideSelectorsAndSpecificities + */ + public function getSpecificityReturnsSpecificityOfSelectorLastProvidedViaSetSelector( + string $selector, + int $expectedSpecificity + ): void { + $subject = new Selector('p'); + + $subject->setSelector($selector); + + self::assertSame($expectedSpecificity, $subject->getSpecificity()); + } }