Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 224 additions & 11 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function provideInitialPropertyNamesAndAnotherPropertyName(): Data
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
public function addRuleWithoutPositionWithoutSiblingAddsRuleAfterInitialRules(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
Expand All @@ -102,8 +102,44 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn

$rules = $this->subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
}

/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithoutPositionWithoutSiblingSetsValidLineNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
}

/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithoutPositionWithoutSiblingSetsValidColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
}
Expand All @@ -115,7 +151,7 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLineNumber(
public function addRuleWithOnlyLineNumberWithoutSiblingAddsRule(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
Expand All @@ -126,8 +162,46 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
$this->subject->addRule($ruleToAdd);

self::assertContains($ruleToAdd, $this->subject->getRules());
}

/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyLineNumberWithoutSiblingSetsColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
}

/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyLineNumberWithoutSiblingPreservesLineNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
}

Expand All @@ -138,7 +212,7 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineNumberPreservingColumnNumber(
public function addRuleWithOnlyColumnNumberWithoutSiblingAddsRuleAfterInitialRules(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
Expand All @@ -150,8 +224,46 @@ public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineN

$rules = $this->subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
}

/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyColumnNumberWithoutSiblingSetsLineNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(null, 42);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
}

/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyColumnNumberWithoutSiblingPreservesColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(null, 42);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertSame(42, $ruleToAdd->getColumnNumber(), 'column number not preserved');
}

Expand All @@ -162,7 +274,7 @@ public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineN
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
public function addRuleWithCompletePositionWithoutSiblingAddsRule(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
Expand All @@ -173,6 +285,25 @@ public function addRuleWithCompletePositionAddsRuleAndPreservesPosition(
$this->subject->addRule($ruleToAdd);

self::assertContains($ruleToAdd, $this->subject->getRules());
}

/**
* @test
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithCompletePositionWithoutSiblingPreservesPosition(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42, 64);
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->addRule($ruleToAdd);

self::assertSame(42, $ruleToAdd->getLineNumber(), 'line number not preserved');
self::assertSame(64, $ruleToAdd->getColumnNumber(), 'column number not preserved');
}
Expand Down Expand Up @@ -286,7 +417,7 @@ public function addRuleWithSiblingSetsValidColumnNumber(
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsValidLineAndColumnNumbers(
public function addRuleWithSiblingNotInSetAddsRuleAfterInitialRules(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
Expand All @@ -299,8 +430,48 @@ public function addRuleWithSiblingNotInRuleSetAddsRuleAfterInitialRulesAndSetsVa

$rules = $this->subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
}

/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithSiblingNotInSetSetsValidLineNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);

// `display` is sometimes in `$initialPropertyNames` and sometimes the `$propertyNameToAdd`.
// Choosing this for the bogus sibling allows testing all combinations of whether it is or isn't.
$this->subject->addRule($ruleToAdd, new Rule('display'));

self::assertIsInt($ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
}

/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider provideInitialPropertyNamesAndAnotherPropertyName
*/
public function addRuleWithSiblingNotInSetSetsValidColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
): void {
$ruleToAdd = new Rule($propertyNameToAdd);
$this->setRulesFromPropertyNames($initialPropertyNames);

// `display` is sometimes in `$initialPropertyNames` and sometimes the `$propertyNameToAdd`.
// Choosing this for the bogus sibling allows testing all combinations of whether it is or isn't.
$this->subject->addRule($ruleToAdd, new Rule('display'));

self::assertIsInt($ruleToAdd->getColumnNumber(), 'column number not set');
self::assertGreaterThanOrEqual(0, $ruleToAdd->getColumnNumber(), 'column number not valid');
}
Expand Down Expand Up @@ -410,6 +581,24 @@ public static function providePropertyNamesAndPropertyNameToRemoveAndExpectedRem
];
}

/**
* @test
*
* @param list<string> $initialPropertyNames
*
* @dataProvider providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesRemovesRulesWithPropertyName(
array $initialPropertyNames,
string $propertyNameToRemove
): void {
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->removeMatchingRules($propertyNameToRemove);

self::assertArrayNotHasKey($propertyNameToRemove, $this->subject->getRulesAssoc());
}

/**
* @test
*
Expand All @@ -418,7 +607,7 @@ public static function providePropertyNamesAndPropertyNameToRemoveAndExpectedRem
*
* @dataProvider providePropertyNamesAndPropertyNameToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers(
public function removeMatchingRulesWithPropertyNameKeepsOtherRules(
array $initialPropertyNames,
string $propertyNameToRemove,
array $expectedRemainingPropertyNames
Expand All @@ -428,7 +617,9 @@ public function removeMatchingRulesRemovesRulesByPropertyNameAndKeepsOthers(
$this->subject->removeMatchingRules($propertyNameToRemove);

$remainingRules = $this->subject->getRulesAssoc();
self::assertArrayNotHasKey($propertyNameToRemove, $remainingRules);
if ($expectedRemainingPropertyNames === []) {
self::assertSame([], $remainingRules);
}
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
}
Expand Down Expand Up @@ -477,14 +668,12 @@ public static function providePropertyNamesAndPropertyNamePrefixToRemoveAndExpec
* @test
*
* @param list<string> $initialPropertyNames
* @param list<string> $expectedRemainingPropertyNames
*
* @dataProvider providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOthers(
public function removeMatchingRulesRemovesRulesWithPropertyNamePrefix(
array $initialPropertyNames,
string $propertyNamePrefix,
array $expectedRemainingPropertyNames
string $propertyNamePrefix
): void {
$propertyNamePrefixWithHyphen = $propertyNamePrefix . '-';
$this->setRulesFromPropertyNames($initialPropertyNames);
Expand All @@ -496,6 +685,30 @@ public function removeMatchingRulesRemovesRulesByPropertyNamePrefixAndKeepsOther
foreach (\array_keys($remainingRules) as $remainingPropertyName) {
self::assertStringStartsNotWith($propertyNamePrefixWithHyphen, $remainingPropertyName);
}
}

/**
* @test
*
* @param list<string> $initialPropertyNames
* @param list<string> $expectedRemainingPropertyNames
*
* @dataProvider providePropertyNamesAndPropertyNamePrefixToRemoveAndExpectedRemainingPropertyNames
*/
public function removeMatchingRulesWithPropertyNamePrefixKeepsOtherRules(
array $initialPropertyNames,
string $propertyNamePrefix,
array $expectedRemainingPropertyNames
): void {
$propertyNamePrefixWithHyphen = $propertyNamePrefix . '-';
$this->setRulesFromPropertyNames($initialPropertyNames);

$this->subject->removeMatchingRules($propertyNamePrefixWithHyphen);

$remainingRules = $this->subject->getRulesAssoc();
if ($expectedRemainingPropertyNames === []) {
self::assertSame([], $remainingRules);
}
foreach ($expectedRemainingPropertyNames as $expectedPropertyName) {
self::assertArrayHasKey($expectedPropertyName, $remainingRules);
}
Expand Down