From bf88379b728105893106375397c59716230f9718 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Dec 2024 17:38:13 +0100 Subject: [PATCH] Throw exception if the asset type is not supported --- packages/framework/src/Facades/Vite.php | 5 +++- .../tests/Unit/Facades/ViteFacadeTest.php | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/framework/src/Facades/Vite.php b/packages/framework/src/Facades/Vite.php index d98fdf50eb1..a3db7dd3608 100644 --- a/packages/framework/src/Facades/Vite.php +++ b/packages/framework/src/Facades/Vite.php @@ -5,6 +5,7 @@ namespace Hyde\Facades; use Illuminate\Support\HtmlString; +use InvalidArgumentException; /** * Vite facade for handling Vite-related operations. @@ -33,6 +34,7 @@ public static function assets(array $paths): HtmlString return new HtmlString($html); } + /** @throws InvalidArgumentException If the asset type is not supported. */ protected static function formatAssetPath(string $path): string { if (static::isCssPath($path)) { @@ -43,7 +45,8 @@ protected static function formatAssetPath(string $path): string return static::formatScriptInclude($path); } - return ''; // TODO: Throw an exception? + // We don't know how to handle other asset types, so we throw an exception to let the user know. + throw new InvalidArgumentException("Unsupported asset type for path: '$path'"); } protected static function checkIfViteWasEnabledViaTheServeCommand(): bool diff --git a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php index 80f223688d9..faa90a4676a 100644 --- a/packages/framework/tests/Unit/Facades/ViteFacadeTest.php +++ b/packages/framework/tests/Unit/Facades/ViteFacadeTest.php @@ -8,6 +8,7 @@ use Hyde\Facades\Vite; use Hyde\Testing\CreatesTemporaryFiles; use Illuminate\Support\HtmlString; +use InvalidArgumentException; /** * @covers \Hyde\Facades\Vite @@ -70,9 +71,20 @@ public function testItAlwaysImportsClientModule() $this->assertStringContainsString('', (string) $html); } - public function testItDoesNotIncludeUnknownExtensions() + public function testAssetMethodThrowsExceptionForUnknownExtensions() { - $this->assertSame((string) Vite::assets([]), (string) Vite::assets(['foo.txt'])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Unsupported asset type for path: 'foo.txt'"); + + Vite::asset('foo.txt'); + } + + public function testAssetsMethodThrowsExceptionForUnknownExtensions() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Unsupported asset type for path: 'foo.txt'"); + + Vite::assets(['foo.txt']); } public function testAssetsMethodReturnsHtmlString() @@ -155,15 +167,6 @@ public function testAssetMethodGeneratesCorrectHtmlForCssFile() $this->assertSame($expected, (string) $html); } - public function testAssetMethodDoesNotIncludeUnknownExtensions() - { - $html = Vite::asset('unknown.ext'); - - $expected = ''; - - $this->assertSame($expected, (string) $html); - } - public static function cssFileExtensionsProvider(): array { return [