Skip to content

Commit 15df6dd

Browse files
committed
[Icons] Add Iconify tests
1 parent fbe4093 commit 15df6dd

File tree

7 files changed

+129
-23
lines changed

7 files changed

+129
-23
lines changed

src/Icons/src/Iconify.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
*/
2727
final class Iconify
2828
{
29+
private const API_BASE_URI = 'https://api.iconify.design';
30+
2931
private HttpClientInterface $http;
3032
private \ArrayObject $sets;
3133

3234
public function __construct(
3335
private CacheInterface $cache,
34-
string $endpoint = 'https://api.iconify.design',
36+
string $endpoint = self::API_BASE_URI,
3537
?HttpClientInterface $http = null,
3638
) {
3739
if (!class_exists(HttpClient::class)) {

src/Icons/src/Svg/Icon.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @internal
1818
*/
19-
final class Icon implements \Stringable, \Serializable, \ArrayAccess
19+
final class Icon implements \Stringable, \Serializable
2020
{
2121
/**
2222
* Transforms a valid icon ID into an icon name.
@@ -212,24 +212,4 @@ public function __unserialize(array $data): void
212212
{
213213
[$this->innerSvg, $this->attributes] = $data;
214214
}
215-
216-
public function offsetExists(mixed $offset): bool
217-
{
218-
return isset($this->attributes[$offset]);
219-
}
220-
221-
public function offsetGet(mixed $offset): mixed
222-
{
223-
return $this->attributes[$offset];
224-
}
225-
226-
public function offsetSet(mixed $offset, mixed $value): void
227-
{
228-
throw new \LogicException('The Icon object is immutable.');
229-
}
230-
231-
public function offsetUnset(mixed $offset): void
232-
{
233-
throw new \LogicException('The Icon object is immutable.');
234-
}
235215
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"fa6-solid": {
3+
"name": "Font Awesome Solid",
4+
"total": 1388,
5+
"version": "6.2.0",
6+
"author": {
7+
"name": "Dave Gandy",
8+
"url": "https://github.com/FortAwesome/Font-Awesome"
9+
},
10+
"license": {
11+
"title": "CC BY 4.0",
12+
"spdx": "CC-BY-4.0",
13+
"url": "https://creativecommons.org/licenses/by/4.0/"
14+
},
15+
"samples": [
16+
"location-pin",
17+
"gem",
18+
"folder"
19+
],
20+
"height": 32,
21+
"displayHeight": 16,
22+
"category": "General",
23+
"palette": false
24+
},
25+
"fa6-regular": {
26+
"name": "Font Awesome Regular",
27+
"total": 163,
28+
"version": "6.2.0",
29+
"author": {
30+
"name": "Dave Gandy",
31+
"url": "https://github.com/FortAwesome/Font-Awesome"
32+
},
33+
"license": {
34+
"title": "CC BY 4.0",
35+
"spdx": "CC-BY-4.0",
36+
"url": "https://creativecommons.org/licenses/by/4.0/"
37+
},
38+
"samples": [
39+
"message",
40+
"clock",
41+
"folder"
42+
],
43+
"height": 32,
44+
"displayHeight": 16,
45+
"category": "General",
46+
"palette": false
47+
}
48+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"prefix": "bi",
3+
"lastModified": 1704439122,
4+
"aliases": {},
5+
"width": 16,
6+
"height": 16,
7+
"icons": {
8+
"heart": {
9+
"body": "<path fill=\"currentColor\" d=\"m8 2.748l-.717-.737C5.6.281 2.514.878 1.4 3.053c-.523 1.023-.641 2.5.314 4.385c.92 1.815 2.834 3.989 6.286 6.357c3.452-2.368 5.365-4.542 6.286-6.357c.955-1.886.838-3.362.314-4.385C13.486.878 10.4.28 8.717 2.01zM8 15C-7.333 4.868 3.279-3.04 7.824 1.143q.09.083.176.171a3 3 0 0 1 .176-.17C12.72-3.042 23.333 4.867 8 15\"/>"
10+
}
11+
}
12+
}
Lines changed: 1 addition & 0 deletions
Loading

src/Icons/tests/Unit/IconifyTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Symfony\Component\Cache\Adapter\NullAdapter;
1616
use Symfony\Component\HttpClient\MockHttpClient;
1717
use Symfony\Component\HttpClient\Response\JsonMockResponse;
18+
use Symfony\Component\HttpClient\Response\MockResponse;
1819
use Symfony\UX\Icons\Exception\IconNotFoundException;
1920
use Symfony\UX\Icons\Iconify;
21+
use Symfony\UX\Icons\Svg\Icon;
2022

2123
/**
2224
* @author Simon André <[email protected]>
@@ -117,4 +119,65 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed(): void
117119

118120
$iconify->fetchIcon('bi', 'heart');
119121
}
122+
123+
public function testGetMetadata(): void
124+
{
125+
$responseFile = __DIR__.'/../Fixtures/Iconify/collections.json';
126+
$client = $this->createHttpClient(json_decode(file_get_contents($responseFile)));
127+
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $client);
128+
129+
$metadata = $iconify->metadataFor('fa6-solid');
130+
$this->assertSame('Font Awesome Solid', $metadata['name']);
131+
}
132+
133+
/**
134+
* @dataProvider provideHttpErrors
135+
*/
136+
public function testGetMetadataWhenHttpError(int $code, string $expectedException): void
137+
{
138+
$mockClient = $this->createHttpClient([], $code);
139+
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $mockClient);
140+
141+
$this->expectException($expectedException);
142+
143+
$iconify->metadataFor('foo');
144+
}
145+
146+
public function testFetchSvg(): void
147+
{
148+
$responseFile = __DIR__.'/../Fixtures/Iconify/icon.svg';
149+
$client = new MockHttpClient(MockResponse::fromFile($responseFile));
150+
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $client);
151+
152+
$svg = $iconify->fetchSvg('foo', 'bar');
153+
154+
$this->assertIsString($svg);
155+
$this->stringContains('-.224l.235-.468ZM6.013 2.06c-.649-.1', $svg);
156+
}
157+
158+
/**
159+
* @dataProvider provideHttpErrors
160+
*/
161+
public function testFetchSvgWhenHttpError(int $code, string $expectedException): void
162+
{
163+
$mockClient = $this->createHttpClient([], $code);
164+
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $mockClient);
165+
166+
$this->expectException($expectedException);
167+
$iconify->fetchSvg('foo', 'bar');
168+
}
169+
170+
public static function provideHttpErrors(): iterable
171+
{
172+
yield [400, IconNotFoundException::class];
173+
yield [404, IconNotFoundException::class];
174+
yield [500, IconNotFoundException::class];
175+
}
176+
177+
private function createHttpClient(mixed $data, int $code = 200): MockHttpClient
178+
{
179+
$mockResponse = new JsonMockResponse($data, ['http_code' => $code]);
180+
181+
return new MockHttpClient($mockResponse);
182+
}
120183
}

src/Icons/tests/Unit/Svg/IconTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testConstructor()
2020
{
2121
$icon = new Icon('foo', ['foo' => 'bar']);
2222
$this->assertSame('foo', $icon->getInnerSvg());
23-
$this->assertSame('bar', $icon['foo']);
23+
$this->assertSame('bar', $icon->getAttributes()['foo']);
2424
}
2525

2626
/**

0 commit comments

Comments
 (0)