Skip to content

Commit 601b706

Browse files
authored
[CLEANUP] Separate some test methods in RuleSetTest (#1274)
These were previously testing more than one aspect of a `RuleSet` method. Now each behaviour has a dedicated test method (albeit with some duplication of the set-up). Also added an additional assertion when there are no expected remaining items following removal, so that an assertion is made.
1 parent d13d953 commit 601b706

File tree

1 file changed

+224
-11
lines changed

1 file changed

+224
-11
lines changed

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 224 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function provideInitialPropertyNamesAndAnotherPropertyName(): Data
9191
*
9292
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
9393
*/
94-
public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
94+
public function addRuleWithoutPositionWithoutSiblingAddsRuleAfterInitialRules(
9595
array $initialPropertyNames,
9696
string $propertyNameToAdd
9797
): void {
@@ -102,8 +102,44 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn
102102

103103
$rules = $this->subject->getRules();
104104
self::assertSame($ruleToAdd, \end($rules));
105+
}
106+
107+
/**
108+
* @test
109+
*
110+
* @param list<string> $initialPropertyNames
111+
*
112+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
113+
*/
114+
public function addRuleWithoutPositionWithoutSiblingSetsValidLineNumber(
115+
array $initialPropertyNames,
116+
string $propertyNameToAdd
117+
): void {
118+
$ruleToAdd = new Rule($propertyNameToAdd);
119+
$this->setRulesFromPropertyNames($initialPropertyNames);
120+
121+
$this->subject->addRule($ruleToAdd);
122+
105123
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
106124
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
125+
}
126+
127+
/**
128+
* @test
129+
*
130+
* @param list<string> $initialPropertyNames
131+
*
132+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
133+
*/
134+
public function addRuleWithoutPositionWithoutSiblingSetsValidColumnNumber(
135+
array $initialPropertyNames,
136+
string $propertyNameToAdd
137+
): void {
138+
$ruleToAdd = new Rule($propertyNameToAdd);
139+
$this->setRulesFromPropertyNames($initialPropertyNames);
140+
141+
$this->subject->addRule($ruleToAdd);
142+
107143
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
108144
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
109145
}
@@ -115,7 +151,7 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn
115151
*
116152
* @param list<string> $initialPropertyNames
117153
*/
118-
public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLineNumber(
154+
public function addRuleWithOnlyLineNumberWithoutSiblingAddsRule(
119155
array $initialPropertyNames,
120156
string $propertyNameToAdd
121157
): void {
@@ -126,8 +162,46 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
126162
$this->subject->addRule($ruleToAdd);
127163

128164
self::assertContains($ruleToAdd, $this->subject->getRules());
165+
}
166+
167+
/**
168+
* @test
169+
*
170+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
171+
*
172+
* @param list<string> $initialPropertyNames
173+
*/
174+
public function addRuleWithOnlyLineNumberWithoutSiblingSetsColumnNumber(
175+
array $initialPropertyNames,
176+
string $propertyNameToAdd
177+
): void {
178+
$ruleToAdd = new Rule($propertyNameToAdd);
179+
$ruleToAdd->setPosition(42);
180+
$this->setRulesFromPropertyNames($initialPropertyNames);
181+
182+
$this->subject->addRule($ruleToAdd);
183+
129184
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
130185
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
186+
}
187+
188+
/**
189+
* @test
190+
*
191+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
192+
*
193+
* @param list<string> $initialPropertyNames
194+
*/
195+
public function addRuleWithOnlyLineNumberWithoutSiblingPreservesLineNumber(
196+
array $initialPropertyNames,
197+
string $propertyNameToAdd
198+
): void {
199+
$ruleToAdd = new Rule($propertyNameToAdd);
200+
$ruleToAdd->setPosition(42);
201+
$this->setRulesFromPropertyNames($initialPropertyNames);
202+
203+
$this->subject->addRule($ruleToAdd);
204+
131205
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
132206
}
133207

@@ -138,7 +212,7 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
138212
*
139213
* @param list<string> $initialPropertyNames
140214
*/
141-
public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineNumberPreservingColumnNumber(
215+
public function addRuleWithOnlyColumnNumberWithoutSiblingAddsRuleAfterInitialRules(
142216
array $initialPropertyNames,
143217
string $propertyNameToAdd
144218
): void {
@@ -150,8 +224,46 @@ public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineN
150224

151225
$rules = $this->subject->getRules();
152226
self::assertSame($ruleToAdd, \end($rules));
227+
}
228+
229+
/**
230+
* @test
231+
*
232+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
233+
*
234+
* @param list<string> $initialPropertyNames
235+
*/
236+
public function addRuleWithOnlyColumnNumberWithoutSiblingSetsLineNumber(
237+
array $initialPropertyNames,
238+
string $propertyNameToAdd
239+
): void {
240+
$ruleToAdd = new Rule($propertyNameToAdd);
241+
$ruleToAdd->setPosition(null, 42);
242+
$this->setRulesFromPropertyNames($initialPropertyNames);
243+
244+
$this->subject->addRule($ruleToAdd);
245+
153246
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
154247
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
248+
}
249+
250+
/**
251+
* @test
252+
*
253+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
254+
*
255+
* @param list<string> $initialPropertyNames
256+
*/
257+
public function addRuleWithOnlyColumnNumberWithoutSiblingPreservesColumnNumber(
258+
array $initialPropertyNames,
259+
string $propertyNameToAdd
260+
): void {
261+
$ruleToAdd = new Rule($propertyNameToAdd);
262+
$ruleToAdd->setPosition(null, 42);
263+
$this->setRulesFromPropertyNames($initialPropertyNames);
264+
265+
$this->subject->addRule($ruleToAdd);
266+
155267
self::assertSame(42, $ruleToAdd->getColumnNumber(), 'column number not preserved');
156268
}
157269

@@ -162,7 +274,7 @@ public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineN
162274
*
163275
* @param list<string> $initialPropertyNames
164276
*/
165-
public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
277+
public function addRuleWithCompletePositionWithoutSiblingAddsRule(
166278
array $initialPropertyNames,
167279
string $propertyNameToAdd
168280
): void {
@@ -173,6 +285,25 @@ public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
173285
$this->subject->addRule($ruleToAdd);
174286

175287
self::assertContains($ruleToAdd, $this->subject->getRules());
288+
}
289+
290+
/**
291+
* @test
292+
*
293+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
294+
*
295+
* @param list<string> $initialPropertyNames
296+
*/
297+
public function addRuleWithCompletePositionWithoutSiblingPreservesPosition(
298+
array $initialPropertyNames,
299+
string $propertyNameToAdd
300+
): void {
301+
$ruleToAdd = new Rule($propertyNameToAdd);
302+
$ruleToAdd->setPosition(42, 64);
303+
$this->setRulesFromPropertyNames($initialPropertyNames);
304+
305+
$this->subject->addRule($ruleToAdd);
306+
176307
self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
177308
self::assertSame(64, $ruleToAdd->getColumnNumber(), 'column number not preserved');
178309
}
@@ -286,7 +417,7 @@ public function addRuleWithSiblingSetsValidColumnNumber(
286417
*
287418
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
288419
*/
289-
public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
420+
public function addRuleWithSiblingNotInSetAddsRuleAfterInitialRules(
290421
array $initialPropertyNames,
291422
string $propertyNameToAdd
292423
): void {
@@ -299,8 +430,48 @@ public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsVa
299430

300431
$rules = $this->subject->getRules();
301432
self::assertSame($ruleToAdd, \end($rules));
433+
}
434+
435+
/**
436+
* @test
437+
*
438+
* @param list<string> $initialPropertyNames
439+
*
440+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
441+
*/
442+
public function addRuleWithSiblingNotInSetSetsValidLineNumber(
443+
array $initialPropertyNames,
444+
string $propertyNameToAdd
445+
): void {
446+
$ruleToAdd = new Rule($propertyNameToAdd);
447+
$this->setRulesFromPropertyNames($initialPropertyNames);
448+
449+
// `display` is sometimes in `$initialPropertyNames` and sometimes the `$propertyNameToAdd`.
450+
// Choosing this for the bogus sibling allows testing all combinations of whether it is or isn't.
451+
$this->subject->addRule($ruleToAdd, new Rule('display'));
452+
302453
self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
303454
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
455+
}
456+
457+
/**
458+
* @test
459+
*
460+
* @param list<string> $initialPropertyNames
461+
*
462+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
463+
*/
464+
public function addRuleWithSiblingNotInSetSetsValidColumnNumber(
465+
array $initialPropertyNames,
466+
string $propertyNameToAdd
467+
): void {
468+
$ruleToAdd = new Rule($propertyNameToAdd);
469+
$this->setRulesFromPropertyNames($initialPropertyNames);
470+
471+
// `display` is sometimes in `$initialPropertyNames` and sometimes the `$propertyNameToAdd`.
472+
// Choosing this for the bogus sibling allows testing all combinations of whether it is or isn't.
473+
$this->subject->addRule($ruleToAdd, new Rule('display'));
474+
304475
self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
305476
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
306477
}
@@ -410,6 +581,24 @@ public static function providePropertyNamesAndPropertyNameToRemoveAndExpectedRem
410581
];
411582
}
412583

584+
/**
585+
* @test
586+
*
587+
* @param list<string> $initialPropertyNames
588+
*
589+
* @dataProvider providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
590+
*/
591+
public function removeMatchingRulesRemovesRulesWithPropertyName(
592+
array $initialPropertyNames,
593+
string $propertyNameToRemove
594+
): void {
595+
$this->setRulesFromPropertyNames($initialPropertyNames);
596+
597+
$this->subject->removeMatchingRules($propertyNameToRemove);
598+
599+
self::assertArrayNotHasKey($propertyNameToRemove, $this->subject->getRulesAssoc());
600+
}
601+
413602
/**
414603
* @test
415604
*
@@ -418,7 +607,7 @@ public static function providePropertyNamesAndPropertyNameToRemoveAndExpectedRem
418607
*
419608
* @dataProvider providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
420609
*/
421-
public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers(
610+
public function removeMatchingRulesWithPropertyNameKeepsOtherRules(
422611
array $initialPropertyNames,
423612
string $propertyNameToRemove,
424613
array $expectedRemainingPropertyNames
@@ -428,7 +617,9 @@ public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers(
428617
$this->subject->removeMatchingRules($propertyNameToRemove);
429618

430619
$remainingRules = $this->subject->getRulesAssoc();
431-
self::assertArrayNotHasKey($propertyNameToRemove, $remainingRules);
620+
if ($expectedRemainingPropertyNames === []) {
621+
self::assertSame([], $remainingRules);
622+
}
432623
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
433624
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
434625
}
@@ -477,14 +668,12 @@ public static function providePropertyNamesAndPropertyNamePrefixToRemoveAndExpec
477668
* @test
478669
*
479670
* @param list<string> $initialPropertyNames
480-
* @param list<string> $expectedRemainingPropertyNames
481671
*
482672
* @dataProvider providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
483673
*/
484-
public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOthers(
674+
public function removeMatchingRulesRemovesRulesWithPropertyNamePrefix(
485675
array $initialPropertyNames,
486-
string $propertyNamePrefix,
487-
array $expectedRemainingPropertyNames
676+
string $propertyNamePrefix
488677
): void {
489678
$propertyNamePrefixWithHyphen = $propertyNamePrefix . '-';
490679
$this->setRulesFromPropertyNames($initialPropertyNames);
@@ -496,6 +685,30 @@ public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOther
496685
foreach (\array_keys($remainingRules) as $remainingPropertyName) {
497686
self::assertStringStartsNotWith($propertyNamePrefixWithHyphen, $remainingPropertyName);
498687
}
688+
}
689+
690+
/**
691+
* @test
692+
*
693+
* @param list<string> $initialPropertyNames
694+
* @param list<string> $expectedRemainingPropertyNames
695+
*
696+
* @dataProvider providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
697+
*/
698+
public function removeMatchingRulesWithPropertyNamePrefixKeepsOtherRules(
699+
array $initialPropertyNames,
700+
string $propertyNamePrefix,
701+
array $expectedRemainingPropertyNames
702+
): void {
703+
$propertyNamePrefixWithHyphen = $propertyNamePrefix . '-';
704+
$this->setRulesFromPropertyNames($initialPropertyNames);
705+
706+
$this->subject->removeMatchingRules($propertyNamePrefixWithHyphen);
707+
708+
$remainingRules = $this->subject->getRulesAssoc();
709+
if ($expectedRemainingPropertyNames === []) {
710+
self::assertSame([], $remainingRules);
711+
}
499712
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
500713
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
501714
}

0 commit comments

Comments
 (0)