Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Icons] Patch to handle Iconify API change #2289

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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