Skip to content

Commit 88bf5df

Browse files
committed
Add explicit appliesTo() tests for HaveTrait and NotHaveTrait rules
Following the pattern from PR #561, add comprehensive tests that explicitly verify the appliesTo() method behavior for both HaveTrait and NotHaveTrait expressions: - Add tests verifying appliesTo() returns true for regular classes - Add tests verifying appliesTo() returns false for interfaces - Add tests verifying appliesTo() returns false for traits - Add tests verifying evaluate() respects appliesTo() behavior Also fix coding standards (trailing commas) in various files. This ensures proper test coverage and prevents the issues fixed in PR #561.
1 parent 85c987e commit 88bf5df

File tree

9 files changed

+125
-6
lines changed

9 files changed

+125
-6
lines changed

src/Analyzer/ClassDescription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(
6262
array $docBlock,
6363
array $attributes,
6464
array $traits,
65-
string $filePath
65+
string $filePath,
6666
) {
6767
$this->FQCN = $FQCN;
6868
$this->filePath = $filePath;

src/Analyzer/FileParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
FileVisitor $fileVisitor,
3030
NameResolver $nameResolver,
3131
DocblockTypesResolver $docblockTypesResolver,
32-
TargetPhpVersion $targetPhpVersion
32+
TargetPhpVersion $targetPhpVersion,
3333
) {
3434
$this->fileVisitor = $fileVisitor;
3535
$this->parsingErrors = [];

src/CLI/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function check(
4444
Parser $fileParser,
4545
Violations $violations,
4646
ParsingErrors $parsingErrors,
47-
bool $stopOnFailure
47+
bool $stopOnFailure,
4848
): void {
4949
/** @var SplFileInfo $file */
5050
foreach ($classSetRule->getClassSet() as $file) {

src/Expression/ForClasses/HaveTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ public function describe(ClassDescription $theClass, string $because): Descripti
2525
return new Description("should use the trait {$this->trait}", $because);
2626
}
2727

28+
public function appliesTo(ClassDescription $theClass): bool
29+
{
30+
return !($theClass->isInterface() || $theClass->isTrait());
31+
}
32+
2833
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
2934
{
35+
if ($theClass->isInterface() || $theClass->isTrait()) {
36+
return;
37+
}
38+
3039
if ($theClass->hasTrait($this->trait)) {
3140
return;
3241
}

src/PHPUnit/ArchRuleCheckerConstraintAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function toString(): string
6161

6262
protected function matches(
6363
/** @var ArchRule $rule */
64-
$other
64+
$other,
6565
): bool {
6666
$this->runner->check(
6767
ClassSetRules::create($this->classSet, $other),

src/Rules/ArchRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
Constraints $constraints,
2929
string $because,
3030
array $classesToBeExcluded,
31-
bool $runOnlyThis
31+
bool $runOnlyThis,
3232
) {
3333
$this->thats = $specs;
3434
$this->shoulds = $constraints;

tests/E2E/Cli/CheckCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected function runCheck(
269269
bool $skipBaseline = false,
270270
bool $ignoreBaselineNumbers = false,
271271
string $format = 'text',
272-
?string $autoloadFilePath = null
272+
?string $autoloadFilePath = null,
273273
): ApplicationTester {
274274
$input = ['check'];
275275

tests/Unit/Expressions/ForClasses/HaveTraitTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,76 @@ public function test_it_should_return_false_if_class_does_not_use_trait(): void
6868

6969
self::assertEquals(1, $violations->count());
7070
}
71+
72+
public function test_it_should_return_no_violation_if_is_an_interface(): void
73+
{
74+
$expression = new HaveTrait('MyTrait');
75+
76+
$classDescription = (new ClassDescriptionBuilder())
77+
->setFilePath('src/Foo.php')
78+
->setClassName('HappyIsland')
79+
->setInterface(true)
80+
->build();
81+
82+
$because = 'we want to add this rule for our software';
83+
$violations = new Violations();
84+
$expression->evaluate($classDescription, $violations, $because);
85+
86+
self::assertEquals(0, $violations->count());
87+
}
88+
89+
public function test_it_should_return_no_violation_if_is_a_trait(): void
90+
{
91+
$expression = new HaveTrait('MyTrait');
92+
93+
$classDescription = (new ClassDescriptionBuilder())
94+
->setFilePath('src/Foo.php')
95+
->setClassName('HappyIsland')
96+
->setTrait(true)
97+
->build();
98+
99+
$because = 'we want to add this rule for our software';
100+
$violations = new Violations();
101+
$expression->evaluate($classDescription, $violations, $because);
102+
103+
self::assertEquals(0, $violations->count());
104+
}
105+
106+
public function test_applies_to_should_return_true_for_regular_classes(): void
107+
{
108+
$expression = new HaveTrait('MyTrait');
109+
110+
$classDescription = (new ClassDescriptionBuilder())
111+
->setFilePath('src/Foo.php')
112+
->setClassName('HappyIsland')
113+
->build();
114+
115+
self::assertTrue($expression->appliesTo($classDescription));
116+
}
117+
118+
public function test_applies_to_should_return_false_for_interfaces(): void
119+
{
120+
$expression = new HaveTrait('MyTrait');
121+
122+
$classDescription = (new ClassDescriptionBuilder())
123+
->setFilePath('src/Foo.php')
124+
->setClassName('HappyIsland')
125+
->setInterface(true)
126+
->build();
127+
128+
self::assertFalse($expression->appliesTo($classDescription));
129+
}
130+
131+
public function test_applies_to_should_return_false_for_traits(): void
132+
{
133+
$expression = new HaveTrait('MyTrait');
134+
135+
$classDescription = (new ClassDescriptionBuilder())
136+
->setFilePath('src/Foo.php')
137+
->setClassName('HappyIsland')
138+
->setTrait(true)
139+
->build();
140+
141+
self::assertFalse($expression->appliesTo($classDescription));
142+
}
71143
}

tests/Unit/Expressions/ForClasses/NotHaveTraitTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,42 @@ public function test_it_should_return_no_violation_if_is_a_trait(): void
100100

101101
self::assertEquals(0, $violations->count());
102102
}
103+
104+
public function test_applies_to_should_return_true_for_regular_classes(): void
105+
{
106+
$traitConstraint = new NotHaveTrait('MyTrait');
107+
108+
$classDescription = (new ClassDescriptionBuilder())
109+
->setFilePath('src/Foo.php')
110+
->setClassName('HappyIsland')
111+
->build();
112+
113+
self::assertTrue($traitConstraint->appliesTo($classDescription));
114+
}
115+
116+
public function test_applies_to_should_return_false_for_interfaces(): void
117+
{
118+
$traitConstraint = new NotHaveTrait('MyTrait');
119+
120+
$classDescription = (new ClassDescriptionBuilder())
121+
->setFilePath('src/Foo.php')
122+
->setClassName('HappyIsland')
123+
->setInterface(true)
124+
->build();
125+
126+
self::assertFalse($traitConstraint->appliesTo($classDescription));
127+
}
128+
129+
public function test_applies_to_should_return_false_for_traits(): void
130+
{
131+
$traitConstraint = new NotHaveTrait('MyTrait');
132+
133+
$classDescription = (new ClassDescriptionBuilder())
134+
->setFilePath('src/Foo.php')
135+
->setClassName('HappyIsland')
136+
->setTrait(true)
137+
->build();
138+
139+
self::assertFalse($traitConstraint->appliesTo($classDescription));
140+
}
103141
}

0 commit comments

Comments
 (0)