|
12 | 12 | namespace Symfony\Reprise\Tests\EventListener; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Asset\Packages; |
| 16 | +use Symfony\Component\Asset\PathPackage; |
| 17 | +use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy; |
| 18 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
15 | 19 | use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
16 | 21 | use Symfony\Component\HttpKernel\Event\FinishRequestEvent; |
17 | 22 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 23 | +use Symfony\Component\HttpKernel\KernelEvents; |
18 | 24 | use Symfony\Reprise\Asset\EntrypointsLookup; |
| 25 | +use Symfony\Reprise\Asset\EntrypointsLookupInterface; |
| 26 | +use Symfony\Reprise\Asset\TagRenderer; |
19 | 27 | use Symfony\Reprise\EventListener\ResetAssetsEventListener; |
20 | 28 |
|
21 | 29 | final class ResetAssetsEventListenerTest extends TestCase |
22 | 30 | { |
23 | | - private function lookup(): EntrypointsLookup |
| 31 | + private function lookup(string $fixture = 'build'): EntrypointsLookup |
24 | 32 | { |
25 | | - return new EntrypointsLookup(__DIR__.'/../fixtures/build/entrypoints.json'); |
| 33 | + return new EntrypointsLookup(__DIR__.'/../fixtures/'.$fixture.'/entrypoints.json'); |
| 34 | + } |
| 35 | + |
| 36 | + private function rendererFor(EntrypointsLookupInterface $lookup): TagRenderer |
| 37 | + { |
| 38 | + return new TagRenderer($lookup, new Packages(new PathPackage('/', new EmptyVersionStrategy()))); |
26 | 39 | } |
27 | 40 |
|
28 | 41 | private function finishRequest(int $requestType): FinishRequestEvent |
29 | 42 | { |
30 | 43 | return new FinishRequestEvent($this->createStub(HttpKernelInterface::class), Request::create('/'), $requestType); |
31 | 44 | } |
32 | 45 |
|
| 46 | + private function exceptionEvent(int $requestType): ExceptionEvent |
| 47 | + { |
| 48 | + return new ExceptionEvent($this->createStub(HttpKernelInterface::class), Request::create('/'), $requestType, new \RuntimeException('boom')); |
| 49 | + } |
| 50 | + |
33 | 51 | public function testResetsDeduplicationWhenTheMainRequestFinishes() |
34 | 52 | { |
35 | 53 | $lookup = $this->lookup(); |
36 | 54 | $lookup->getPreloadFiles('app'); // marks the shared chunk as already returned |
37 | 55 |
|
38 | | - new ResetAssetsEventListener($lookup)->onFinishRequest($this->finishRequest(HttpKernelInterface::MAIN_REQUEST)); |
| 56 | + new ResetAssetsEventListener($lookup, $this->rendererFor($lookup))->onFinishRequest($this->finishRequest(HttpKernelInterface::MAIN_REQUEST)); |
39 | 57 |
|
40 | 58 | // After the reset the shared chunk is offered again to the next request. |
41 | 59 | $this->assertSame(['build/shared-e5f6.js'], $lookup->getPreloadFiles('admin')); |
42 | 60 | } |
43 | 61 |
|
44 | | - public function testIgnoresSubRequests() |
| 62 | + public function testIgnoresSubRequestsOnFinishRequest() |
45 | 63 | { |
46 | 64 | $lookup = $this->lookup(); |
47 | 65 | $lookup->getPreloadFiles('app'); |
48 | 66 |
|
49 | | - new ResetAssetsEventListener($lookup)->onFinishRequest($this->finishRequest(HttpKernelInterface::SUB_REQUEST)); |
| 67 | + new ResetAssetsEventListener($lookup, $this->rendererFor($lookup))->onFinishRequest($this->finishRequest(HttpKernelInterface::SUB_REQUEST)); |
50 | 68 |
|
51 | 69 | // A sub-request finishing must NOT reset -- the shared chunk stays deduplicated. |
52 | 70 | $this->assertSame([], $lookup->getPreloadFiles('admin')); |
53 | 71 | } |
| 72 | + |
| 73 | + public function testResetsDeduplicationWhenAnExceptionIsHandled() |
| 74 | + { |
| 75 | + $lookup = $this->lookup(); |
| 76 | + $lookup->getPreloadFiles('app'); |
| 77 | + |
| 78 | + new ResetAssetsEventListener($lookup, $this->rendererFor($lookup))->onException($this->exceptionEvent(HttpKernelInterface::MAIN_REQUEST)); |
| 79 | + |
| 80 | + $this->assertSame(['build/shared-e5f6.js'], $lookup->getPreloadFiles('admin')); |
| 81 | + } |
| 82 | + |
| 83 | + public function testResetsDeduplicationEvenForSubRequestExceptions() |
| 84 | + { |
| 85 | + // An exception at any level abandons the render, so even a sub-request one resets (unlike FINISH_REQUEST). |
| 86 | + $lookup = $this->lookup(); |
| 87 | + $lookup->getPreloadFiles('app'); |
| 88 | + |
| 89 | + new ResetAssetsEventListener($lookup, $this->rendererFor($lookup))->onException($this->exceptionEvent(HttpKernelInterface::SUB_REQUEST)); |
| 90 | + |
| 91 | + $this->assertSame(['build/shared-e5f6.js'], $lookup->getPreloadFiles('admin')); |
| 92 | + } |
| 93 | + |
| 94 | + public function testResetsTheRendererWhenAnExceptionIsHandled() |
| 95 | + { |
| 96 | + $lookup = $this->lookup('dev'); |
| 97 | + $renderer = $this->rendererFor($lookup); |
| 98 | + $renderer->renderScriptTags('app'); |
| 99 | + $this->assertStringNotContainsString('@vite/client', $renderer->renderScriptTags('app'), 'sanity: not re-injected within the same request'); |
| 100 | + |
| 101 | + new ResetAssetsEventListener($lookup, $renderer)->onException($this->exceptionEvent(HttpKernelInterface::MAIN_REQUEST)); |
| 102 | + |
| 103 | + $this->assertStringContainsString('@vite/client', $renderer->renderScriptTags('app')); |
| 104 | + } |
| 105 | + |
| 106 | + public function testResetsBeforeTheErrorPageIsRendered() |
| 107 | + { |
| 108 | + $lookup = $this->lookup(); |
| 109 | + $renderer = $this->rendererFor($lookup); |
| 110 | + |
| 111 | + $dispatcher = new EventDispatcher(); |
| 112 | + $dispatcher->addSubscriber(new ResetAssetsEventListener($lookup, $renderer)); |
| 113 | + |
| 114 | + // Symfony's ErrorListener renders the error page at priority -128; capture what it would emit. |
| 115 | + $errorPageTags = null; |
| 116 | + $dispatcher->addListener(KernelEvents::EXCEPTION, static function () use (&$errorPageTags, $renderer) { |
| 117 | + $errorPageTags = $renderer->renderScriptTags('app'); |
| 118 | + }, -128); |
| 119 | + |
| 120 | + $full = $renderer->renderScriptTags('app'); |
| 121 | + $dispatcher->dispatch($this->exceptionEvent(HttpKernelInterface::MAIN_REQUEST), KernelEvents::EXCEPTION); |
| 122 | + |
| 123 | + // Reprise's reset (default priority 0) runs before -128, so the error page gets the full tag set. |
| 124 | + $this->assertSame($full, $errorPageTags); |
| 125 | + $this->assertStringContainsString('build/app-a1b2.js', $errorPageTags); |
| 126 | + } |
54 | 127 | } |
0 commit comments