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