Skip to content

Commit bc840af

Browse files
committed
[TASK] Add unit tests for RuleSet::removeRule
This re-uses some data providers, which have been renamed to reflect their more generic usage. Also, the PHPDoc type has been tightened to `non-empty-list` where applicable. Part of #974.
1 parent fdbb925 commit bc840af

File tree

1 file changed

+60
-20
lines changed

1 file changed

+60
-20
lines changed

tests/Unit/RuleSet/RuleSetTest.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function providePropertyNamesToBeSetInitially(): array
6767
/**
6868
* @return array<string, array{0: non-empty-string}>
6969
*/
70-
public static function providePropertyNameToAdd(): array
70+
public static function provideAnotherPropertyName(): array
7171
{
7272
return [
7373
'property name `color` maybe matching that of existing declaration' => ['color'],
@@ -79,17 +79,17 @@ public static function providePropertyNameToAdd(): array
7979
/**
8080
* @return DataProvider<string, array{0: list<string>, 1: string}>
8181
*/
82-
public static function provideInitialPropertyNamesAndPropertyNameToAdd(): DataProvider
82+
public static function provideInitialPropertyNamesAndAnotherPropertyName(): DataProvider
8383
{
84-
return DataProvider::cross(self::providePropertyNamesToBeSetInitially(), self::providePropertyNameToAdd());
84+
return DataProvider::cross(self::providePropertyNamesToBeSetInitially(), self::provideAnotherPropertyName());
8585
}
8686

8787
/**
8888
* @test
8989
*
9090
* @param list<string> $initialPropertyNames
9191
*
92-
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
92+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
9393
*/
9494
public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
9595
array $initialPropertyNames,
@@ -111,7 +111,7 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn
111111
/**
112112
* @test
113113
*
114-
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
114+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
115115
*
116116
* @param list<string> $initialPropertyNames
117117
*/
@@ -134,7 +134,7 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
134134
/**
135135
* @test
136136
*
137-
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
137+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
138138
*
139139
* @param list<string> $initialPropertyNames
140140
*/
@@ -158,7 +158,7 @@ public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineN
158158
/**
159159
* @test
160160
*
161-
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
161+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
162162
*
163163
* @param list<string> $initialPropertyNames
164164
*/
@@ -180,21 +180,21 @@ public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
180180
/**
181181
* @return array<string, array{0: non-empty-list<non-empty-string>, 1: int<0, max>}>
182182
*/
183-
public static function provideInitialPropertyNamesAndSiblingIndex(): array
183+
public static function provideInitialPropertyNamesAndIndexOfOne(): array
184184
{
185185
$initialPropertyNamesSets = self::providePropertyNamesToBeSetInitially();
186186

187-
// Provide sets with each possible sibling index for the initially set `Rule`s.
188-
$initialPropertyNamesAndSiblingIndexSets = [];
187+
// Provide sets with each possible index for the initially set `Rule`s.
188+
$initialPropertyNamesAndIndexSets = [];
189189
foreach ($initialPropertyNamesSets as $setName => $data) {
190190
$initialPropertyNames = $data[0];
191-
for ($siblingIndex = 0; $siblingIndex < \count($initialPropertyNames); ++$siblingIndex) {
192-
$initialPropertyNamesAndSiblingIndexSets[$setName . ', sibling index ' . $siblingIndex] =
193-
[$initialPropertyNames, $siblingIndex];
191+
for ($index = 0; $index < \count($initialPropertyNames); ++$index) {
192+
$initialPropertyNamesAndIndexSets[$setName . ', index ' . $index] =
193+
[$initialPropertyNames, $index];
194194
}
195195
}
196196

197-
return $initialPropertyNamesAndSiblingIndexSets;
197+
return $initialPropertyNamesAndIndexSets;
198198
}
199199

200200
/**
@@ -203,15 +203,15 @@ public static function provideInitialPropertyNamesAndSiblingIndex(): array
203203
public static function provideInitialPropertyNamesAndSiblingIndexAndPropertyNameToAdd(): DataProvider
204204
{
205205
return DataProvider::cross(
206-
self::provideInitialPropertyNamesAndSiblingIndex(),
207-
self::providePropertyNameToAdd()
206+
self::provideInitialPropertyNamesAndIndexOfOne(),
207+
self::provideAnotherPropertyName()
208208
);
209209
}
210210

211211
/**
212212
* @test
213213
*
214-
* @param list<string> $initialPropertyNames
214+
* @param non-empty-list<string> $initialPropertyNames
215215
* @param int<0, max> $siblingIndex
216216
*
217217
* @dataProvider provideInitialPropertyNamesAndSiblingIndexAndPropertyNameToAdd
@@ -236,7 +236,7 @@ public function addRuleWithSiblingInsertsRuleBeforeSibling(
236236
/**
237237
* @test
238238
*
239-
* @param list<string> $initialPropertyNames
239+
* @param non-empty-list<string> $initialPropertyNames
240240
* @param int<0, max> $siblingIndex
241241
*
242242
* @dataProvider provideInitialPropertyNamesAndSiblingIndexAndPropertyNameToAdd
@@ -259,7 +259,7 @@ public function addRuleWithSiblingSetsValidLineNumber(
259259
/**
260260
* @test
261261
*
262-
* @param list<string> $initialPropertyNames
262+
* @param non-empty-list<string> $initialPropertyNames
263263
* @param int<0, max> $siblingIndex
264264
*
265265
* @dataProvider provideInitialPropertyNamesAndSiblingIndexAndPropertyNameToAdd
@@ -284,7 +284,7 @@ public function addRuleWithSiblingSetsValidColumnNumber(
284284
*
285285
* @param list<string> $initialPropertyNames
286286
*
287-
* @dataProvider provideInitialPropertyNamesAndPropertyNameToAdd
287+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
288288
*/
289289
public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
290290
array $initialPropertyNames,
@@ -305,6 +305,46 @@ public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsVa
305305
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
306306
}
307307

308+
/**
309+
* @test
310+
*
311+
* @param non-empty-list<string> $initialPropertyNames
312+
* @param int<0, max> $indexToRemove
313+
*
314+
* @dataProvider provideInitialPropertyNamesAndIndexOfOne
315+
*/
316+
public function removeRuleRemovesRuleInSetAndKeepsOthers(array $initialPropertyNames, int $indexToRemove): void
317+
{
318+
$this->setRulesFromPropertyNames($initialPropertyNames);
319+
$ruleToRemove = $this->subject->getRules()[$indexToRemove];
320+
321+
$this->subject->removeRule($ruleToRemove);
322+
323+
$remainingRules = $this->subject->getRules();
324+
self::assertNotContains($ruleToRemove, $remainingRules);
325+
self::assertCount(\count($initialPropertyNames) - 1, $remainingRules);
326+
}
327+
328+
/**
329+
* @test
330+
*
331+
* @param list<string> $initialPropertyNames
332+
*
333+
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
334+
*/
335+
public function removeRuleDoesNothingWithRuleNotInSet(
336+
array $initialPropertyNames,
337+
string $propertyNameToRemove
338+
): void {
339+
$this->setRulesFromPropertyNames($initialPropertyNames);
340+
$initialRules = $this->subject->getRules();
341+
$ruleToRemove = new Rule($propertyNameToRemove);
342+
343+
$this->subject->removeRule($ruleToRemove);
344+
345+
self::assertSame($initialRules, $this->subject->getRules());
346+
}
347+
308348
/**
309349
* @return array<string, array{0: list<string>, 1: string, 2: list<string>}>
310350
*/

0 commit comments

Comments
 (0)