|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenCodeModelingTest\CodeAst\Code; |
| 6 | + |
| 7 | +use OpenCodeModeling\CodeAst\Code\MethodGenerator; |
| 8 | +use OpenCodeModeling\CodeAst\Code\ParameterGenerator; |
| 9 | +use PhpParser\Parser; |
| 10 | +use PhpParser\ParserFactory; |
| 11 | +use PhpParser\PrettyPrinter\Standard; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +final class MethodGeneratorTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Parser |
| 18 | + */ |
| 19 | + private $parser; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var Standard |
| 23 | + */ |
| 24 | + private $printer; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + $this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); |
| 29 | + $this->printer = new Standard(['shortArraySyntax' => true]); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @test |
| 34 | + */ |
| 35 | + public function it_generates_method_with_doc_block(): void |
| 36 | + { |
| 37 | + $method = new MethodGenerator( |
| 38 | + 'setType', |
| 39 | + [ |
| 40 | + new ParameterGenerator('type', '?string'), |
| 41 | + ] |
| 42 | + ); |
| 43 | + $method->setDocBlockComment('Sets an awesome type'); |
| 44 | + |
| 45 | + $expectedOutput = <<<'EOF' |
| 46 | +<?php |
| 47 | +
|
| 48 | +/** |
| 49 | + * Sets an awesome type |
| 50 | + * |
| 51 | + * @var string|null $type |
| 52 | + */ |
| 53 | +public function setType(?string $type); |
| 54 | +EOF; |
| 55 | + |
| 56 | + $this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()])); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @test |
| 61 | + */ |
| 62 | + public function it_generates_method_with_array_type_doc_block(): void |
| 63 | + { |
| 64 | + $parameter = new ParameterGenerator('items', 'array'); |
| 65 | + $parameter->setTypeDocBlockHint('array<string, \stdClass>'); |
| 66 | + |
| 67 | + $method = new MethodGenerator( |
| 68 | + 'setItems', |
| 69 | + [ |
| 70 | + $parameter, |
| 71 | + ] |
| 72 | + ); |
| 73 | + $method->setDocBlockComment('Sets awesome items'); |
| 74 | + |
| 75 | + $expectedOutput = <<<'EOF' |
| 76 | +<?php |
| 77 | +
|
| 78 | +/** |
| 79 | + * Sets awesome items |
| 80 | + * |
| 81 | + * @var array<string, \stdClass> $items |
| 82 | + */ |
| 83 | +public function setItems(array $items); |
| 84 | +EOF; |
| 85 | + |
| 86 | + $this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()])); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @test |
| 91 | + */ |
| 92 | + public function it_generates_method_with_long_doc_block(): void |
| 93 | + { |
| 94 | + $docBlockComment = <<<'EOF' |
| 95 | +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's |
| 96 | +standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a |
| 97 | +type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, |
| 98 | +remaining essentially unchanged. |
| 99 | +
|
| 100 | +It is a long established fact that a reader will be distracted by the readable content of a page when looking at |
| 101 | +its layout. |
| 102 | +EOF; |
| 103 | + |
| 104 | + |
| 105 | + $method = new MethodGenerator( |
| 106 | + 'setType', |
| 107 | + [ |
| 108 | + new ParameterGenerator('type', 'string'), |
| 109 | + new ParameterGenerator('value', '?int'), |
| 110 | + ] |
| 111 | + ); |
| 112 | + $method->setDocBlockComment($docBlockComment); |
| 113 | + |
| 114 | + $expectedOutput = <<<'EOF' |
| 115 | +<?php |
| 116 | +
|
| 117 | +/** |
| 118 | + * Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's |
| 119 | + * standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a |
| 120 | + * type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, |
| 121 | + * remaining essentially unchanged. |
| 122 | + * |
| 123 | + * It is a long established fact that a reader will be distracted by the readable content of a page when looking at |
| 124 | + * its layout. |
| 125 | + * |
| 126 | + * @var string $type |
| 127 | + * @var int|null $value |
| 128 | + */ |
| 129 | +public function setType(string $type, ?int $value); |
| 130 | +EOF; |
| 131 | + |
| 132 | + $this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()])); |
| 133 | + } |
| 134 | +} |
0 commit comments