Skip to content

Commit 4f1fcf3

Browse files
authored
[TASK] Add unit tests for RuleSet::getRulesAssoc (#1279)
Part of #974.
1 parent 2bbb89d commit 4f1fcf3

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,78 @@ public function getRulesWithPatternOrdersRulesByPosition(): void
10701070
self::assertSame([$first, $second, $third], $result);
10711071
}
10721072

1073+
/**
1074+
* @return array<string, array{0: list<non-empty-string>}>
1075+
*/
1076+
public static function provideDistinctPropertyNames(): array
1077+
{
1078+
return [
1079+
'no properties' => [[]],
1080+
'one property' => [['color']],
1081+
'two properties' => [['color', 'display']],
1082+
];
1083+
}
1084+
1085+
/**
1086+
* @test
1087+
*
1088+
* @param list<string> $propertyNamesToSet
1089+
*
1090+
* @dataProvider provideDistinctPropertyNames
1091+
*/
1092+
public function getRulesAssocReturnsAllRulesWithDistinctPropertyNames(array $propertyNamesToSet): void
1093+
{
1094+
$rulesToSet = self::createRulesFromPropertyNames($propertyNamesToSet);
1095+
$this->subject->setRules($rulesToSet);
1096+
1097+
$result = $this->subject->getRulesAssoc();
1098+
1099+
self::assertSame($rulesToSet, \array_values($result));
1100+
}
1101+
1102+
/**
1103+
* @test
1104+
*/
1105+
public function getRulesAssocReturnsLastRuleWithSamePropertyName(): void
1106+
{
1107+
$firstRule = new Rule('color');
1108+
$lastRule = new Rule('color');
1109+
$this->subject->setRules([$firstRule, $lastRule]);
1110+
1111+
$result = $this->subject->getRulesAssoc();
1112+
1113+
self::assertSame([$lastRule], \array_values($result));
1114+
}
1115+
1116+
/**
1117+
* @test
1118+
*/
1119+
public function getRulesAssocOrdersRulesByPosition(): void
1120+
{
1121+
$first = (new Rule('color'))->setPosition(1, 42);
1122+
$second = (new Rule('display'))->setPosition(1, 64);
1123+
$third = (new Rule('width'))->setPosition(55, 7);
1124+
$this->subject->setRules([$third, $second, $first]);
1125+
1126+
$result = $this->subject->getRulesAssoc();
1127+
1128+
self::assertSame([$first, $second, $third], \array_values($result));
1129+
}
1130+
1131+
/**
1132+
* @test
1133+
*/
1134+
public function getRulesAssocKeysRulesByPropertyName(): void
1135+
{
1136+
$this->subject->setRules([new Rule('color'), new Rule('display')]);
1137+
1138+
$result = $this->subject->getRulesAssoc();
1139+
1140+
foreach ($result as $key => $rule) {
1141+
self::assertSame($rule->getRule(), $key);
1142+
}
1143+
}
1144+
10731145
/**
10741146
* @param list<string> $propertyNames
10751147
*/

0 commit comments

Comments
 (0)