|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Arkitect\Tests\Integration; |
| 6 | + |
| 7 | +use Arkitect\Expression\ForClasses\HaveNameMatching; |
| 8 | +use Arkitect\Expression\ForClasses\Implement; |
| 9 | +use Arkitect\Rules\Rule; |
| 10 | +use Arkitect\Tests\Utils\TestRunner; |
| 11 | +use org\bovigo\vfs\vfsStream; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class ImplementsTest extends TestCase |
| 15 | +{ |
| 16 | + public function test_naming_is_enforced(): void |
| 17 | + { |
| 18 | + $dir = vfsStream::setup('root', null, $this->createDirStructure())->url(); |
| 19 | + |
| 20 | + $runner = TestRunner::create('8.2'); |
| 21 | + |
| 22 | + $rule = Rule::allClasses() |
| 23 | + ->that(new Implement('App\AnInterface')) |
| 24 | + ->should(new HaveNameMatching('An*')) |
| 25 | + ->because('reasons'); |
| 26 | + |
| 27 | + $runner->run($dir, $rule); |
| 28 | + |
| 29 | + $this->assertCount(0, $runner->getParsingErrors()); |
| 30 | + $this->assertCount(2, $runner->getViolations()); |
| 31 | + |
| 32 | + $this->assertEquals('App\AClass', $runner->getViolations()->get(0)->getFqcn()); |
| 33 | + $this->assertStringContainsString('should have a name that matches An* because reasons', $runner->getViolations()->get(0)->getError()); |
| 34 | + |
| 35 | + $this->assertEquals('App\AEnum', $runner->getViolations()->get(1)->getFqcn()); |
| 36 | + $this->assertStringContainsString('should have a name that matches An* because reasons', $runner->getViolations()->get(1)->getError()); |
| 37 | + } |
| 38 | + |
| 39 | + public function createDirStructure(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + 'App' => [ |
| 43 | + 'AEnum.php' => '<?php |
| 44 | +
|
| 45 | + namespace App; |
| 46 | +
|
| 47 | + enum AEnum implements AnInterface { |
| 48 | + case PENDING; |
| 49 | + case PAYED; |
| 50 | +
|
| 51 | + public function amethod() {} |
| 52 | + } |
| 53 | + ', |
| 54 | + 'OneTrait.php' => '<?php |
| 55 | +
|
| 56 | + namespace App; |
| 57 | +
|
| 58 | + trait OneTrait { |
| 59 | + public function one() {} |
| 60 | + } |
| 61 | + ', |
| 62 | + 'AClass.php' => '<?php |
| 63 | +
|
| 64 | + namespace App; |
| 65 | +
|
| 66 | + class AClass implements AnInterface { |
| 67 | + public function amethod() {} |
| 68 | + } |
| 69 | + ', |
| 70 | + 'AnInterface.php' => '<?php |
| 71 | +
|
| 72 | + namespace App; |
| 73 | +
|
| 74 | + interface AnInterface { |
| 75 | + public function amethod(); |
| 76 | + } |
| 77 | + ', |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments