Skip to content

Commit 7ef82db

Browse files
authored
[TASK] Add tests for the selector specificity (#1025)
Mostly move tests from `ParserTest` that belong in the unit tests. Part of #757 Part of #758
1 parent 2093b19 commit 7ef82db

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

config/phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ parameters:
222222
count: 1
223223
path: ../src/Property/Import.php
224224

225-
-
226-
message: '#^Parameters should have "string" types as the only types passed to this method$#'
227-
identifier: typePerfect.narrowPublicClassMethodParamType
228-
count: 1
229-
path: ../src/Property/Selector.php
230-
231225
-
232226
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
233227
identifier: method.notFound

tests/ParserTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -246,30 +246,6 @@ public function unicodeRangeParsing(): void
246246
public function specificity(): void
247247
{
248248
$document = self::parsedStructureForFile('specificity');
249-
$declarationBlocks = $document->getAllDeclarationBlocks();
250-
$declarationBlock = $declarationBlocks[0];
251-
$selectors = $declarationBlock->getSelectors();
252-
foreach ($selectors as $selector) {
253-
switch ($selector->getSelector()) {
254-
case '#test .help':
255-
self::assertSame(110, $selector->getSpecificity());
256-
break;
257-
case '#file':
258-
self::assertSame(100, $selector->getSpecificity());
259-
break;
260-
case '.help:hover':
261-
self::assertSame(20, $selector->getSpecificity());
262-
break;
263-
case 'ol li::before':
264-
self::assertSame(3, $selector->getSpecificity());
265-
break;
266-
case 'li.green':
267-
self::assertSame(11, $selector->getSpecificity());
268-
break;
269-
default:
270-
self::fail('specificity: untested selector ' . $selector->getSelector());
271-
}
272-
}
273249
self::assertEquals([new Selector('#test .help', true)], $document->getSelectorsBySpecificity('> 100'));
274250
self::assertEquals(
275251
[new Selector('#test .help', true), new Selector('#file', true)],

tests/Unit/Property/SelectorTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,56 @@ public function setSelectorOverwritesSelectorProvidedToConstructor(): void
3535

3636
self::assertSame($selector, $subject->getSelector());
3737
}
38+
39+
/**
40+
* @return array<string, array{0: non-empty-string, 1: int<0, max>}>
41+
*/
42+
public static function provideSelectorsAndSpecificities(): array
43+
{
44+
return [
45+
'element' => ['a', 1],
46+
'element and descendant with pseudo-selector' => ['ol li::before', 3],
47+
'class' => ['.highlighted', 10],
48+
'element with class' => ['li.green', 11],
49+
'class with pseudo-selector' => ['.help:hover', 20],
50+
'ID' => ['#file', 100],
51+
'ID and descendant class' => ['#test .help', 110],
52+
];
53+
}
54+
55+
/**
56+
* @test
57+
*
58+
* @param non-empty-string $selector
59+
* @param int<0, max> $expectedSpecificity
60+
*
61+
* @dataProvider provideSelectorsAndSpecificities
62+
*/
63+
public function getSpecificityByDefaultReturnsSpecificityOfSelectorProvidedToConstructor(
64+
string $selector,
65+
int $expectedSpecificity
66+
): void {
67+
$subject = new Selector($selector);
68+
69+
self::assertSame($expectedSpecificity, $subject->getSpecificity());
70+
}
71+
72+
/**
73+
* @test
74+
*
75+
* @param non-empty-string $selector
76+
* @param int<0, max> $expectedSpecificity
77+
*
78+
* @dataProvider provideSelectorsAndSpecificities
79+
*/
80+
public function getSpecificityReturnsSpecificityOfSelectorLastProvidedViaSetSelector(
81+
string $selector,
82+
int $expectedSpecificity
83+
): void {
84+
$subject = new Selector('p');
85+
86+
$subject->setSelector($selector);
87+
88+
self::assertSame($expectedSpecificity, $subject->getSpecificity());
89+
}
3890
}

0 commit comments

Comments
 (0)