|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +use Brain\Monkey\Functions; |
5 | 6 | use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Pollora\Asset\Application\Services\MissingBuilds; |
6 | 9 | use Pollora\Asset\Infrastructure\Repositories\AssetContainer; |
7 | 10 | use Pollora\Asset\Infrastructure\Services\ViteManager; |
| 11 | +use Pollora\Asset\UI\Http\MissingBuildNotice; |
8 | 12 |
|
9 | 13 | beforeEach(function (): void { |
| 14 | + // AssetServiceProvider's binding: one record of missing builds per request |
| 15 | + app()->singleton(MissingBuilds::class); |
| 16 | + |
10 | 17 | $this->buildRoot = 'build/vite-manager-test-'.uniqid(); |
11 | 18 | $this->hotFile = sys_get_temp_dir().'/vite-manager-test-'.uniqid().'.hot'; |
12 | 19 |
|
|
69 | 76 | expect($theme->getViteClientHtml())->toContain('https://example.test:5173/@vite/client'); |
70 | 77 | }); |
71 | 78 | }); |
| 79 | + |
| 80 | +/** |
| 81 | + * A container with neither a dev server nor a manifest: the build was never |
| 82 | + * run, failed, or public/build was deleted. Laravel's Vite threw while |
| 83 | + * WordPress booted and every request answered 500 (Pollora/pollora#79). |
| 84 | + */ |
| 85 | +describe('ViteManager without a build', function (): void { |
| 86 | + beforeEach(function (): void { |
| 87 | + $this->unbuilt = new ViteManager(new AssetContainer('theme', [ |
| 88 | + 'hot_file' => sys_get_temp_dir().'/missing-'.uniqid().'.hot', |
| 89 | + 'build_directory' => $this->buildRoot.'/theme', |
| 90 | + 'manifest_path' => 'manifest.json', |
| 91 | + 'base_path' => 'resources/assets/', |
| 92 | + ])); |
| 93 | + }); |
| 94 | + |
| 95 | + it('gives no asset URLs instead of throwing', function (): void { |
| 96 | + expect($this->unbuilt->getAssetUrls(['app.js']))->toBe(['js' => [], 'css' => []]) |
| 97 | + ->and($this->unbuilt->asset('images/logo.svg'))->toBe('') |
| 98 | + ->and($this->unbuilt->isMissingBuild())->toBeTrue(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('records the missing build once, with its manifest', function (): void { |
| 102 | + Log::spy(); |
| 103 | + |
| 104 | + $this->unbuilt->getAssetUrls(['app.js']); |
| 105 | + $this->unbuilt->getAssetUrls(['editor.js']); |
| 106 | + $this->unbuilt->asset('images/logo.svg'); |
| 107 | + |
| 108 | + expect(resolve(MissingBuilds::class)->all())->toBe([ |
| 109 | + 'theme' => public_path($this->buildRoot.'/theme/manifest.json'), |
| 110 | + ]); |
| 111 | + |
| 112 | + Log::shouldHaveReceived('warning')->once(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('records nothing for a built container or one served by the dev server', function (): void { |
| 116 | + $built = new ViteManager(new AssetContainer('plugin.demo', [ |
| 117 | + 'hot_file' => sys_get_temp_dir().'/missing-'.uniqid().'.hot', |
| 118 | + 'build_directory' => $this->buildRoot.'/plugin', |
| 119 | + 'manifest_path' => 'manifest.json', |
| 120 | + ])); |
| 121 | + $hot = new ViteManager(new AssetContainer('theme.hot', [ |
| 122 | + 'hot_file' => $this->hotFile, |
| 123 | + 'build_directory' => $this->buildRoot.'/missing', |
| 124 | + 'manifest_path' => 'manifest.json', |
| 125 | + ])); |
| 126 | + |
| 127 | + expect($built->isMissingBuild())->toBeFalse() |
| 128 | + ->and($hot->isMissingBuild())->toBeFalse() |
| 129 | + ->and(resolve(MissingBuilds::class)->all())->toBe([]); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('MissingBuildNotice', function (): void { |
| 134 | + beforeEach(function (): void { |
| 135 | + Functions\when('current_user_can')->justReturn(true); |
| 136 | + Functions\when('esc_html')->returnArg(); |
| 137 | + Functions\when('esc_html__')->returnArg(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('names the missing build and the commands to run', function (): void { |
| 141 | + resolve(MissingBuilds::class)->record('theme', base_path('public/build/theme/default/manifest.json')); |
| 142 | + resolve(MissingBuilds::class)->record('theme.blocks', base_path('public/build/theme/default/manifest.json')); |
| 143 | + |
| 144 | + ob_start(); |
| 145 | + resolve(MissingBuildNotice::class)->render(); |
| 146 | + $notice = (string) ob_get_clean(); |
| 147 | + |
| 148 | + expect($notice) |
| 149 | + ->toContain('Pollora: assets not built') |
| 150 | + ->toContain('theme, theme.blocks') |
| 151 | + ->and(substr_count($notice, 'public/build/theme/default/manifest.json'))->toBe(1) |
| 152 | + ->and($notice) |
| 153 | + ->toContain('npm install && npm run build') |
| 154 | + ->toContain('php artisan pollora:doctor'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('stays silent when every build is there', function (): void { |
| 158 | + ob_start(); |
| 159 | + resolve(MissingBuildNotice::class)->render(); |
| 160 | + |
| 161 | + expect(ob_get_clean())->toBe(''); |
| 162 | + }); |
| 163 | +}); |
0 commit comments