diff --git a/src/Resolver.php b/src/Resolver.php index f3b918d..7104d2a 100644 --- a/src/Resolver.php +++ b/src/Resolver.php @@ -52,7 +52,8 @@ public function getBrowserPath(string $src, string $presetName): string } $options = []; - foreach ($preset['options'] as $optionName => $optionParams) { + $presetOptions = $preset['options'] ?? []; + foreach ($presetOptions as $optionName => $optionParams) { $option = OptionFactory::fromName($optionName, $optionParams); $options[] = $option->resolve(); } @@ -65,7 +66,7 @@ public function getBrowserPath(string $src, string $presetName): string $source = Encoder::encode($src, true); } - $path = '/'.$options.'/'.$source.$separator.$preset['format']; + $path = '/'.($options ? $options.'/' : '').$source.$separator.$preset['format']; $signature = $this->signer->generateSignature($path); return $this->host.'/'.$signature.$path; diff --git a/tests/ResolverTest.php b/tests/ResolverTest.php index a92e9f4..ede802b 100644 --- a/tests/ResolverTest.php +++ b/tests/ResolverTest.php @@ -76,6 +76,14 @@ public function testWrongPresetName(): void $this->resolver->getBrowserPath('https://fakeimg.pl/350x200/?text=Mezcalito', 'test'); } + #[DataProvider('localUrlFalsyOptions')] + public function testCreateLocalUrlWithFalsyOption(string $src, string $filter, string $result): void + { + $this->addingContainer(); + + $this->assertEquals($result, $this->resolver->getBrowserPath($src, $filter)); + } + public static function plainUrl(): iterable { yield [ @@ -122,6 +130,25 @@ public static function localUrlWithMediaUrl(): iterable ]; } + public static function localUrlFalsyOptions(): iterable + { + yield [ + 'src' => 'image.png', + 'filter' => 'no_options', + 'result' => 'http://localhost:8080/X_PYvMBPKiTcVblydcE_wagAk_jwFeHOM3JoHyiZ5Gk/plain/localhostimage.png@webp', + ]; + yield [ + 'src' => 'image.png', + 'filter' => 'empty_options', + 'result' => 'http://localhost:8080/X_PYvMBPKiTcVblydcE_wagAk_jwFeHOM3JoHyiZ5Gk/plain/localhostimage.png@webp', + ]; + yield [ + 'src' => 'image.png', + 'filter' => 'null_options', + 'result' => 'http://localhost:8080/X_PYvMBPKiTcVblydcE_wagAk_jwFeHOM3JoHyiZ5Gk/plain/localhostimage.png@webp', + ]; + } + private function addingContainer(bool $withMediaUrl = false, bool $withRequest = true): void { $container = $this->createContainer($withMediaUrl); @@ -160,6 +187,17 @@ private function createContainer(bool $withMediaUrl = false): ContainerBuilder 'rotate' => ['angle' => 270], ], ], + 'no_options' => [ + 'format' => 'webp', + ], + 'empty_options' => [ + 'format' => 'webp', + 'options' => [], + ], + 'null_options' => [ + 'format' => 'webp', + 'options' => null, + ], ], ];