|
| 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\HaveCorrespondingUnit; |
| 10 | +use Arkitect\Rules\Violations; |
| 11 | +use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\Cat; |
| 12 | +use Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\Dog; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class HaveCorrespondingUnitTest extends TestCase |
| 16 | +{ |
| 17 | + public function test_it_should_pass_the_validation(): void |
| 18 | + { |
| 19 | + $class = Cat::class; |
| 20 | + $classDescription = new ClassDescription( |
| 21 | + FullyQualifiedClassName::fromString($class), |
| 22 | + [], |
| 23 | + [], |
| 24 | + null, |
| 25 | + false, |
| 26 | + false, |
| 27 | + false, |
| 28 | + false, |
| 29 | + false |
| 30 | + ); |
| 31 | + $constraint = new HaveCorrespondingUnit( |
| 32 | + function ($fqn) { |
| 33 | + return $fqn.'TestCase'; |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + $because = 'we want all our command handlers to have a test'; |
| 38 | + $violations = new Violations(); |
| 39 | + $constraint->evaluate($classDescription, $violations, $because); |
| 40 | + |
| 41 | + self::assertEquals(0, $violations->count()); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_it_should_return_violation_error(): void |
| 45 | + { |
| 46 | + $class = Dog::class; |
| 47 | + $classDescription = new ClassDescription( |
| 48 | + FullyQualifiedClassName::fromString($class), |
| 49 | + [], |
| 50 | + [], |
| 51 | + null, |
| 52 | + false, |
| 53 | + false, |
| 54 | + false, |
| 55 | + false, |
| 56 | + false |
| 57 | + ); |
| 58 | + $constraint = new HaveCorrespondingUnit( |
| 59 | + function ($fqn) { |
| 60 | + return $fqn.'TestCase'; |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + $because = 'we want all our command handlers to have a test'; |
| 65 | + $violations = new Violations(); |
| 66 | + $constraint->evaluate($classDescription, $violations, $because); |
| 67 | + |
| 68 | + self::assertNotEquals(0, $violations->count()); |
| 69 | + |
| 70 | + $violationError = $constraint->describe($classDescription, $because)->toString(); |
| 71 | + $this->assertEquals( |
| 72 | + 'should have a matching unit named: ' |
| 73 | + ."'Arkitect\Tests\Unit\Expressions\ForClasses\DummyClasses\DogTestCase' because $because", |
| 74 | + $violationError |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments