Skip to content

[Icons] Compute viewBox with iconset data (+tests) #1607

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

Merged
merged 1 commit into from
Mar 10, 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
8 changes: 7 additions & 1 deletion src/Icons/src/Iconify.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ public function fetchIcon(string $prefix, string $name): Icon
throw new IconNotFoundException(sprintf('The icon "%s:%s" does not exist on iconify.design.', $prefix, $name));
}

$height = $data['icons'][$name]['height'] ?? $this->sets()[$prefix]['height'] ?? null;
$width = $data['icons'][$name]['width'] ?? $this->sets()[$prefix]['width'] ?? null;
if (null === $width && null === $height) {
throw new \RuntimeException(sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $name));
}

return new Icon($data['icons'][$name]['body'], [
'viewBox' => sprintf('0 0 %s %s', $data['width'], $data['height']),
'viewBox' => sprintf('0 0 %s %s', $width ?? $height, $height ?? $width),
]);
}

Expand Down
120 changes: 120 additions & 0 deletions src/Icons/tests/Unit/IconifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Icons\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\UX\Icons\Exception\IconNotFoundException;
use Symfony\UX\Icons\Iconify;

/**
* @author Simon André <[email protected]>
*/
class IconifyTest extends TestCase
{
public function testFetchIcon(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [],
]),
new JsonMockResponse([
'icons' => [
'heart' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
'height' => 24,
],
],
]),
]),
);

$icon = $iconify->fetchIcon('bi', 'heart');

$this->assertEquals($icon->getInnerSvg(), '<path d="M0 0h24v24H0z" fill="none"/>');
$this->assertEquals($icon->getAttributes(), ['viewBox' => '0 0 24 24']);
}

public function testFetchIconThrowsWhenIconSetDoesNotExists(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([]),
]),
);

$this->expectException(IconNotFoundException::class);
$this->expectExceptionMessage('The icon "bi:heart" does not exist on iconify.design.');

$iconify->fetchIcon('bi', 'heart');
}

public function testFetchIconUsesIconsetViewBoxHeight(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [
'height' => 17,
],
]),
new JsonMockResponse([
'icons' => [
'heart' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
],
]),
]),
);

$icon = $iconify->fetchIcon('bi', 'heart');

$this->assertIsArray($icon->getAttributes());
$this->assertArrayHasKey('viewBox', $icon->getAttributes());
$this->assertEquals('0 0 17 17', $icon->getAttributes()['viewBox']);
}

public function testFetchIconThrowsWhenViewBoxCannotBeComputed(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [],
]),
new JsonMockResponse([
'icons' => [
'heart' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
],
]),
]),
);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The icon "bi:heart" does not have a width or height.');

$iconify->fetchIcon('bi', 'heart');
}
}