|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenCodeModelingTest\CodeAst\NodeVisitor; |
| 6 | + |
| 7 | +use OpenCodeModeling\CodeAst\Code\ClassConstGenerator; |
| 8 | +use OpenCodeModeling\CodeAst\Code\ClassGenerator; |
| 9 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassConstant; |
| 10 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassFile; |
| 11 | +use OpenCodeModeling\CodeAst\NodeVisitor\ClassNamespace; |
| 12 | +use OpenCodeModeling\CodeAst\NodeVisitor\StrictType; |
| 13 | +use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\BooleanFactory; |
| 14 | +use PhpParser\NodeTraverser; |
| 15 | +use PhpParser\Parser; |
| 16 | +use PhpParser\ParserFactory; |
| 17 | +use PhpParser\PrettyPrinter\Standard; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +final class ClassConstantTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Parser |
| 24 | + */ |
| 25 | + private $parser; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Standard |
| 29 | + */ |
| 30 | + private $printer; |
| 31 | + |
| 32 | + public function setUp(): void |
| 33 | + { |
| 34 | + $this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); |
| 35 | + $this->printer = new Standard(['shortArraySyntax' => true]); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @test |
| 40 | + */ |
| 41 | + public function it_generates_constant_for_class_for_empty_file(): void |
| 42 | + { |
| 43 | + $ast = $this->parser->parse(''); |
| 44 | + |
| 45 | + $nodeTraverser = new NodeTraverser(); |
| 46 | + $nodeTraverser->addVisitor(new StrictType()); |
| 47 | + $nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass'))); |
| 48 | + $nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string')); |
| 49 | + |
| 50 | + $expected = <<<'EOF' |
| 51 | +<?php |
| 52 | +
|
| 53 | +declare (strict_types=1); |
| 54 | +class TestClass |
| 55 | +{ |
| 56 | + public const TYPE_STRING = 'string'; |
| 57 | +} |
| 58 | +EOF; |
| 59 | + |
| 60 | + $this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @test |
| 65 | + */ |
| 66 | + public function it_generates_constant_for_class_for_existing_file(): void |
| 67 | + { |
| 68 | + $ast = $this->parser->parse('<?php class TestClass {}'); |
| 69 | + |
| 70 | + $nodeTraverser = new NodeTraverser(); |
| 71 | + $nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string', ClassConstGenerator::FLAG_PRIVATE)); |
| 72 | + |
| 73 | + $expected = <<<'EOF' |
| 74 | +<?php |
| 75 | +
|
| 76 | +class TestClass |
| 77 | +{ |
| 78 | + private const TYPE_STRING = 'string'; |
| 79 | +} |
| 80 | +EOF; |
| 81 | + |
| 82 | + $this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @test |
| 87 | + */ |
| 88 | + public function it_generates_constant_for_class_with_namespace_for_empty_file(): void |
| 89 | + { |
| 90 | + $ast = $this->parser->parse(''); |
| 91 | + |
| 92 | + $nodeTraverser = new NodeTraverser(); |
| 93 | + $nodeTraverser->addVisitor(new StrictType()); |
| 94 | + $nodeTraverser->addVisitor(new ClassNamespace('My\\Awesome\\Service')); |
| 95 | + $nodeTraverser->addVisitor(new ClassFile(new ClassGenerator('TestClass'))); |
| 96 | + $nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string')); |
| 97 | + |
| 98 | + $expected = <<<'EOF' |
| 99 | +<?php |
| 100 | +
|
| 101 | +declare (strict_types=1); |
| 102 | +namespace My\Awesome\Service; |
| 103 | +
|
| 104 | +class TestClass |
| 105 | +{ |
| 106 | + public const TYPE_STRING = 'string'; |
| 107 | +} |
| 108 | +EOF; |
| 109 | + |
| 110 | + $this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @test |
| 115 | + */ |
| 116 | + public function it_generates_constant_for_class_with_namespace_for_existing_file(): void |
| 117 | + { |
| 118 | + $code = <<<'PHP' |
| 119 | +<?php |
| 120 | +
|
| 121 | +namespace My\Awesome\Service; |
| 122 | +
|
| 123 | +class TestClass {} |
| 124 | +PHP; |
| 125 | + |
| 126 | + $ast = $this->parser->parse($code); |
| 127 | + |
| 128 | + $nodeTraverser = new NodeTraverser(); |
| 129 | + $nodeTraverser->addVisitor(ClassConstant::forClassConstant('TYPE_STRING', 'string', ClassConstGenerator::FLAG_PRIVATE)); |
| 130 | + |
| 131 | + $expected = <<<'EOF' |
| 132 | +<?php |
| 133 | +
|
| 134 | +namespace My\Awesome\Service; |
| 135 | +
|
| 136 | +class TestClass |
| 137 | +{ |
| 138 | + private const TYPE_STRING = 'string'; |
| 139 | +} |
| 140 | +EOF; |
| 141 | + |
| 142 | + $this->assertSame($expected, $this->printer->prettyPrintFile($nodeTraverser->traverse($ast))); |
| 143 | + } |
| 144 | +} |
0 commit comments