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,178 @@ 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 ()
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 string $propertyName
83
+ * @param list<string> $expectedRemainingPropertyNames
84
+ *
85
+ * @dataProvider provideRulesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
86
+ */
87
+ public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers (
88
+ array $ rules ,
89
+ $ propertyName ,
90
+ array $ expectedRemainingPropertyNames
91
+ ) {
92
+ $ subject = new ConcreteRuleSet ();
93
+ $ subject ->setRules ($ rules );
94
+
95
+ $ subject ->removeMatchingRules ($ propertyName );
96
+
97
+ $ remainingRules = $ subject ->getRulesAssoc ();
98
+ self ::assertArrayNotHasKey ($ propertyName , $ remainingRules );
99
+ foreach ($ expectedRemainingPropertyNames as $ expectedPropertyName ) {
100
+ self ::assertArrayHasKey ($ expectedPropertyName , $ remainingRules );
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @return array<string, array{0: list<Rule>, 1: string, 2: list<string>}>
106
+ */
107
+ public static function provideRulesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames ()
108
+ {
109
+ return [
110
+ 'removing shorthand rule ' => [
111
+ [new Rule ('font ' )],
112
+ 'font ' ,
113
+ [],
114
+ ],
115
+ 'removing longhand rule ' => [
116
+ [new Rule ('font-size ' )],
117
+ 'font ' ,
118
+ [],
119
+ ],
120
+ 'removing shorthand and longhand rule ' => [
121
+ [new Rule ('font ' ), new Rule ('font-size ' )],
122
+ 'font ' ,
123
+ [],
124
+ ],
125
+ 'removing shorthand rule with another kept ' => [
126
+ [new Rule ('font ' ), new Rule ('color ' )],
127
+ 'font ' ,
128
+ ['color ' ],
129
+ ],
130
+ 'removing longhand rule with another kept ' => [
131
+ [new Rule ('font-size ' ), new Rule ('color ' )],
132
+ 'font ' ,
133
+ ['color ' ],
134
+ ],
135
+ 'keeping other rules whose property names begin with the same characters ' => [
136
+ [new Rule ('contain ' ), new Rule ('container ' ), new Rule ('container-type ' )],
137
+ 'contain ' ,
138
+ ['container ' , 'container-type ' ],
139
+ ],
140
+ ];
141
+ }
142
+
143
+ /**
144
+ * @test
145
+ *
146
+ * @param list<Rule> $rules
147
+ * @param string $propertyNamePrefix
148
+ * @param list<string> $expectedRemainingPropertyNames
149
+ *
150
+ * @dataProvider provideRulesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
151
+ */
152
+ public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOthers (
153
+ array $ rules ,
154
+ $ propertyNamePrefix ,
155
+ array $ expectedRemainingPropertyNames
156
+ ) {
157
+ $ propertyNamePrefixWithHyphen = $ propertyNamePrefix . '- ' ;
158
+ $ subject = new ConcreteRuleSet ();
159
+ $ subject ->setRules ($ rules );
160
+
161
+ $ subject ->removeMatchingRules ($ propertyNamePrefixWithHyphen );
162
+
163
+ $ remainingRules = $ subject ->getRulesAssoc ();
164
+ self ::assertArrayNotHasKey ($ propertyNamePrefix , $ remainingRules );
165
+ foreach (\array_keys ($ remainingRules ) as $ remainingPropertyName ) {
166
+ self ::assertStringStartsNotWith ($ propertyNamePrefixWithHyphen , $ remainingPropertyName );
167
+ }
168
+ foreach ($ expectedRemainingPropertyNames as $ expectedPropertyName ) {
169
+ self ::assertArrayHasKey ($ expectedPropertyName , $ remainingRules );
170
+ }
171
+ }
172
+
173
+ /**
174
+ * @return array<string, array{0: list<Rule>}>
175
+ */
176
+ public static function provideRulesToRemove ()
177
+ {
178
+ return [
179
+ 'no rules ' => [[]],
180
+ 'one rule ' => [[new Rule ('color ' )]],
181
+ 'two rules for different properties ' => [[new Rule ('color ' ), new Rule ('display ' )]],
182
+ 'two rules for the same property ' => [[new Rule ('color ' ), new Rule ('color ' )]],
183
+ ];
184
+ }
185
+
186
+ /**
187
+ * @test
188
+ *
189
+ * @param list<Rule> $rules
190
+ *
191
+ * @dataProvider provideRulesToRemove
192
+ */
193
+ public function removeAllRulesRemovesAllRules (array $ rules )
194
+ {
195
+ $ subject = new ConcreteRuleSet ();
196
+ $ subject ->setRules ($ rules );
197
+
198
+ $ subject ->removeAllRules ();
199
+
200
+ self ::assertSame ([], $ subject ->getRules ());
201
+ }
27
202
}
0 commit comments