Skip to content

Commit ed1d7d7

Browse files
committed
[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.
1 parent c9bf5ee commit ed1d7d7

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Icons/src/Iconify.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function fetchIcon(string $prefix, string $name): Icon
5555

5656
$response = $this->http->request('GET', \sprintf('/%s.json?icons=%s', $prefix, $name));
5757

58+
if (200 !== $response->getStatusCode()) {
59+
throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
60+
}
61+
5862
try {
5963
$data = $response->toArray();
6064
} catch (JsonException) {
@@ -87,16 +91,17 @@ public function fetchSvg(string $prefix, string $name): string
8791
throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
8892
}
8993

90-
$content = $this->http
91-
->request('GET', \sprintf('/%s/%s.svg', $prefix, $name))
92-
->getContent()
93-
;
94+
$response = $this->http->request('GET', \sprintf('/%s/%s.svg', $prefix, $name));
95+
96+
if (200 !== $response->getStatusCode()) {
97+
throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
98+
}
9499

95-
if (!str_starts_with($content, '<svg')) {
100+
if (!str_starts_with($svg = $response->getContent(), '<svg')) {
96101
throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
97102
}
98103

99-
return $content;
104+
return $svg;
100105
}
101106

102107
public function getIconSets(): array

src/Icons/tests/Unit/IconifyTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed(): void
144144
$iconify->fetchIcon('bi', 'heart');
145145
}
146146

147+
public function testFetchIconThrowsWhenStatusCodeNot200(): void
148+
{
149+
$iconify = new Iconify(
150+
cache: new NullAdapter(),
151+
endpoint: 'https://example.com',
152+
http: new MockHttpClient([
153+
new JsonMockResponse([
154+
'bi' => [],
155+
]),
156+
new JsonMockResponse([], ['http_code' => 404]),
157+
]),
158+
);
159+
160+
$this->expectException(IconNotFoundException::class);
161+
$this->expectExceptionMessage('The icon "bi:heart" does not exist on iconify.design.');
162+
163+
$iconify->fetchIcon('bi', 'heart');
164+
}
165+
147166
public function testGetMetadata(): void
148167
{
149168
$responseFile = __DIR__.'/../Fixtures/Iconify/collections.json';
@@ -170,6 +189,21 @@ public function testFetchSvg(): void
170189
$this->stringContains('-.224l.235-.468ZM6.013 2.06c-.649-.1', $svg);
171190
}
172191

192+
public function testFetchSvgThrowIconNotFoundExceptionWhenStatusCodeNot200(): void
193+
{
194+
$client = new MockHttpClient([
195+
new MockResponse(file_get_contents(__DIR__.'/../Fixtures/Iconify/collections.json'), [
196+
'response_headers' => ['content-type' => 'application/json'],
197+
]),
198+
new MockResponse('', ['http_code' => 404]),
199+
]);
200+
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $client);
201+
202+
$this->expectException(IconNotFoundException::class);
203+
204+
$iconify->fetchSvg('fa6-regular', 'bar');
205+
}
206+
173207
private function createHttpClient(mixed $data, int $code = 200): MockHttpClient
174208
{
175209
$mockResponse = new JsonMockResponse($data, ['http_code' => $code]);

0 commit comments

Comments
 (0)