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

0 commit comments

Comments
 (0)