From b7c90b1854f1e2d3cd078271c98b8156fe23a4f6 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 19 Jun 2025 16:27:02 +0100 Subject: [PATCH 1/4] [TASK] Add tests for `RuleSet::getRulesAssoc` with `$searchPattern` Part of #974. --- tests/Unit/RuleSet/RuleSetTest.php | 66 ++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/Unit/RuleSet/RuleSetTest.php b/tests/Unit/RuleSet/RuleSetTest.php index 53123466..03225fc6 100644 --- a/tests/Unit/RuleSet/RuleSetTest.php +++ b/tests/Unit/RuleSet/RuleSetTest.php @@ -1165,6 +1165,72 @@ public function getRulesAssocKeysRulesByPropertyName(): void } } + /** + * @test + * + * @param list $propertyNamesToSet + * @param list $matchingPropertyNames + * + * @dataProvider providePropertyNamesAndSearchPatternAndMatchingPropertyNames + */ + public function getRulesAssocWithPatternReturnsAllMatchingPropertyNames( + array $propertyNamesToSet, + string $searchPattern, + array $matchingPropertyNames + ): void { + $this->setRulesFromPropertyNames($propertyNamesToSet); + + $result = $this->subject->getRulesAssoc($searchPattern); + + if ($matchingPropertyNames === []) { + self::assertSame([], $result); + } + foreach ($matchingPropertyNames as $expectedMatchingPropertyName) { + self::assertContains($expectedMatchingPropertyName, \array_keys($result)); + } + } + + /** + * @test + * + * @param list $propertyNamesToSet + * @param list $matchingPropertyNames + * + * @dataProvider providePropertyNamesAndSearchPatternAndMatchingPropertyNames + */ + public function getRulesAssocWithPatternFiltersNonMatchingRules( + array $propertyNamesToSet, + string $searchPattern, + array $matchingPropertyNames + ): void { + $this->setRulesFromPropertyNames($propertyNamesToSet); + + $result = $this->subject->getRulesAssoc($searchPattern); + + if ($result === []) { + self::expectNotToPerformAssertions(); + } + foreach ($result as $resultRule) { + // 'expected' and 'actual' are transposed here due to necessity + self::assertContains($resultRule->getRule(), $matchingPropertyNames); + } + } + + /** + * @test + */ + public function getRulesAssocWithPatternOrdersRulesByPosition(): void + { + $first = (new Rule('font'))->setPosition(1, 42); + $second = (new Rule('font-family'))->setPosition(1, 64); + $third = (new Rule('font-weight'))->setPosition(55, 7); + $this->subject->setRules([$third, $second, $first]); + + $result = $this->subject->getRules('font-'); + + self::assertSame([$first, $second, $third], \array_values($result)); + } + /** * @param list $propertyNames */ From 8c7419cde6f3685fd80e89b27442b3b099b5eb65 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 19 Jun 2025 19:01:55 +0100 Subject: [PATCH 2/4] Add separate test for non-matching patterns --- tests/Unit/RuleSet/RuleSetTest.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/Unit/RuleSet/RuleSetTest.php b/tests/Unit/RuleSet/RuleSetTest.php index 03225fc6..e8b54c07 100644 --- a/tests/Unit/RuleSet/RuleSetTest.php +++ b/tests/Unit/RuleSet/RuleSetTest.php @@ -1182,9 +1182,6 @@ public function getRulesAssocWithPatternReturnsAllMatchingPropertyNames( $result = $this->subject->getRulesAssoc($searchPattern); - if ($matchingPropertyNames === []) { - self::assertSame([], $result); - } foreach ($matchingPropertyNames as $expectedMatchingPropertyName) { self::assertContains($expectedMatchingPropertyName, \array_keys($result)); } @@ -1207,15 +1204,30 @@ public function getRulesAssocWithPatternFiltersNonMatchingRules( $result = $this->subject->getRulesAssoc($searchPattern); - if ($result === []) { - self::expectNotToPerformAssertions(); - } foreach ($result as $resultRule) { // 'expected' and 'actual' are transposed here due to necessity self::assertContains($resultRule->getRule(), $matchingPropertyNames); } } + /** + * @test + * + * @param list $propertyNamesToSet + * + * @dataProvider providePropertyNamesAndNonMatchingSearchPattern + */ + public function getRulesAssocWithNonMatchingPatternReturnsEmptyArray( + array $propertyNamesToSet, + string $searchPattern + ): void { + $this->setRulesFromPropertyNames($propertyNamesToSet); + + $result = $this->subject->getRulesAssoc($searchPattern); + + self::assertSame([], $result); + } + /** * @test */ From a32a6982c9ca4356a0c33ff1a041e54a312bef89 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 19 Jun 2025 22:29:07 +0100 Subject: [PATCH 3/4] Use `assertArrayHasKey()` --- tests/Unit/RuleSet/RuleSetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/RuleSet/RuleSetTest.php b/tests/Unit/RuleSet/RuleSetTest.php index e8b54c07..0341beb4 100644 --- a/tests/Unit/RuleSet/RuleSetTest.php +++ b/tests/Unit/RuleSet/RuleSetTest.php @@ -1183,7 +1183,7 @@ public function getRulesAssocWithPatternReturnsAllMatchingPropertyNames( $result = $this->subject->getRulesAssoc($searchPattern); foreach ($matchingPropertyNames as $expectedMatchingPropertyName) { - self::assertContains($expectedMatchingPropertyName, \array_keys($result)); + self::assertArrayHasKey($expectedMatchingPropertyName, $result); } } From fcca94e4f02132c8f799b9742caad1ea68b0961b Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Sat, 21 Jun 2025 00:03:27 +0100 Subject: [PATCH 4/4] Use single test method check that exactly all property names are matched --- tests/Unit/RuleSet/RuleSetTest.php | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/tests/Unit/RuleSet/RuleSetTest.php b/tests/Unit/RuleSet/RuleSetTest.php index 0341beb4..8127efdb 100644 --- a/tests/Unit/RuleSet/RuleSetTest.php +++ b/tests/Unit/RuleSet/RuleSetTest.php @@ -1182,32 +1182,10 @@ public function getRulesAssocWithPatternReturnsAllMatchingPropertyNames( $result = $this->subject->getRulesAssoc($searchPattern); - foreach ($matchingPropertyNames as $expectedMatchingPropertyName) { - self::assertArrayHasKey($expectedMatchingPropertyName, $result); - } - } - - /** - * @test - * - * @param list $propertyNamesToSet - * @param list $matchingPropertyNames - * - * @dataProvider providePropertyNamesAndSearchPatternAndMatchingPropertyNames - */ - public function getRulesAssocWithPatternFiltersNonMatchingRules( - array $propertyNamesToSet, - string $searchPattern, - array $matchingPropertyNames - ): void { - $this->setRulesFromPropertyNames($propertyNamesToSet); - - $result = $this->subject->getRulesAssoc($searchPattern); - - foreach ($result as $resultRule) { - // 'expected' and 'actual' are transposed here due to necessity - self::assertContains($resultRule->getRule(), $matchingPropertyNames); - } + $resultPropertyNames = \array_keys($result); + \sort($matchingPropertyNames); + \sort($resultPropertyNames); + self::assertSame($matchingPropertyNames, $resultPropertyNames); } /**