Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Expression/ForClasses/IsAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
if ($theClass->isAbstract() || $theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum()
|| $theClass->isFinal()) {
if ($theClass->isAbstract()) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Expression/ForClasses/IsFinal.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
if ($theClass->isAbstract() || $theClass->isInterface() || $theClass->isFinal() || $theClass->isTrait()
|| $theClass->isEnum()) {
if ($theClass->isFinal()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Expression/ForClasses/IsReadonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function describe(ClassDescription $theClass, string $because): Descripti

public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void
{
if ($theClass->isReadonly() || $theClass->isInterface() || $theClass->isTrait() || $theClass->isEnum()) {
if ($theClass->isReadonly()) {
return;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/E2E/PHPUnit/CheckClassWithMultipleExpressionsTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\E2E\PHPUnit;

use Arkitect\ClassSet;
use Arkitect\Expression\ForClasses\HaveNameMatching;
use Arkitect\Expression\ForClasses\IsAbstract;
use Arkitect\Expression\ForClasses\IsNotAbstract;
use Arkitect\Expression\ForClasses\IsNotEnum;
use Arkitect\Expression\ForClasses\IsNotFinal;
Expand Down Expand Up @@ -33,4 +36,28 @@ public function test_it_can_check_multiple_expressions(): void

ArchRuleTestCase::assertArchRule($rule, $set);
}

public function test_is_abstract_in_that(): void
{
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/is_something');

$rule = Rule::allClasses()
->that(new IsAbstract())
->should(new HaveNameMatching('*Abstract'))
->because('we want to prefix abstract classes');

ArchRuleTestCase::assertArchRule($rule, $set);
}

public function test_is_abstract_in_should(): void
{
$set = ClassSet::fromDir(__DIR__.'/../_fixtures/is_something');

$rule = Rule::allClasses()
->that(new ResideInOneOfTheseNamespaces('App'))
->should(new IsAbstract())
->because('we want to prefix abstract classes');

ArchRuleTestCase::assertArchRule($rule, $set);
}
}
9 changes: 9 additions & 0 deletions tests/E2E/_fixtures/is_something/MyAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App;

abstract class MyAbstract
{
}
9 changes: 9 additions & 0 deletions tests/E2E/_fixtures/is_something/MyClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App;

final class MyClass
{
}
8 changes: 8 additions & 0 deletions tests/E2E/_fixtures/is_something/MyEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<?php

declare(strict_types=1);

namespace App;

enum MyEnum {}
9 changes: 9 additions & 0 deletions tests/E2E/_fixtures/is_something/MyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App;

interface MyInterface
{
}
7 changes: 7 additions & 0 deletions tests/E2E/_fixtures/is_something/MyReadOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace App;

readonly class MyReadOnly {}
70 changes: 40 additions & 30 deletions tests/Unit/Expressions/ForClasses/IsAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,65 @@
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsAbstract;
use Arkitect\Rules\Specs;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

class IsAbstractTest extends TestCase
{
public function test_it_should_return_violation_error(): void
public function test_it_should_return_true_if_is_abstract(): void
{
$isAbstract = new IsAbstract();
$specStore = new Specs();
$specStore->add(new IsAbstract());

$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
false,
false,
false,
true,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violationError = $isAbstract->describe($classDescription, $because)->toString();

$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertNotEquals(0, $violations->count());
$because = 'we want to add this rule for our software';

$this->assertEquals('HappyIsland should be abstract because we want to add this rule for our software', $violationError);
$this->assertTrue($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_it_should_return_true_if_is_abstract(): void
public function test_it_should_return_violation_error(): void
{
$isAbstract = new IsAbstract();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[],
null,
true,
true,
true,
false,
false,
false,
false,
false,
false
);
$because = 'we want to add this rule for our software';
$violationError = $isAbstract->describe($classDescription, $because)->toString();

$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());
self::assertNotEquals(0, $violations->count());

$this->assertEquals('HappyIsland should be abstract because we want to add this rule for our software', $violationError);
}

public function test_interfaces_can_not_be_abstract_and_should_be_ignored(): void
{
$isAbstract = new IsAbstract();
$specStore = new Specs();
$specStore->add(new IsAbstract());

$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -73,15 +78,17 @@ public function test_interfaces_can_not_be_abstract_and_should_be_ignored(): voi
false,
false
);

$because = 'we want to add this rule for our software';
$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_traits_can_not_be_abstract_and_should_be_ignored(): void
{
$isAbstract = new IsAbstract();
$specStore = new Specs();
$specStore->add(new IsAbstract());

$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -94,15 +101,17 @@ public function test_traits_can_not_be_abstract_and_should_be_ignored(): void
true,
false
);

$because = 'we want to add this rule for our software';
$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_enums_can_not_be_abstract_and_should_be_ignored(): void
{
$isAbstract = new IsAbstract();
$specStore = new Specs();
$specStore->add(new IsAbstract());

$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -115,15 +124,17 @@ public function test_enums_can_not_be_abstract_and_should_be_ignored(): void
false,
true
);

$because = 'we want to add this rule for our software';
$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_final_classes_can_not_be_abstract_and_should_be_ignored(): void
{
$isAbstract = new IsAbstract();
$specStore = new Specs();
$specStore->add(new IsAbstract());

$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -137,8 +148,7 @@ public function test_final_classes_can_not_be_abstract_and_should_be_ignored():
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isAbstract->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}
}
46 changes: 21 additions & 25 deletions tests/Unit/Expressions/ForClasses/IsFinalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\ForClasses\IsFinal;
use Arkitect\Rules\Specs;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -39,9 +40,9 @@ public function test_it_should_return_violation_error(): void

public function test_it_should_return_true_if_is_final(): void
{
$class = 'myClass';
$specStore = new Specs();
$specStore->add(new IsFinal());

$isFinal = new IsFinal();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -55,16 +56,15 @@ public function test_it_should_return_true_if_is_final(): void
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isFinal->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertTrue($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_abstract_classes_can_not_be_final_and_should_be_ignored(): void
{
$class = 'myClass';
$specStore = new Specs();
$specStore->add(new IsFinal());

$isFinal = new IsFinal();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -78,16 +78,15 @@ public function test_abstract_classes_can_not_be_final_and_should_be_ignored():
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isFinal->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_interfaces_can_not_be_final_and_should_be_ignored(): void
{
$class = 'myClass';
$specStore = new Specs();
$specStore->add(new IsFinal());

$isFinal = new IsFinal();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -101,16 +100,15 @@ public function test_interfaces_can_not_be_final_and_should_be_ignored(): void
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isFinal->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_traits_can_not_be_final_and_should_be_ignored(): void
{
$class = 'myClass';
$specStore = new Specs();
$specStore->add(new IsFinal());

$isFinal = new IsFinal();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -124,16 +122,15 @@ public function test_traits_can_not_be_final_and_should_be_ignored(): void
false
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isFinal->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}

public function test_enums_can_not_be_final_and_should_be_ignored(): void
{
$class = 'myClass';
$specStore = new Specs();
$specStore->add(new IsFinal());

$isFinal = new IsFinal();
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
Expand All @@ -147,8 +144,7 @@ public function test_enums_can_not_be_final_and_should_be_ignored(): void
true
);
$because = 'we want to add this rule for our software';
$violations = new Violations();
$isFinal->evaluate($classDescription, $violations, $because);
self::assertEquals(0, $violations->count());

$this->assertFalse($specStore->allSpecsAreMatchedBy($classDescription, $because));
}
}
Loading
Loading