|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Arkitect\Tests\Unit\Expressions\ForClasses; |
| 6 | + |
| 7 | +use Arkitect\Analyzer\ClassDescription; |
| 8 | +use Arkitect\Analyzer\FullyQualifiedClassName; |
| 9 | +use Arkitect\Expression\ForClasses\Andx; |
| 10 | +use Arkitect\Expression\ForClasses\Extend; |
| 11 | +use Arkitect\Expression\ForClasses\Implement; |
| 12 | +use Arkitect\Rules\Violations; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class AndxTest extends TestCase |
| 16 | +{ |
| 17 | + public function test_it_should_pass_the_rule(): void |
| 18 | + { |
| 19 | + $interface = 'interface'; |
| 20 | + $class = 'SomeClass'; |
| 21 | + $classDescription = new ClassDescription( |
| 22 | + FullyQualifiedClassName::fromString('HappyIsland'), |
| 23 | + [], |
| 24 | + [FullyQualifiedClassName::fromString($interface)], |
| 25 | + FullyQualifiedClassName::fromString($class), |
| 26 | + false, |
| 27 | + false, |
| 28 | + false, |
| 29 | + false, |
| 30 | + false |
| 31 | + ); |
| 32 | + $implementConstraint = new Implement($interface); |
| 33 | + $extendsConstraint = new Extend($class); |
| 34 | + $andConstraint = new Andx($implementConstraint, $extendsConstraint); |
| 35 | + |
| 36 | + $because = 'reasons'; |
| 37 | + $violations = new Violations(); |
| 38 | + $andConstraint->evaluate($classDescription, $violations, $because); |
| 39 | + |
| 40 | + self::assertEquals(0, $violations->count()); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_it_should_pass_the_rule_when_and_is_empty(): void |
| 44 | + { |
| 45 | + $interface = 'interface'; |
| 46 | + $class = 'SomeClass'; |
| 47 | + $classDescription = new ClassDescription( |
| 48 | + FullyQualifiedClassName::fromString('HappyIsland'), |
| 49 | + [], |
| 50 | + [FullyQualifiedClassName::fromString($interface)], |
| 51 | + FullyQualifiedClassName::fromString($class), |
| 52 | + false, |
| 53 | + false, |
| 54 | + false, |
| 55 | + false, |
| 56 | + false |
| 57 | + ); |
| 58 | + $andConstraint = new Andx(); |
| 59 | + |
| 60 | + $because = 'reasons'; |
| 61 | + $violations = new Violations(); |
| 62 | + $andConstraint->evaluate($classDescription, $violations, $because); |
| 63 | + |
| 64 | + self::assertEquals(0, $violations->count()); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_it_should_not_pass_the_rule(): void |
| 68 | + { |
| 69 | + $interface = 'SomeInterface'; |
| 70 | + $class = 'SomeClass'; |
| 71 | + |
| 72 | + $classDescription = new ClassDescription( |
| 73 | + FullyQualifiedClassName::fromString('HappyIsland'), |
| 74 | + [], |
| 75 | + [FullyQualifiedClassName::fromString($interface)], |
| 76 | + null, |
| 77 | + false, |
| 78 | + false, |
| 79 | + false, |
| 80 | + false, |
| 81 | + false |
| 82 | + ); |
| 83 | + |
| 84 | + $implementConstraint = new Implement($interface); |
| 85 | + $extendsConstraint = new Extend($class); |
| 86 | + $andConstraint = new Andx($implementConstraint, $extendsConstraint); |
| 87 | + |
| 88 | + $because = 'reasons'; |
| 89 | + $violationError = $andConstraint->describe($classDescription, $because)->toString(); |
| 90 | + |
| 91 | + $violations = new Violations(); |
| 92 | + $andConstraint->evaluate($classDescription, $violations, $because); |
| 93 | + self::assertNotEquals(0, $violations->count()); |
| 94 | + |
| 95 | + $this->assertEquals( |
| 96 | + 'all expressions must be true (should implement SomeInterface, should extend SomeClass) because reasons', |
| 97 | + $violationError |
| 98 | + ); |
| 99 | + $this->assertEquals( |
| 100 | + "The class 'HappyIsland' violated the expression should extend SomeClass, but " |
| 101 | + . "all expressions must be true (should implement SomeInterface, should extend SomeClass) because reasons", |
| 102 | + $violations->get(0)->getError() |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments