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