|
7 | 7 | use PHPUnit\Framework\TestCase;
|
8 | 8 | use Sabberworm\CSS\CSSElement;
|
9 | 9 | use Sabberworm\CSS\CSSList\CSSListItem;
|
| 10 | +use Sabberworm\CSS\Rule\Rule; |
10 | 11 | use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
|
11 | 12 |
|
12 | 13 | /**
|
@@ -39,4 +40,173 @@ public function implementsCSSListItem(): void
|
39 | 40 | {
|
40 | 41 | self::assertInstanceOf(CSSListItem::class, $this->subject);
|
41 | 42 | }
|
| 43 | + |
| 44 | + /** |
| 45 | + * @return array<string, array{0: list<Rule>, 1: string, 2: list<string>}> |
| 46 | + */ |
| 47 | + public static function provideRulesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames(): array |
| 48 | + { |
| 49 | + return [ |
| 50 | + 'removing single rule' => [ |
| 51 | + [new Rule('color')], |
| 52 | + 'color', |
| 53 | + [], |
| 54 | + ], |
| 55 | + 'removing first rule' => [ |
| 56 | + [new Rule('color'), new Rule('display')], |
| 57 | + 'color', |
| 58 | + ['display'], |
| 59 | + ], |
| 60 | + 'removing last rule' => [ |
| 61 | + [new Rule('color'), new Rule('display')], |
| 62 | + 'display', |
| 63 | + ['color'], |
| 64 | + ], |
| 65 | + 'removing middle rule' => [ |
| 66 | + [new Rule('color'), new Rule('display'), new Rule('width')], |
| 67 | + 'display', |
| 68 | + ['color', 'width'], |
| 69 | + ], |
| 70 | + 'removing multiple rules' => [ |
| 71 | + [new Rule('color'), new Rule('color')], |
| 72 | + 'color', |
| 73 | + [], |
| 74 | + ], |
| 75 | + 'removing multiple rules with another kept' => [ |
| 76 | + [new Rule('color'), new Rule('color'), new Rule('display')], |
| 77 | + 'color', |
| 78 | + ['display'], |
| 79 | + ], |
| 80 | + 'removing nonexistent rule from empty list' => [ |
| 81 | + [], |
| 82 | + 'color', |
| 83 | + [], |
| 84 | + ], |
| 85 | + 'removing nonexistent rule from nonempty list' => [ |
| 86 | + [new Rule('color'), new Rule('display')], |
| 87 | + 'width', |
| 88 | + ['color', 'display'], |
| 89 | + ], |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @test |
| 95 | + * |
| 96 | + * @param list<Rule> $rules |
| 97 | + * @param list<string> $expectedRemainingPropertyNames |
| 98 | + * |
| 99 | + * @dataProvider provideRulesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames |
| 100 | + */ |
| 101 | + public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers( |
| 102 | + array $rules, |
| 103 | + string $propertyName, |
| 104 | + array $expectedRemainingPropertyNames |
| 105 | + ): void { |
| 106 | + $this->subject->setRules($rules); |
| 107 | + |
| 108 | + $this->subject->removeMatchingRules($propertyName); |
| 109 | + |
| 110 | + $remainingRules = $this->subject->getRulesAssoc(); |
| 111 | + self::assertArrayNotHasKey($propertyName, $remainingRules); |
| 112 | + foreach ($expectedRemainingPropertyNames as $expectedPropertyName) { |
| 113 | + self::assertArrayHasKey($expectedPropertyName, $remainingRules); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return array<string, array{0: list<Rule>, 1: string, 2: list<string>}> |
| 119 | + */ |
| 120 | + public static function provideRulesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames(): array |
| 121 | + { |
| 122 | + return [ |
| 123 | + 'removing shorthand rule' => [ |
| 124 | + [new Rule('font')], |
| 125 | + 'font', |
| 126 | + [], |
| 127 | + ], |
| 128 | + 'removing longhand rule' => [ |
| 129 | + [new Rule('font-size')], |
| 130 | + 'font', |
| 131 | + [], |
| 132 | + ], |
| 133 | + 'removing shorthand and longhand rule' => [ |
| 134 | + [new Rule('font'), new Rule('font-size')], |
| 135 | + 'font', |
| 136 | + [], |
| 137 | + ], |
| 138 | + 'removing shorthand rule with another kept' => [ |
| 139 | + [new Rule('font'), new Rule('color')], |
| 140 | + 'font', |
| 141 | + ['color'], |
| 142 | + ], |
| 143 | + 'removing longhand rule with another kept' => [ |
| 144 | + [new Rule('font-size'), new Rule('color')], |
| 145 | + 'font', |
| 146 | + ['color'], |
| 147 | + ], |
| 148 | + 'keeping other rules whose property names begin with the same characters' => [ |
| 149 | + [new Rule('contain'), new Rule('container'), new Rule('container-type')], |
| 150 | + 'contain', |
| 151 | + ['container', 'container-type'], |
| 152 | + ], |
| 153 | + ]; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @test |
| 158 | + * |
| 159 | + * @param list<Rule> $rules |
| 160 | + * @param list<string> $expectedRemainingPropertyNames |
| 161 | + * |
| 162 | + * @dataProvider provideRulesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames |
| 163 | + */ |
| 164 | + public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOthers( |
| 165 | + array $rules, |
| 166 | + string $propertyNamePrefix, |
| 167 | + array $expectedRemainingPropertyNames |
| 168 | + ): void { |
| 169 | + $propertyNamePrefixWithHyphen = $propertyNamePrefix . '-'; |
| 170 | + $this->subject->setRules($rules); |
| 171 | + |
| 172 | + $this->subject->removeMatchingRules($propertyNamePrefixWithHyphen); |
| 173 | + |
| 174 | + $remainingRules = $this->subject->getRulesAssoc(); |
| 175 | + self::assertArrayNotHasKey($propertyNamePrefix, $remainingRules); |
| 176 | + foreach (\array_keys($remainingRules) as $remainingPropertyName) { |
| 177 | + self::assertStringStartsNotWith($propertyNamePrefixWithHyphen, $remainingPropertyName); |
| 178 | + } |
| 179 | + foreach ($expectedRemainingPropertyNames as $expectedPropertyName) { |
| 180 | + self::assertArrayHasKey($expectedPropertyName, $remainingRules); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @return array<string, array{0: list<Rule>}> |
| 186 | + */ |
| 187 | + public static function provideRulesToRemove(): array |
| 188 | + { |
| 189 | + return [ |
| 190 | + 'no rules' => [[]], |
| 191 | + 'one rule' => [[new Rule('color')]], |
| 192 | + 'two rules for different properties' => [[new Rule('color'), new Rule('display')]], |
| 193 | + 'two rules for the same property' => [[new Rule('color'), new Rule('color')]], |
| 194 | + ]; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @test |
| 199 | + * |
| 200 | + * @param list<Rule> $rules |
| 201 | + * |
| 202 | + * @dataProvider provideRulesToRemove |
| 203 | + */ |
| 204 | + public function removeAllRulesRemovesAllRules(array $rules): void |
| 205 | + { |
| 206 | + $this->subject->setRules($rules); |
| 207 | + |
| 208 | + $this->subject->removeAllRules(); |
| 209 | + |
| 210 | + self::assertSame([], $this->subject->getRules()); |
| 211 | + } |
42 | 212 | }
|
0 commit comments