Skip to content

Commit

Permalink
Merge pull request #3 from mimmi20/updates
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
mimmi20 authored Nov 19, 2023
2 parents 3d69df6 + 2416624 commit 018a657
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/View/Helper/ViteUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ public function js(
return $view->url($server . '/' . $name, $params, $options, $reuseMatchedParams);
}

try {
$manifest = $this->manifestContents();
} catch (JsonException $e) {
throw new RuntimeException('Could not load manifest file', 0, $e);
}
$manifest = $this->manifestContents();

if (!isset($manifest[$name]['file'])) {
throw new RuntimeException('Unknown Vite entrypoint ' . $name);
Expand Down Expand Up @@ -152,11 +148,7 @@ public function css(
return $view->url($server . '/' . $name, $params, $options, $reuseMatchedParams);
}

try {
$manifest = $this->manifestContents();
} catch (JsonException $e) {
throw new RuntimeException('Could not load manifest file', 0, $e);
}
$manifest = $this->manifestContents();

if (!isset($manifest[$name]['css'])) {
throw new RuntimeException('Unknown Vite CSS entrypoint ' . $name);
Expand Down Expand Up @@ -193,7 +185,6 @@ private function hotServer(): string | null
* @return array<string, array{file: string, imports: array<string, mixed>, css: array<string, mixed>}>
*
* @throws RuntimeException
* @throws JsonException
*/
private function manifestContents(): array
{
Expand All @@ -215,6 +206,14 @@ private function manifestContents(): array
);
}

return json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR);
try {
return json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new RuntimeException(
sprintf('Could not decode Vite manifest at: %s', $manifestPath),
0,
$e,
);
}
}
}
36 changes: 36 additions & 0 deletions tests/View/Helper/ViteUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,40 @@ public function testJsWithManifest4(): void

$object->js($name);
}

/**
* @throws Exception
* @throws RuntimeException
*/
public function testJsWithManifest5(): void
{
$root = vfsStream::setup('root');
$name = 'test.js';
$buildDir = 'test-build-dir';

$file1 = vfsStream::newFile('manifest.json', 0777);
$file1->setContent('{test:');

$dir = vfsStream::newDirectory($buildDir);
$dir->addChild($file1);

$root->addChild($dir);
$manifestPath = $root->url() . '/' . $buildDir . '/manifest.json';

$object = new ViteUrl($root->url(), $buildDir);

$view = $this->createMock(PhpRenderer::class);
$view->expects(self::never())
->method('__call');

$object->setView($view);

$this->expectException(RuntimeException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
sprintf('Could not decode Vite manifest at: %s', $manifestPath),
);

$object->js($name);
}
}

0 comments on commit 018a657

Please sign in to comment.