-
-
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 complex nested expressions, like ANDs inside ORs.
- Loading branch information
Herberto Graca
committed
Jul 26, 2023
1 parent
976c200
commit 6dd16f1
Showing
2 changed files
with
139 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,58 @@ | ||
<?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 Andx implements Expression | ||
{ | ||
/** @var Expression[] */ | ||
private $expressions; | ||
|
||
public function __construct(Expression ...$expressions) | ||
{ | ||
if (\count($expressions) <= 1) { | ||
throw new \InvalidArgumentException('at least two expressions are required'); | ||
} | ||
$this->expressions = $expressions; | ||
} | ||
|
||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
$expressionsDescriptions = []; | ||
foreach ($this->expressions as $expression) { | ||
$expressionsDescriptions[] = $expression->describe($theClass, '')->toString(); | ||
} | ||
return new Description( | ||
'all expressions must be true (' . implode(', ', $expressionsDescriptions) . ')', | ||
$because | ||
); | ||
} | ||
|
||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
foreach ($this->expressions as $expression) { | ||
$newViolations = new Violations(); | ||
$expression->evaluate($theClass, $newViolations, $because); | ||
if (0 !== $newViolations->count()) { | ||
$violations->add(Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::withDescription( | ||
$this->describe($theClass, $because), | ||
"The class '" . $theClass->getFQCN() . "' violated the expression " | ||
. $expression->describe($theClass, '')->toString() | ||
) | ||
)); | ||
|
||
return; | ||
} | ||
} | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arkitect\Tests\Unit\Expressions\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Analyzer\FullyQualifiedClassName; | ||
use Arkitect\Expression\ForClasses\Andx; | ||
use Arkitect\Expression\ForClasses\Extend; | ||
use Arkitect\Expression\ForClasses\Implement; | ||
use Arkitect\Rules\Violations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AndxTest extends TestCase | ||
{ | ||
public function test_it_should_pass_the_rule(): void | ||
{ | ||
$interface = 'interface'; | ||
$class = 'SomeClass'; | ||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString('HappyIsland'), | ||
[], | ||
[FullyQualifiedClassName::fromString($interface)], | ||
FullyQualifiedClassName::fromString($class), | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
$implementConstraint = new Implement($interface); | ||
$extendsConstraint = new Extend($class); | ||
$andConstraint = new Andx($implementConstraint, $extendsConstraint); | ||
|
||
$because = 'reasons'; | ||
$violations = new Violations(); | ||
$andConstraint->evaluate($classDescription, $violations, $because); | ||
|
||
self::assertEquals(0, $violations->count()); | ||
} | ||
|
||
public function test_it_should_not_pass_the_rule(): void | ||
{ | ||
$interface = 'SomeInterface'; | ||
$class = 'SomeClass'; | ||
|
||
$classDescription = new ClassDescription( | ||
FullyQualifiedClassName::fromString('HappyIsland'), | ||
[], | ||
[FullyQualifiedClassName::fromString($interface)], | ||
null, | ||
false, | ||
false, | ||
false, | ||
false, | ||
false | ||
); | ||
|
||
$implementConstraint = new Implement($interface); | ||
$extendsConstraint = new Extend($class); | ||
$andConstraint = new Andx($implementConstraint, $extendsConstraint); | ||
|
||
$because = 'reasons'; | ||
$violationError = $andConstraint->describe($classDescription, $because)->toString(); | ||
|
||
$violations = new Violations(); | ||
$andConstraint->evaluate($classDescription, $violations, $because); | ||
self::assertNotEquals(0, $violations->count()); | ||
|
||
$this->assertEquals( | ||
'all expressions must be true (should implement SomeInterface, should extend SomeClass) because reasons', | ||
$violationError | ||
); | ||
$this->assertEquals( | ||
"The class 'HappyIsland' violated the expression should extend SomeClass, but " | ||
. "all expressions must be true (should implement SomeInterface, should extend SomeClass) because reasons", | ||
$violations->get(0)->getError() | ||
); | ||
} | ||
} |