From 64fb7ab10aa8b95f2cb4caa2ccb17360b4367803 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Wed, 13 Nov 2024 18:31:52 +0100 Subject: [PATCH] fix: ignore missing directory in isVendor() --- Factory/MappedAssetFactory.php | 2 +- Tests/Factory/MappedAssetFactoryTest.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Factory/MappedAssetFactory.php b/Factory/MappedAssetFactory.php index 1d2ba70..14f273b 100644 --- a/Factory/MappedAssetFactory.php +++ b/Factory/MappedAssetFactory.php @@ -128,6 +128,6 @@ private function isVendor(string $sourcePath): bool $sourcePath = realpath($sourcePath); $vendorDir = realpath($this->vendorDir); - return $sourcePath && str_starts_with($sourcePath, $vendorDir); + return $sourcePath && $vendorDir && str_starts_with($sourcePath, $vendorDir); } } diff --git a/Tests/Factory/MappedAssetFactoryTest.php b/Tests/Factory/MappedAssetFactoryTest.php index d4e129a..7e3d164 100644 --- a/Tests/Factory/MappedAssetFactoryTest.php +++ b/Tests/Factory/MappedAssetFactoryTest.php @@ -26,6 +26,8 @@ class MappedAssetFactoryTest extends TestCase { + private const DEFAULT_FIXTURES = __DIR__.'/../Fixtures/assets/vendor'; + private AssetMapperInterface&MockObject $assetMapper; public function testCreateMappedAsset() @@ -137,7 +139,15 @@ public function testCreateMappedAssetInVendor() $this->assertTrue($asset->isVendor); } - private function createFactory(?AssetCompilerInterface $extraCompiler = null): MappedAssetFactory + public function testCreateMappedAssetInMissingVendor() + { + $assetMapper = $this->createFactory(null, '/this-path-does-not-exist/'); + $asset = $assetMapper->createMappedAsset('lodash.js', __DIR__.'/../Fixtures/assets/vendor/lodash/lodash.index.js'); + $this->assertSame('lodash.js', $asset->logicalPath); + $this->assertFalse($asset->isVendor); + } + + private function createFactory(?AssetCompilerInterface $extraCompiler = null, ?string $vendorDir = self::DEFAULT_FIXTURES): MappedAssetFactory { $compilers = [ new JavaScriptImportPathCompiler($this->createMock(ImportMapConfigReader::class)), @@ -162,7 +172,7 @@ private function createFactory(?AssetCompilerInterface $extraCompiler = null): M $factory = new MappedAssetFactory( $pathResolver, $compiler, - __DIR__.'/../Fixtures/assets/vendor', + $vendorDir, ); // mock the AssetMapper to behave like normal: by calling back to the factory