Skip to content

Commit b2d6720

Browse files
committed
add tests
1 parent 8a12573 commit b2d6720

File tree

10 files changed

+116
-4
lines changed

10 files changed

+116
-4
lines changed

src/Icons/src/Registry/LocalSvgIconRegistry.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,33 @@ 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+
$allAttributes = array_map(fn (\DOMAttr $a) => $a->value, [...$svgElement->attributes]);
4664
$attributes = [];
4765

4866
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: 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Symfony\UX\Icons\Tests\Unit\Registry;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\UX\Icons\Registry\LocalSvgIconRegistry;
7+
8+
/**
9+
* @author Kevin Bond <[email protected]>
10+
*/
11+
final class LocalSvgIconRegistryTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
* @dataProvider validSvgProvider
16+
*/
17+
public function valid_svgs(string $name, array $expectedAttributes, string $expectedContent): void
18+
{
19+
[$content, $attributes] = $this->registry()->get($name);
20+
21+
$this->assertSame($expectedContent, $content);
22+
$this->assertSame($expectedAttributes, $attributes);
23+
}
24+
25+
public static function validSvgProvider(): iterable
26+
{
27+
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>'];
28+
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>'];
29+
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>'];
30+
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>'];
31+
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>'];
32+
}
33+
34+
/**
35+
* @test
36+
* @dataProvider invalidSvgProvider
37+
*/
38+
public function invalid_svgs(string $name): void
39+
{
40+
$this->expectException(\RuntimeException::class);
41+
42+
$this->registry()->get($name);
43+
}
44+
45+
public static function invalidSvgProvider(): iterable
46+
{
47+
yield ['invalid1'];
48+
yield ['invalid2'];
49+
yield ['invalid3'];
50+
}
51+
52+
private function registry(): LocalSvgIconRegistry
53+
{
54+
return new LocalSvgIconRegistry(__DIR__.'/../../Fixtures/svg');
55+
}
56+
}

0 commit comments

Comments
 (0)