Skip to content

Commit

Permalink
bug #2289 [Icons] Patch to handle Iconify API change (smnandre)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

[Icons] Patch to handle Iconify API change

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | Fix #...
| License       | MIT

Until now, the iconify API returned "200" even when the resource (icon data, svg) did not exist.

It will change in [the next days](#2272 (comment))

This patch allow the `ux:icon:lock` and the CacheWarmer to work after this change.

Commits
-------

ed1d7d7 [Icons] Handle Iconify API change
  • Loading branch information
kbond committed Oct 21, 2024
2 parents c9bf5ee + ed1d7d7 commit 923878b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Icons/src/Iconify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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, '<svg')) {
if (!str_starts_with($svg = $response->getContent(), '<svg')) {
throw new IconNotFoundException(\sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
}

return $content;
return $svg;
}

public function getIconSets(): array
Expand Down
34 changes: 34 additions & 0 deletions src/Icons/tests/Unit/IconifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed(): void
$iconify->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';
Expand All @@ -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]);
Expand Down

0 comments on commit 923878b

Please sign in to comment.