From ed1d7d7e3a36955703f6e436f99491aea21894e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 20 Oct 2024 16:02:12 +0200 Subject: [PATCH] [Icons] Handle Iconify API change Until now, the iconify API returned "200" even when the resource (icon data, svg) did not exist. It will change in the next days. This patch allow the `ux:icon:lock` and the CacheWarmer to work after this change. --- src/Icons/src/Iconify.php | 17 +++++++++----- src/Icons/tests/Unit/IconifyTest.php | 34 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/Icons/src/Iconify.php b/src/Icons/src/Iconify.php index d9d3fa15b02..f26b1c7b874 100644 --- a/src/Icons/src/Iconify.php +++ b/src/Icons/src/Iconify.php @@ -55,6 +55,10 @@ public function fetchIcon(string $prefix, string $name): Icon $response = $this->http->request('GET', \sprintf('/%s.json?icons=%s', $prefix, $name)); + if (200 !== $response->getStatusCode()) { + throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name)); + } + try { $data = $response->toArray(); } catch (JsonException) { @@ -87,16 +91,17 @@ public function fetchSvg(string $prefix, string $name): string throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name)); } - $content = $this->http - ->request('GET', \sprintf('/%s/%s.svg', $prefix, $name)) - ->getContent() - ; + $response = $this->http->request('GET', \sprintf('/%s/%s.svg', $prefix, $name)); + + if (200 !== $response->getStatusCode()) { + throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name)); + } - if (!str_starts_with($content, 'getContent(), 'fetchIcon('bi', 'heart'); } + public function testFetchIconThrowsWhenStatusCodeNot200(): void + { + $iconify = new Iconify( + cache: new NullAdapter(), + endpoint: 'https://example.com', + http: new MockHttpClient([ + new JsonMockResponse([ + 'bi' => [], + ]), + new JsonMockResponse([], ['http_code' => 404]), + ]), + ); + + $this->expectException(IconNotFoundException::class); + $this->expectExceptionMessage('The icon "bi:heart" does not exist on iconify.design.'); + + $iconify->fetchIcon('bi', 'heart'); + } + public function testGetMetadata(): void { $responseFile = __DIR__.'/../Fixtures/Iconify/collections.json'; @@ -170,6 +189,21 @@ public function testFetchSvg(): void $this->stringContains('-.224l.235-.468ZM6.013 2.06c-.649-.1', $svg); } + public function testFetchSvgThrowIconNotFoundExceptionWhenStatusCodeNot200(): void + { + $client = new MockHttpClient([ + new MockResponse(file_get_contents(__DIR__.'/../Fixtures/Iconify/collections.json'), [ + 'response_headers' => ['content-type' => 'application/json'], + ]), + new MockResponse('', ['http_code' => 404]), + ]); + $iconify = new Iconify(new NullAdapter(), 'https://localhost', $client); + + $this->expectException(IconNotFoundException::class); + + $iconify->fetchSvg('fa6-regular', 'bar'); + } + private function createHttpClient(mixed $data, int $code = 200): MockHttpClient { $mockResponse = new JsonMockResponse($data, ['http_code' => $code]);