-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathRuleSetTest.php
More file actions
348 lines (313 loc) · 11.2 KB
/
Copy pathRuleSetTest.php
File metadata and controls
348 lines (313 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
declare(strict_types=1);
namespace Sabberworm\CSS\Tests\Unit\RuleSet;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\CSSElement;
use Sabberworm\CSS\CSSList\CSSListItem;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\RuleSet\RuleContainer;
use Sabberworm\CSS\Tests\Unit\RuleSet\Fixtures\ConcreteRuleSet;
use TRegx\PhpUnit\DataProviders\DataProvider;
/**
* @covers \Sabberworm\CSS\RuleSet\RuleSet
*/
final class RuleSetTest extends TestCase
{
/**
* @var ConcreteRuleSet
*/
private $subject;
protected function setUp(): void
{
$this->subject = new ConcreteRuleSet();
}
/**
* @test
*/
public function implementsCSSElement(): void
{
self::assertInstanceOf(CSSElement::class, $this->subject);
}
/**
* @test
*/
public function implementsCSSListItem(): void
{
self::assertInstanceOf(CSSListItem::class, $this->subject);
}
/**
* @test
*/
public function implementsRuleContainer(): void
{
self::assertInstanceOf(RuleContainer::class, $this->subject);
}
/**
* @return array<string, array{0: list<non-empty-string>}>
*/
public static function providePropertyNamesToBeSetInitially(): array
{
return [
'no properties' => [[]],
'one property' => [['color']],
'two different properties' => [['color', 'display']],
'two of the same property' => [['color', 'color']],
];
}
/**
* @return array<string, array{0: non-empty-string}>
*/
public static function providePropertyNameToAdd(): array
{
return [
'property name `color` maybe matching that of existing declaration' => ['color'],
'property name `display` maybe matching that of existing declaration' => ['display'],
'property name `width` not matching that of existing declaration' => ['width'],
];
}
/**
* @return DataProvider<string, array{0: list<string>, 1: string}>
*/
public static function provideInitialPropertyNamesAndPropertyNameToAdd(): DataProvider
{
return DataProvider::cross(self::providePropertyNamesToBeSetInitially(), self::providePropertyNameToAdd());
}
/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
*/
public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->addRule($ruleToAdd);
$rules = $this->subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
}
/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLineNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42);
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->addRule($ruleToAdd);
self::assertContains($ruleToAdd, $this->subject->getRules());
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
}
/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineNumberPreservingColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(null, 42);
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->addRule($ruleToAdd);
$rules = $this->subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
self::assertSame(42, $ruleToAdd->getColumnNumber(), 'column number not preserved');
}
/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42, 64);
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->addRule($ruleToAdd);
self::assertContains($ruleToAdd, $this->subject->getRules());
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
self::assertSame(64, $ruleToAdd->getColumnNumber(), 'column number not preserved');
}
/**
* @return array<string, array{0: list<string>, 1: string, 2: list<string>}>
*/
public static function providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames(): array
{
return [
'removing single rule' => [
['color'],
'color',
[],
],
'removing first rule' => [
['color', 'display'],
'color',
['display'],
],
'removing last rule' => [
['color', 'display'],
'display',
['color'],
],
'removing middle rule' => [
['color', 'display', 'width'],
'display',
['color', 'width'],
],
'removing multiple rules' => [
['color', 'color'],
'color',
[],
],
'removing multiple rules with another kept' => [
['color', 'color', 'display'],
'color',
['display'],
],
'removing nonexistent rule from empty list' => [
[],
'color',
[],
],
'removing nonexistent rule from nonempty list' => [
['color', 'display'],
'width',
['color', 'display'],
],
];
}
/**
* @test
*
* @param list<string> $initialPropertyNames
* @param list<string> $expectedRemainingPropertyNames
*
* @dataProvider providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers(
array $initialPropertyNames,
string $propertyNameToRemove,
array $expectedRemainingPropertyNames
): void {
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->removeMatchingRules($propertyNameToRemove);
$remainingRules = $this->subject->getRulesAssoc();
self::assertArrayNotHasKey($propertyNameToRemove, $remainingRules);
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
}
}
/**
* @return array<string, array{0: list<string>, 1: string, 2: list<string>}>
*/
public static function providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames(): array
{
return [
'removing shorthand rule' => [
['font'],
'font',
[],
],
'removing longhand rule' => [
['font-size'],
'font',
[],
],
'removing shorthand and longhand rule' => [
['font', 'font-size'],
'font',
[],
],
'removing shorthand rule with another kept' => [
['font', 'color'],
'font',
['color'],
],
'removing longhand rule with another kept' => [
['font-size', 'color'],
'font',
['color'],
],
'keeping other rules whose property names begin with the same characters' => [
['contain', 'container', 'container-type'],
'contain',
['container', 'container-type'],
],
];
}
/**
* @test
*
* @param list<string> $initialPropertyNames
* @param list<string> $expectedRemainingPropertyNames
*
* @dataProvider providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOthers(
array $initialPropertyNames,
string $propertyNamePrefix,
array $expectedRemainingPropertyNames
): void {
$propertyNamePrefixWithHyphen = $propertyNamePrefix . '-';
$this->setRulesFromPropertyNames($initialPropertyNames);
$this->subject->removeMatchingRules($propertyNamePrefixWithHyphen);
$remainingRules = $this->subject->getRulesAssoc();
self::assertArrayNotHasKey($propertyNamePrefix, $remainingRules);
foreach (\array_keys($remainingRules) as $remainingPropertyName) {
self::assertStringStartsNotWith($propertyNamePrefixWithHyphen, $remainingPropertyName);
}
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
}
}
/**
* @test
*
* @param list<string> $propertyNamesToRemove
*
* @dataProvider providePropertyNamesToBeSetInitially
*/
public function removeAllRulesRemovesAllRules(array $propertyNamesToRemove): void
{
$this->setRulesFromPropertyNames($propertyNamesToRemove);
$this->subject->removeAllRules();
self::assertSame([], $this->subject->getRules());
}
/**
* @param list<string> $propertyNames
*/
private function setRulesFromPropertyNames(array $propertyNames): void
{
$this->subject->setRules(\array_map(
static function (string $propertyName): Rule {
return new Rule($propertyName);
},
$propertyNames
));
}
}