Skip to content

Commit 190d8b2

Browse files
committed
add tests
1 parent 8a12573 commit 190d8b2

File tree

12 files changed

+130
-5
lines changed

12 files changed

+130
-5
lines changed

src/Icons/src/Registry/CacheIconRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function isOptional(): bool
7878
return true;
7979
}
8080

81-
public function warmUp(string $cacheDir, string $buildDir = null): array
81+
public function warmUp(string $cacheDir, ?string $buildDir = null): array
8282
{
8383
foreach ($this as $name) {
8484
$this->get($name, refresh: true);

src/Icons/src/Registry/LocalSvgIconRegistry.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,37 @@ public function get(string $name): array
3434

3535
$svg = file_get_contents($filename) ?: throw new \RuntimeException(sprintf('The icon file "%s" could not be read.', $filename));
3636
$doc = new \DOMDocument();
37-
$doc->loadXML($svg);
38-
$svgTag = $doc->firstElementChild;
37+
$doc->preserveWhiteSpace = false;
38+
39+
try {
40+
$doc->loadXML($svg);
41+
} catch (\Throwable $e) {
42+
throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename), previous: $e);
43+
}
44+
45+
$svgElements = $doc->getElementsByTagName('svg');
46+
47+
if (0 === $svgElements->length) {
48+
throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename));
49+
}
50+
51+
if (1 !== $svgElements->length) {
52+
throw new \RuntimeException(sprintf('The icon file "%s" contains more than one SVG.', $filename));
53+
}
54+
55+
$svgElement = $svgElements->item(0) ?? throw new \RuntimeException(sprintf('The icon file "%s" does not contain a valid SVG.', $filename));
56+
3957
$html = '';
4058

41-
foreach ($svgTag->childNodes as $child) {
59+
foreach ($svgElement->childNodes as $child) {
4260
$html .= $doc->saveHTML($child);
4361
}
4462

45-
$allAttributes = array_map(fn (\DOMAttr $a) => $a->value, [...$svgTag->attributes]);
63+
if (!$html) {
64+
throw new \RuntimeException(sprintf('The icon file "%s" contains an empty SVG.', $filename));
65+
}
66+
67+
$allAttributes = array_map(fn (\DOMAttr $a) => $a->value, [...$svgElement->attributes]);
4668
$attributes = [];
4769

4870
if (isset($allAttributes['viewBox'])) {

src/Icons/tests/Fixtures/svg/invalid1.svg

Loading
Lines changed: 6 additions & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 7 additions & 0 deletions
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Icons\Tests\Unit\Registry;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\Icons\Registry\LocalSvgIconRegistry;
16+
17+
/**
18+
* @author Kevin Bond <[email protected]>
19+
*/
20+
final class LocalSvgIconRegistryTest extends TestCase
21+
{
22+
/**
23+
* @dataProvider validSvgProvider
24+
*/
25+
public function testValidSvgs(string $name, array $expectedAttributes, string $expectedContent): void
26+
{
27+
[$content, $attributes] = $this->registry()->get($name);
28+
29+
$this->assertSame($expectedContent, $content);
30+
$this->assertSame($expectedAttributes, $attributes);
31+
}
32+
33+
public static function validSvgProvider(): iterable
34+
{
35+
yield ['valid1', ['viewBox' => '0 0 24 24'], '<path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path>'];
36+
yield ['valid2', ['viewBox' => '0 0 24 24'], '<path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path>'];
37+
yield ['valid3', ['viewBox' => '0 0 24 24'], '<path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path>'];
38+
yield ['valid4', ['viewBox' => '0 0 24 24'], '<path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path>'];
39+
yield ['valid5', ['viewBox' => '0 0 24 24'], '<path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path><g><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></g>'];
40+
}
41+
42+
/**
43+
* @dataProvider invalidSvgProvider
44+
*/
45+
public function testInvalidSvgs(string $name): void
46+
{
47+
$this->expectException(\RuntimeException::class);
48+
49+
$this->registry()->get($name);
50+
}
51+
52+
public static function invalidSvgProvider(): iterable
53+
{
54+
yield ['invalid1'];
55+
yield ['invalid2'];
56+
yield ['invalid3'];
57+
yield ['invalid4'];
58+
}
59+
60+
private function registry(): LocalSvgIconRegistry
61+
{
62+
return new LocalSvgIconRegistry(__DIR__.'/../../Fixtures/svg');
63+
}
64+
}

0 commit comments

Comments
 (0)