forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconifyTest.php
366 lines (314 loc) · 11.5 KB
/
IconifyTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?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\Icon;
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', 'xmlns' => 'https://www.w3.org/2000/svg']);
}
public function testFetchIconByAlias(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [],
]),
new JsonMockResponse([
'aliases' => [
'foo' => [
'parent' => 'bar',
],
],
'icons' => [
'bar' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
'height' => 24,
],
],
]),
]),
);
$icon = $iconify->fetchIcon('bi', 'foo');
$this->assertEquals($icon->getInnerSvg(), '<path d="M0 0h24v24H0z" fill="none"/>');
$this->assertEquals($icon->getAttributes(), ['viewBox' => '0 0 24 24', 'xmlns' => 'https://www.w3.org/2000/svg']);
}
public function testFetchIconThrowsWhenIconSetDoesNotExists(): void
{
$iconify = new Iconify(new NullAdapter(), 'https://example.com', 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');
}
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 testFetchIcons(): 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' => 17,
],
'bar' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
'height' => 17,
],
],
]),
]),
);
$icons = $iconify->fetchIcons('bi', ['heart', 'bar']);
$this->assertCount(2, $icons);
$this->assertSame(['bar', 'heart'], array_keys($icons));
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
}
public function testFetchIconsByAliases(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'mdi' => [],
]),
new JsonMockResponse([
'aliases' => [
'capsule' => [
'parent' => 'pill',
],
'sign' => [
'parent' => 'draw',
],
],
'icons' => [
'pill' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
'glasses' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
'draw' => [
'body' => '<path d="M0 0h24v24H0z" fill="none"/>',
],
],
]),
]),
);
$icons = $iconify->fetchIcons('mdi', ['capsule', 'sign', 'glasses']);
$this->assertCount(3, $icons);
$this->assertSame(['capsule', 'glasses', 'sign'], array_keys($icons));
$this->assertContainsOnlyInstancesOf(Icon::class, $icons);
}
public function testFetchIconsThrowsWithInvalidIconNames(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [],
]),
]),
);
$this->expectException(\InvalidArgumentException::class);
$iconify->fetchIcons('bi', ['à', 'foo']);
}
public function testFetchIconsThrowsWithTooManyIcons(): void
{
$iconify = new Iconify(
cache: new NullAdapter(),
endpoint: 'https://example.com',
http: new MockHttpClient([
new JsonMockResponse([
'bi' => [],
]),
]),
);
$this->expectException(\InvalidArgumentException::class);
$iconify->fetchIcons('bi', array_fill(0, 50, '1234567890'));
}
public function testGetMetadata(): void
{
$responseFile = __DIR__.'/../Fixtures/Iconify/collections.json';
$client = $this->createHttpClient(json_decode(file_get_contents($responseFile)));
$iconify = new Iconify(new NullAdapter(), 'https://localhost', $client);
$metadata = $iconify->metadataFor('fa6-solid');
$this->assertSame('Font Awesome Solid', $metadata['name']);
}
/**
* @dataProvider provideChunkCases
*/
public function testChunk(int $maxQueryLength, string $prefix, array $names, array $chunks): void
{
$iconify = new Iconify(
new NullAdapter(),
'https://example.com',
new MockHttpClient([]),
$maxQueryLength,
);
$this->assertSame($chunks, iterator_to_array($iconify->chunk($prefix, $names)));
}
public static function provideChunkCases(): iterable
{
yield 'no icon should make no chunk' => [
10,
'ppppp',
[],
[],
];
yield 'one icon should make one chunk' => [
10,
'ppppp',
['aaaa1'],
[['aaaa1']],
];
yield 'two icons that should make two chunck' => [
10,
'ppppp',
['aa1', 'aa2'],
[['aa1'], ['aa2']],
];
yield 'three icons that should make two chunck' => [
15,
'ppppp',
['aaa1', 'aaa2', 'aaa3'],
[['aaa1', 'aaa2'], ['aaa3']],
];
yield 'four icons that should make two chunck' => [
15,
'ppppp',
['aaaaaaaa1', 'a2', 'a3', 'a4'],
[['aaaaaaaa1'], ['a2', 'a3', 'a4']],
];
}
public function testChunkThrowWithIconPrefixTooLong(): void
{
$iconify = new Iconify(new NullAdapter(), 'https://example.com', new MockHttpClient([]));
$prefix = str_pad('p', 101, 'p');
$name = 'icon';
$this->expectExceptionMessage(\sprintf('The icon prefix "%s" is too long.', $prefix));
// We need to iterate over the iterator to trigger the exception
$result = iterator_to_array($iconify->chunk($prefix, [$name]));
}
public function testChunkThrowWithIconNameTooLong(): void
{
$iconify = new Iconify(new NullAdapter(), 'https://example.com', new MockHttpClient([]));
$prefix = 'prefix';
$name = str_pad('n', 101, 'n');
$this->expectExceptionMessage(\sprintf('The icon name "%s" is too long.', $name));
// We need to iterate over the iterator to trigger the exception
$result = iterator_to_array($iconify->chunk($prefix, [$name]));
}
private function createHttpClient(mixed $data, int $code = 200): MockHttpClient
{
$mockResponse = new JsonMockResponse($data, ['http_code' => $code]);
return new MockHttpClient($mockResponse);
}
}