Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a And expression #395

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions src/Expression/Boolean/Andx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Arkitect\Expression\Boolean;

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)
{
$this->expressions = $expressions;
}

public function describe(ClassDescription $theClass, string $because): Description
{
$expressionsDescriptions = [];
foreach ($this->expressions as $expression) {
$expressionsDescriptions[] = $expression->describe($theClass, $because)->toString();
}
$expressionsDescriptionsString = "(\n"
.implode("\nAND\n", array_unique(array_map('trim', $expressionsDescriptions)))
."\n)";

return new Description($expressionsDescriptionsString, $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\n"
.$expression->describe($theClass, '')->toString()
)
));

return;
}
}
}
}
127 changes: 127 additions & 0 deletions tests/Unit/Expressions/Boolean/AndxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

namespace Arkitect\Tests\Unit\Expressions\Boolean;

use Arkitect\Analyzer\ClassDescription;
use Arkitect\Analyzer\ClassDescriptionBuilder;
use Arkitect\Analyzer\FullyQualifiedClassName;
use Arkitect\Expression\Boolean\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_return_no_violation_when_empty(): void
{
$and = new Andx();

$classDescription = (new ClassDescriptionBuilder())
->setClassName('My\Class')
->setExtends('My\BaseClass', 10)
->build();

$violations = new Violations();
$and->evaluate($classDescription, $violations, 'because');

self::assertEquals(0, $violations->count());
}

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_pass_the_rule_when_and_is_empty(): void
{
$interface = 'interface';
$class = 'SomeClass';
$classDescription = new ClassDescription(
FullyQualifiedClassName::fromString('HappyIsland'),
[],
[FullyQualifiedClassName::fromString($interface)],
FullyQualifiedClassName::fromString($class),
false,
false,
false,
false,
false
);
$andConstraint = new Andx();

$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(
"(\nshould implement SomeInterface because reasons\nAND\nshould extend SomeClass because reasons\n) because reasons",
$violationError
);
$this->assertEquals(
"The class 'HappyIsland' violated the expression\n"
."should extend SomeClass\n"
."from the rule\n"
."(\n"
."should implement SomeInterface because reasons\n"
."AND\n"
."should extend SomeClass because reasons\n"
.') because reasons',
$violations->get(0)->getError()
);
}
}