|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Smeghead\PhpClassDiagram\Config\Options; |
| 7 | +use Smeghead\PhpClassDiagram\DiagramElement\ClassIdentifier; |
| 8 | +use Smeghead\PhpClassDiagram\DiagramElement\Entry; |
| 9 | +use Smeghead\PhpClassDiagram\Php\PhpReader; |
| 10 | + |
| 11 | +final class ClassIdentifierTest extends TestCase |
| 12 | +{ |
| 13 | + private string $fixtureDir; |
| 14 | + |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + $this->fixtureDir = realpath(sprintf('%s/../fixtures', __DIR__)); |
| 18 | + } |
| 19 | + |
| 20 | + public function testClass(): void |
| 21 | + { |
| 22 | + $directory = sprintf('%s/namespace', $this->fixtureDir); |
| 23 | + |
| 24 | + $options = new Options([]); |
| 25 | + $entry = $this->getTargetEntry('product/Product.php', $directory, $options); |
| 26 | + |
| 27 | + $sut = new ClassIdentifier($options, 'product', $entry); |
| 28 | + |
| 29 | + $this->assertSame('class "Product" as product_Product', $sut->getIdentifier()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testInterface(): void |
| 33 | + { |
| 34 | + $directory = sprintf('%s/interface', $this->fixtureDir); |
| 35 | + |
| 36 | + $options = new Options([]); |
| 37 | + $entry = $this->getTargetEntry('product/Interface_.php', $directory, $options); |
| 38 | + |
| 39 | + $sut = new ClassIdentifier($options, 'product', $entry); |
| 40 | + |
| 41 | + $this->assertSame('interface "Interface_" as product_Interface_', $sut->getIdentifier()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testEnum(): void |
| 45 | + { |
| 46 | + $directory = sprintf('%s/enum', $this->fixtureDir); |
| 47 | + |
| 48 | + $options = new Options([]); |
| 49 | + $entry = $this->getTargetEntry('TestEnum.php', $directory, $options); |
| 50 | + |
| 51 | + $sut = new ClassIdentifier($options, '', $entry); |
| 52 | + |
| 53 | + $this->assertSame('enum "Suit\n<b>スート</b>" as Suit', $sut->getIdentifier()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testClassRelTarget(): void |
| 57 | + { |
| 58 | + $directory = sprintf('%s/namespace', $this->fixtureDir); |
| 59 | + |
| 60 | + $options = new Options(['rel-target' => 'Product,Name']); |
| 61 | + $entry = $this->getTargetEntry('product/Product.php', $directory, $options); |
| 62 | + |
| 63 | + $sut = new ClassIdentifier($options, 'product', $entry); |
| 64 | + |
| 65 | + $this->assertSame('class "Product" as product_Product #FFF0F5;line:740125;text:740125', $sut->getIdentifier()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testClassRelTargetNotMatch(): void |
| 69 | + { |
| 70 | + $directory = sprintf('%s/namespace', $this->fixtureDir); |
| 71 | + |
| 72 | + $options = new Options(['rel-target' => 'ProductXX,Name']); |
| 73 | + $entry = $this->getTargetEntry('product/Product.php', $directory, $options); |
| 74 | + |
| 75 | + $sut = new ClassIdentifier($options, 'product', $entry); |
| 76 | + |
| 77 | + $this->assertSame('class "Product" as product_Product', $sut->getIdentifier()); |
| 78 | + } |
| 79 | + |
| 80 | + private function getTargetEntry(string $path, string $directory, Options $options): Entry |
| 81 | + { |
| 82 | + $filename = sprintf('%s/%s', $directory, $path); |
| 83 | + $classes = PhpReader::parseFile($directory, $filename, $options); |
| 84 | + if (count($classes) === 0) { |
| 85 | + throw new \Exception('class not found.'); |
| 86 | + } |
| 87 | + return new Entry(dirname($path), $classes[0]->getInfo(), $options); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments