|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Generators\Statements; |
| 4 | + |
| 5 | +use Blueprint\Blueprint; |
| 6 | +use Blueprint\Generators\Statements\InertiaPageGenerator; |
| 7 | +use Blueprint\Lexers\StatementLexer; |
| 8 | +use Blueprint\Tree; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\Attributes\Test; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * @see InertiaPageGenerator |
| 15 | + */ |
| 16 | +final class InertiaPageGeneratorTest extends TestCase |
| 17 | +{ |
| 18 | + private $blueprint; |
| 19 | + |
| 20 | + protected $files; |
| 21 | + |
| 22 | + /** @var InertiaPageGenerator */ |
| 23 | + private $subject; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->subject = new InertiaPageGenerator($this->files); |
| 30 | + |
| 31 | + $this->blueprint = new Blueprint; |
| 32 | + $this->blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new StatementLexer)); |
| 33 | + $this->blueprint->registerGenerator($this->subject); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function output_writes_nothing_for_empty_tree(): void |
| 38 | + { |
| 39 | + $this->filesystem->shouldNotHaveReceived('put'); |
| 40 | + |
| 41 | + $this->assertEquals([], $this->subject->output(new Tree(['controllers' => []]))); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function output_writes_nothing_without_inertia_statements(): void |
| 46 | + { |
| 47 | + $this->filesystem->shouldNotHaveReceived('put'); |
| 48 | + |
| 49 | + $tokens = $this->blueprint->parse($this->fixture('drafts/controllers-only.yaml')); |
| 50 | + $tree = $this->blueprint->analyze($tokens); |
| 51 | + |
| 52 | + $this->assertEquals([], $this->subject->output($tree)); |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + public function output_writes_nothing_when_package_json_is_missing(): void |
| 57 | + { |
| 58 | + $this->filesystem->expects('exists') |
| 59 | + ->with(base_path('package.json')) |
| 60 | + ->andReturnFalse(); |
| 61 | + $this->filesystem->shouldNotHaveReceived('put'); |
| 62 | + |
| 63 | + $tokens = $this->blueprint->parse($this->fixture('drafts/controllers-only.yaml')); |
| 64 | + $tree = $this->blueprint->analyze($tokens); |
| 65 | + |
| 66 | + $this->assertEquals([], $this->subject->output($tree)); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function output_writes_nothing_when_adapter_is_not_found(): void |
| 71 | + { |
| 72 | + $this->filesystem->expects('exists') |
| 73 | + ->with(base_path('package.json')) |
| 74 | + ->andReturnTrue(); |
| 75 | + $this->filesystem->expects('get') |
| 76 | + ->with(base_path('package.json')) |
| 77 | + ->andReturn(''); |
| 78 | + $this->filesystem->shouldNotHaveReceived('put'); |
| 79 | + |
| 80 | + $tokens = $this->blueprint->parse($this->fixture('drafts/controllers-only.yaml')); |
| 81 | + $tree = $this->blueprint->analyze($tokens); |
| 82 | + |
| 83 | + $this->assertEquals([], $this->subject->output($tree)); |
| 84 | + } |
| 85 | + |
| 86 | + #[Test] |
| 87 | + #[DataProvider('inertiaAdaptersDataProvider')] |
| 88 | + public function output_writes_pages_for_inertia_statements($framework, $dependencies, $path, $extension): void |
| 89 | + { |
| 90 | + $this->filesystem->expects('exists') |
| 91 | + ->with(base_path('package.json')) |
| 92 | + ->andReturnTrue(); |
| 93 | + $this->filesystem->expects('get') |
| 94 | + ->with(base_path('package.json')) |
| 95 | + ->andReturn($dependencies); |
| 96 | + $this->filesystem->expects('stub') |
| 97 | + ->with("inertia.$framework.stub") |
| 98 | + ->andReturn($this->stub("inertia.$framework.stub")); |
| 99 | + $this->filesystem->expects('exists') |
| 100 | + ->with($path) |
| 101 | + ->andReturnFalse(); |
| 102 | + $this->filesystem->expects('put') |
| 103 | + ->with($path, $this->fixture('inertia-pages/customer-show' . $extension)); |
| 104 | + |
| 105 | + $tokens = $this->blueprint->parse($this->fixture('drafts/inertia-render.yaml')); |
| 106 | + $tree = $this->blueprint->analyze($tokens); |
| 107 | + $output = $this->subject->output($tree); |
| 108 | + |
| 109 | + $this->assertContains( |
| 110 | + $path, |
| 111 | + $output['created'], |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + #[Test] |
| 116 | + #[DataProvider('inertiaAdaptersDataProvider')] |
| 117 | + public function it_outputs_skipped_pages($framework, $dependencies, $path): void |
| 118 | + { |
| 119 | + $this->filesystem->expects('exists') |
| 120 | + ->with(base_path('package.json')) |
| 121 | + ->andReturnTrue(); |
| 122 | + $this->filesystem->expects('get') |
| 123 | + ->with(base_path('package.json')) |
| 124 | + ->andReturn($dependencies); |
| 125 | + $this->filesystem->expects('stub') |
| 126 | + ->with("inertia.$framework.stub") |
| 127 | + ->andReturn($this->stub("inertia.$framework.stub")); |
| 128 | + $this->filesystem->expects('exists') |
| 129 | + ->with($path) |
| 130 | + ->andReturnTrue(); |
| 131 | + $this->filesystem->expects('put') |
| 132 | + ->never(); |
| 133 | + |
| 134 | + $tokens = $this->blueprint->parse($this->fixture('drafts/inertia-render.yaml')); |
| 135 | + $tree = $this->blueprint->analyze($tokens); |
| 136 | + $ouput = $this->subject->output($tree); |
| 137 | + |
| 138 | + $this->assertEquals([ |
| 139 | + 'skipped' => [ |
| 140 | + $path, |
| 141 | + ], |
| 142 | + ], $ouput); |
| 143 | + } |
| 144 | + |
| 145 | + public static function inertiaAdaptersDataProvider(): array |
| 146 | + { |
| 147 | + return [ |
| 148 | + ['vue', '"@inertiajs/vue3": "^2.0.0"', 'resources/js/Pages/Customer/Show.vue', '.vue'], |
| 149 | + ['react', '"@inertiajs/react": "^2.0.0"', 'resources/js/Pages/Customer/Show.jsx', '.jsx'], |
| 150 | + ['svelte', '"@inertiajs/svelte": "^2.0.0"', 'resources/js/Pages/Customer/Show.svelte', '.svelte'], |
| 151 | + ]; |
| 152 | + } |
| 153 | +} |
0 commit comments