Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleorselli committed Feb 20, 2025
1 parent aafa490 commit 70d6762
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_is_abstract_in_that(): void

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

ArchRuleTestCase::assertArchRule($rule, $set);
Expand Down
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 {}
69 changes: 39 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,16 +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 @@ -95,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 @@ -116,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 @@ -138,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));
}
}
29 changes: 17 additions & 12 deletions tests/Unit/Expressions/ForClasses/IsReadonlyTest.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\IsReadonly;
use Arkitect\Rules\Specs;
use Arkitect\Rules\Violations;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -62,7 +63,9 @@ public function test_it_should_return_true_if_is_readonly(): void

public function test_interfaces_can_not_be_readonly_and_should_be_ignored(): void
{
$isReadonly = new IsReadonly();
$specStore = new Specs();
$specStore->add(new IsReadonly());

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

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

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

public function test_traits_can_not_be_readonly_and_should_be_ignored(): void
{
$isReadonly = new IsReadonly();
$specStore = new Specs();
$specStore->add(new IsReadonly());

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

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

public function test_enums_can_not_be_readonly_and_should_be_ignored(): void
{
$isReadonly = new IsReadonly();
$specStore = new Specs();
$specStore->add(new IsReadonly());

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

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

0 comments on commit 70d6762

Please sign in to comment.