Skip to content

Commit f9f68b6

Browse files
committed
[TASK] Add tests for RuleSet::getRules with $searchPattern
Part of #974.
1 parent 2433e6f commit f9f68b6

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,163 @@ public function getRulesOrdersRulesWithSameLineNumberByColumnNumber(): void
913913
self::assertSame([$first, $second, $third], $result);
914914
}
915915

916+
/**
917+
* @return array<string, array{0: list<string>, 1: string, 2: list<string>}>
918+
*/
919+
public static function providePropertyNamesAndSearchPatternAndMatchingPropertyNames(): array
920+
{
921+
return [
922+
'single rule matched' => [
923+
['color'],
924+
'color',
925+
['color'],
926+
],
927+
'first rule matched' => [
928+
['color', 'display'],
929+
'color',
930+
['color'],
931+
],
932+
'last rule matched' => [
933+
['color', 'display'],
934+
'display',
935+
['display'],
936+
],
937+
'middle rule matched' => [
938+
['color', 'display', 'width'],
939+
'display',
940+
['display'],
941+
],
942+
'multiple rules for the same property matched' => [
943+
['color', 'color'],
944+
'color',
945+
['color'],
946+
],
947+
'multiple rules for the same property matched in haystack' => [
948+
['color', 'display', 'color', 'width'],
949+
'color',
950+
['color'],
951+
],
952+
'no match in empty list' => [
953+
[],
954+
'color',
955+
[],
956+
],
957+
'no match for property not in list' => [
958+
['color', 'display'],
959+
'width',
960+
[],
961+
],
962+
'shorthand rule matched' => [
963+
['font'],
964+
'font-',
965+
['font'],
966+
],
967+
'longhand rule matched' => [
968+
['font-size'],
969+
'font-',
970+
['font-size'],
971+
],
972+
'shorthand and longhand rule matched' => [
973+
['font', 'font-size'],
974+
'font-',
975+
['font', 'font-size'],
976+
],
977+
'shorthand rule matched in haystack' => [
978+
['font', 'color'],
979+
'font-',
980+
['font'],
981+
],
982+
'longhand rule matched in haystack' => [
983+
['font-size', 'color'],
984+
'font-',
985+
['font-size'],
986+
],
987+
'rules whose property names begin with the same characters not matched with pattern match' => [
988+
['contain', 'container', 'container-type'],
989+
'contain-',
990+
['contain'],
991+
],
992+
'rules whose property names begin with the same characters not matched with exact match' => [
993+
['contain', 'container', 'container-type'],
994+
'contain',
995+
['contain'],
996+
],
997+
];
998+
}
999+
1000+
/**
1001+
* @test
1002+
*
1003+
* @param list<string> $propertyNamesToSet
1004+
* @param list<string> $matchingPropertyNames
1005+
*
1006+
* @dataProvider providePropertyNamesAndSearchPatternAndMatchingPropertyNames
1007+
*/
1008+
public function getRulesWithPatternReturnsAllMatchingRules(
1009+
array $propertyNamesToSet,
1010+
string $searchPattern,
1011+
array $matchingPropertyNames
1012+
): void {
1013+
$rulesToSet = self::createRulesFromPropertyNames($propertyNamesToSet);
1014+
$matchingRules = \array_filter(
1015+
$rulesToSet,
1016+
static function (Rule $rule) use ($matchingPropertyNames): bool {
1017+
return \in_array($rule->getRule(), $matchingPropertyNames, true);
1018+
}
1019+
);
1020+
$this->subject->setRules($rulesToSet);
1021+
1022+
$result = $this->subject->getRules($searchPattern);
1023+
1024+
if ($matchingRules === []) {
1025+
self::assertSame([], $result);
1026+
}
1027+
foreach ($matchingRules as $expectedMatchingRule) {
1028+
self::assertContains($expectedMatchingRule, $result);
1029+
}
1030+
}
1031+
1032+
/**
1033+
* @test
1034+
*
1035+
* @param list<string> $propertyNamesToSet
1036+
* @param list<string> $matchingPropertyNames
1037+
*
1038+
* @dataProvider providePropertyNamesAndSearchPatternAndMatchingPropertyNames
1039+
*/
1040+
public function getRulesWithPatternFiltersNonMatchingRules(
1041+
array $propertyNamesToSet,
1042+
string $searchPattern,
1043+
array $matchingPropertyNames
1044+
): void {
1045+
$this->setRulesFromPropertyNames($propertyNamesToSet);
1046+
1047+
$result = $this->subject->getRules($searchPattern);
1048+
1049+
if ($result === []) {
1050+
self::expectNotToPerformAssertions();
1051+
}
1052+
foreach ($result as $resultRule) {
1053+
// 'expected' and 'actual' are transposed here due to necessity
1054+
self::assertContains($resultRule->getRule(), $matchingPropertyNames);
1055+
}
1056+
}
1057+
1058+
/**
1059+
* @test
1060+
*/
1061+
public function getRulesWithPatternOrdersRulesByPosition(): void
1062+
{
1063+
$first = (new Rule('color'))->setPosition(1, 42);
1064+
$second = (new Rule('color'))->setPosition(1, 64);
1065+
$third = (new Rule('color'))->setPosition(55, 7);
1066+
$this->subject->setRules([$third, $second, $first]);
1067+
1068+
$result = $this->subject->getRules('color');
1069+
1070+
self::assertSame([$first, $second, $third], $result);
1071+
}
1072+
9161073
/**
9171074
* @param list<string> $propertyNames
9181075
*/

0 commit comments

Comments
 (0)