|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +use Smeghead\PhpClassDiagram\Config\Options; |
| 8 | +use Smeghead\PhpClassDiagram\DiagramElement\{ |
| 9 | + Relation, |
| 10 | + Entry, |
| 11 | +}; |
| 12 | +use Smeghead\PhpClassDiagram\Php\PhpReader; |
| 13 | + |
| 14 | +final class PackageSvgLink extends TestCase |
| 15 | +{ |
| 16 | + private string $fixtureDir; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + $this->fixtureDir = sprintf('%s/fixtures', __DIR__); |
| 21 | + } |
| 22 | + |
| 23 | + public function testDump_SvgLink_Classes(): void |
| 24 | + { |
| 25 | + $directory = sprintf('%s/namespace', $this->fixtureDir); |
| 26 | + $options = new Options([ |
| 27 | + 'disable-class-properties' => true, |
| 28 | + 'disable-class-methods' => true, |
| 29 | + 'svg-topurl' => 'https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/namespace', |
| 30 | + ]); |
| 31 | + $files = [ |
| 32 | + 'product/Product.php', |
| 33 | + 'product/Price.php', |
| 34 | + 'product/Name.php', |
| 35 | + ]; |
| 36 | + $entries = []; |
| 37 | + foreach ($files as $f) { |
| 38 | + $filename = sprintf('%s/namespace/%s', $this->fixtureDir, $f); |
| 39 | + $classes = PhpReader::parseFile($directory, $filename, $options); |
| 40 | + foreach ($classes as $c) { |
| 41 | + $entries = array_merge($entries, [new Entry('product', $c->getInfo(), $options)]); |
| 42 | + } |
| 43 | + } |
| 44 | + $rel = new Relation($entries, $options); |
| 45 | + |
| 46 | + $expected = <<<EOS |
| 47 | +@startuml class-diagram |
| 48 | + skinparam svgLinkTarget _blank |
| 49 | + skinparam topurl https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/namespace |
| 50 | + package product as product { |
| 51 | + class "Product" as product_Product [[/product/Product.php Product Class]] |
| 52 | + class "Price" as product_Price [[/product/Price.php Price Class]] |
| 53 | + class "Name" as product_Name [[/product/Name.php Name Class]] |
| 54 | + } |
| 55 | + product_Product ..> product_Name |
| 56 | + product_Product ..> product_Price |
| 57 | + product_Product ..> product_Product |
| 58 | +@enduml |
| 59 | +EOS; |
| 60 | + $this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.'); |
| 61 | + } |
| 62 | + |
| 63 | + public function testDump_SvgLink_Interfaces(): void |
| 64 | + { |
| 65 | + $directory = sprintf('%s/interface', $this->fixtureDir); |
| 66 | + $options = new Options([ |
| 67 | + 'disable-class-properties' => true, |
| 68 | + 'enable-class-methods' => true, |
| 69 | + 'svg-topurl' => 'https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/interface', |
| 70 | + ]); |
| 71 | + $files = [ |
| 72 | + 'product/Interface_.php', |
| 73 | + ]; |
| 74 | + $entries = []; |
| 75 | + foreach ($files as $f) { |
| 76 | + $filename = sprintf('%s/%s', $directory, $f); |
| 77 | + $classes = PhpReader::parseFile($directory, $filename, $options); |
| 78 | + foreach ($classes as $c) { |
| 79 | + $entries = array_merge($entries, [new Entry(dirname($f), $c->getInfo(), $options)]); |
| 80 | + } |
| 81 | + } |
| 82 | + $rel = new Relation($entries, $options); |
| 83 | + $expected = <<<EOS |
| 84 | +@startuml class-diagram |
| 85 | + skinparam svgLinkTarget _blank |
| 86 | + skinparam topurl https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/interface |
| 87 | + package product as product { |
| 88 | + interface "Interface_" as product_Interface_ [[/product/Interface_.php Interface_ Interface]] { |
| 89 | + +method1(param1) |
| 90 | + } |
| 91 | + } |
| 92 | +@enduml |
| 93 | +EOS; |
| 94 | + $this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.'); |
| 95 | + } |
| 96 | + public function testDump_SvgLink_ClassesWithoutNamespace(): void |
| 97 | + { |
| 98 | + $directory = sprintf('%s/no-namespace', $this->fixtureDir); |
| 99 | + $options = new Options([ |
| 100 | + 'disable-class-properties' => true, |
| 101 | + 'disable-class-methods' => true, |
| 102 | + 'svg-topurl' => 'https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/no-namespace', |
| 103 | + ]); |
| 104 | + $files = [ |
| 105 | + 'product/Product.php', |
| 106 | + 'product/Price.php', |
| 107 | + 'product/Name.php', |
| 108 | + ]; |
| 109 | + $entries = []; |
| 110 | + foreach ($files as $f) { |
| 111 | + $filename = sprintf('%s/%s', $directory, $f); |
| 112 | + $classes = PhpReader::parseFile($directory, $filename, $options); |
| 113 | + foreach ($classes as $c) { |
| 114 | + $entries = array_merge($entries, [new Entry(dirname($f), $c->getInfo(), $options)]); |
| 115 | + } |
| 116 | + } |
| 117 | + $rel = new Relation($entries, $options); |
| 118 | + |
| 119 | + $expected = <<<EOS |
| 120 | +@startuml class-diagram |
| 121 | + skinparam svgLinkTarget _blank |
| 122 | + skinparam topurl https://github.com/smeghead/php-class-diagram/tree/main/test/fixtures/no-namespace |
| 123 | + package product as product { |
| 124 | + class "Product" as product_Product [[/product/Product.php Product Class]] |
| 125 | + class "Price" as product_Price [[/product/Price.php Price Class]] |
| 126 | + class "Name" as product_Name [[/product/Name.php Name Class]] |
| 127 | + } |
| 128 | + product_Product ..> product_Name |
| 129 | + product_Product ..> product_Price |
| 130 | + product_Product ..> product_Product |
| 131 | +@enduml |
| 132 | +EOS; |
| 133 | + $this->assertSame($expected, implode(PHP_EOL, $rel->dump()), 'output PlantUML script.'); |
| 134 | + } |
| 135 | +} |
0 commit comments