-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will allow for testing if a class extends or implements another code unit, anywhere in the inheritance tree.
- Loading branch information
Herberto Graca
committed
Jul 26, 2023
1 parent
976c200
commit 7b00915
Showing
7 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Expression\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Expression\Description; | ||
use Arkitect\Expression\Expression; | ||
use Arkitect\Rules\Violation; | ||
use Arkitect\Rules\ViolationMessage; | ||
use Arkitect\Rules\Violations; | ||
|
||
final class IsA implements Expression | ||
{ | ||
/** @var string[] */ | ||
private $allowedFqcnList; | ||
|
||
public function __construct(string ...$allowedFqcnList) | ||
{ | ||
$this->allowedFqcnList = $allowedFqcnList; | ||
} | ||
|
||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
$allowedFqcnList = implode(', ', $this->allowedFqcnList); | ||
|
||
return new Description("should inherit from one of: $allowedFqcnList", $because); | ||
} | ||
|
||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
if (!$this->isA($theClass, ...$this->allowedFqcnList)) { | ||
$violation = Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::selfExplanatory($this->describe($theClass, $because)) | ||
); | ||
|
||
$violations->add($violation); | ||
} | ||
} | ||
|
||
private function isA(ClassDescription $theClass, string ...$allowedFqcnList): bool | ||
{ | ||
foreach ($allowedFqcnList as $allowedFqcn) { | ||
if ( | ||
\is_a($theClass->getFQCN(), $allowedFqcn, true) | ||
|| \is_subclass_of($theClass->getFQCN(), $allowedFqcn) | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses; | ||
|
||
class Banana implements FruitInterface | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/Expressions/ForClasses/DummyClasses/CavendishBanana.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses; | ||
|
||
class CavendishBanana extends Banana | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses; | ||
|
||
final class Dog | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/Expressions/ForClasses/DummyClasses/DwarfCavendishBanana.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses; | ||
|
||
final class DwarfCavendishBanana extends CavendishBanana | ||
{ | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
tests/Unit/Expressions/ForClasses/DummyClasses/FruitInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses; | ||
|
||
interface FruitInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Analyzer\FullyQualifiedClassName; | ||
use Arkitect\Expression\ForClasses\IsA; | ||
use Arkitect\Rules\Violations; | ||
use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\Banana; | ||
use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\CavendishBanana; | ||
use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\Dog; | ||
use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\DwarfCavendishBanana; | ||
use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\FruitInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class IsATest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_should_have_no_violation_when_it_implements(): void | ||
{ | ||
$interface = FruitInterface::class; | ||
$isA = new IsA($interface); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString(CavendishBanana::class), | ||
[], | ||
[FullyQualifiedClassName::fromString($interface)], | ||
null, | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
|
||
$violations = new Violations(); | ||
$isA->evaluate($classDescription, $violations, ''); | ||
|
||
self::assertEquals(0, $violations->count()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_have_no_violation_when_it_extends(): void | ||
{ | ||
$class = Banana::class; | ||
$isA = new IsA($class); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString(DwarfCavendishBanana::class), | ||
[], | ||
[], | ||
FullyQualifiedClassName::fromString($class), | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
|
||
$violations = new Violations(); | ||
$isA->evaluate($classDescription, $violations, ''); | ||
|
||
self::assertEquals(0, $violations->count()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_have_violation_if_it_doesnt_extend_nor_implement(): void | ||
{ | ||
$interface = FruitInterface::class; | ||
$class = Banana::class; | ||
$isA = new IsA($class, $interface); | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString(Dog::class), | ||
[], | ||
[], | ||
null, | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
|
||
$violations = new Violations(); | ||
$isA->evaluate($classDescription, $violations, ''); | ||
|
||
self::assertEquals(1, $violations->count()); | ||
self::assertEquals( | ||
"should inherit from one of: $class, $interface", | ||
$isA->describe($classDescription, '')->toString() | ||
); | ||
} | ||
} |