Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit d3fa016

Browse files
author
Nate Wiebe
committed
Add tests for icons
1 parent bb661cd commit d3fa016

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tests/IconTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
use Feather\Icon;
4+
use PHPUnit\Framework\TestCase;
5+
use PHPUnit\Util\Xml\Loader;
6+
7+
class IconTest extends TestCase
8+
{
9+
/**
10+
* @var Icon
11+
*/
12+
private $icon;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $iconContent = '<polyline points="0 12 24 12" />';
18+
19+
protected function setUp(): void
20+
{
21+
$this->icon = new Icon('test', $this->iconContent, []);
22+
}
23+
24+
public function testRender(): void
25+
{
26+
$iconString = \sprintf(
27+
'<svg class="feather feather-%s" aria-hidden="true">%s</svg>',
28+
$this->icon->getName(),
29+
$this->iconContent
30+
);
31+
32+
$this->assertXmlStringEqualsXmlString($iconString, $this->icon->render());
33+
}
34+
35+
public function testToString(): void
36+
{
37+
$this->assertXmlStringEqualsXmlString($this->icon->render(), (string)$this->icon);
38+
}
39+
40+
public function testAttributeFiltering(): void
41+
{
42+
$this->icon->setAttributes(['width' => 24, 'height' => null]);
43+
44+
$iconString = \sprintf(
45+
'<svg class="feather feather-%s" aria-hidden="true" width="24">%s</svg>',
46+
$this->icon->getName(),
47+
$this->iconContent
48+
);
49+
50+
$this->assertXmlStringEqualsXmlString($iconString, $this->icon->render());
51+
}
52+
53+
public function testAttributeEscaping(): void
54+
{
55+
$this->icon->setAttributes(['data-title' => 'Quote " should be escaped']);
56+
57+
$iconString = \sprintf(
58+
'<svg class="feather feather-%s" aria-hidden="true" data-title="Quote &quot; should be escaped">%s</svg>',
59+
$this->icon->getName(),
60+
$this->iconContent
61+
);
62+
63+
$this->assertXmlStringEqualsXmlString($iconString, $this->icon->render());
64+
}
65+
66+
public function testAltText(): void
67+
{
68+
$iconTitleId = 'feather-icon-title';
69+
70+
$this->icon->setAltText('Example alt text');
71+
72+
$iconString = \sprintf(
73+
<<<SVG
74+
<svg aria-labelledby="%s" class="feather feather-%s" role="img">
75+
<title id="%s">%s</title>
76+
%s
77+
</svg>
78+
SVG,
79+
$iconTitleId,
80+
$this->icon->getName(),
81+
$iconTitleId,
82+
$this->icon->getAltText(),
83+
$this->iconContent
84+
);
85+
86+
$this->assertXmlStringEqualsXmlString($iconString, $this->changeSvgTitleId($this->icon->render(), $iconTitleId));
87+
}
88+
89+
private function changeSvgTitleId(string $svg, string $id): string
90+
{
91+
$xmlLoader = new Loader();
92+
$iconXml = $xmlLoader->load($svg);
93+
94+
$svgNode = $iconXml->firstChild;
95+
96+
$labelledByAttribute = $svgNode->attributes->getNamedItem('aria-labelledby');
97+
$labelledByAttribute->nodeValue = $id;
98+
99+
$titleNode = $svgNode->childNodes->item(0);
100+
101+
$idAttribute = $titleNode->attributes->getNamedItem('id');
102+
$idAttribute->nodeValue = $id;
103+
104+
return $iconXml->saveXML();
105+
}
106+
}

0 commit comments

Comments
 (0)