|
| 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; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\UX\Icons\Exception\IconNotFoundException; |
| 16 | +use Symfony\UX\Icons\IconRegistryInterface; |
| 17 | +use Symfony\UX\Icons\IconRenderer; |
| 18 | +use Symfony\UX\Icons\Svg\Icon; |
| 19 | +use Symfony\UX\Icons\Tests\Util\InMemoryIconRegistry; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Simon André <[email protected]> |
| 23 | + */ |
| 24 | +class IconRendererTest extends TestCase |
| 25 | +{ |
| 26 | + public function testRenderIcon(): void |
| 27 | + { |
| 28 | + $registry = $this->createRegistry([ |
| 29 | + 'user' => '<circle ', |
| 30 | + ]); |
| 31 | + $iconRenderer = new IconRenderer($registry); |
| 32 | + |
| 33 | + $icon = $iconRenderer->renderIcon('user'); |
| 34 | + |
| 35 | + $this->assertStringContainsString('<svg', $icon); |
| 36 | + $this->assertStringContainsString('<circle', $icon); |
| 37 | + } |
| 38 | + |
| 39 | + public function testRenderIconThrowsExceptionWhenIconNotFound(): void |
| 40 | + { |
| 41 | + $registry = $this->createRegistry([]); |
| 42 | + $iconRenderer = new IconRenderer($registry); |
| 43 | + |
| 44 | + $this->expectException(IconNotFoundException::class); |
| 45 | + |
| 46 | + $iconRenderer->renderIcon('foo'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testRenderIconThrowsExceptionWhenAttributesAreInvalid(): void |
| 50 | + { |
| 51 | + $registry = $this->createRegistry(['foo' => '<path d="M0 0L12 12"/>']); |
| 52 | + $iconRenderer = new IconRenderer($registry); |
| 53 | + |
| 54 | + $this->expectException(\InvalidArgumentException::class); |
| 55 | + |
| 56 | + $iconRenderer->renderIcon('foo', [1, 2, null]); |
| 57 | + } |
| 58 | + |
| 59 | + public function testRenderIconWithAttributes(): void |
| 60 | + { |
| 61 | + $registry = $this->createRegistry([ |
| 62 | + 'foo' => '<path d="M0 0L12 12"/>', |
| 63 | + ]); |
| 64 | + $iconRenderer = new IconRenderer($registry); |
| 65 | + $attributes = ['viewBox' => '0 0 24 24', 'class' => 'icon', 'id' => 'FooBar']; |
| 66 | + |
| 67 | + $svg = $iconRenderer->renderIcon('foo', $attributes); |
| 68 | + |
| 69 | + $this->assertSame('<svg viewBox="0 0 24 24" class="icon" id="FooBar"><path d="M0 0L12 12"/></svg>', $svg); |
| 70 | + } |
| 71 | + |
| 72 | + public function testRenderIconWithDefaultAttributes(): void |
| 73 | + { |
| 74 | + $registry = $this->createRegistry([ |
| 75 | + 'foo' => '<path d="M0 0L12 12"/>', |
| 76 | + ]); |
| 77 | + $iconRenderer = new IconRenderer($registry, ['viewBox' => '0 0 24 24', 'class' => 'icon']); |
| 78 | + |
| 79 | + $svg = $iconRenderer->renderIcon('foo'); |
| 80 | + |
| 81 | + $this->assertSame('<svg viewBox="0 0 24 24" class="icon"><path d="M0 0L12 12"/></svg>', $svg); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dataProvider provideAttributesWithDefaultAttributesCases |
| 86 | + */ |
| 87 | + public function testRenderIconWithAttributesAndDefaultAttributes($iconAttrs, $defaultAttrs, $renderAttr, $expectedTag): void |
| 88 | + { |
| 89 | + $registry = $this->createRegistry([ |
| 90 | + 'foo' => ['', $iconAttrs], |
| 91 | + ]); |
| 92 | + $iconRenderer = new IconRenderer($registry, $defaultAttrs); |
| 93 | + |
| 94 | + $svg = $iconRenderer->renderIcon('foo', $renderAttr); |
| 95 | + $this->assertStringStartsWith($expectedTag, $svg); |
| 96 | + } |
| 97 | + |
| 98 | + public static function provideAttributesWithDefaultAttributesCases() |
| 99 | + { |
| 100 | + yield 'no_attributes' => [ |
| 101 | + [], |
| 102 | + [], |
| 103 | + [], |
| 104 | + '<svg>', |
| 105 | + ]; |
| 106 | + yield 'icon_attributes_are_used' => [ |
| 107 | + ['id' => 'icon'], |
| 108 | + [], |
| 109 | + [], |
| 110 | + '<svg id="icon">', |
| 111 | + ]; |
| 112 | + yield 'default_attributes_are_used' => [ |
| 113 | + [], |
| 114 | + ['id' => 'default'], |
| 115 | + [], |
| 116 | + '<svg id="default">', |
| 117 | + ]; |
| 118 | + yield 'render_attributes_are_used' => [ |
| 119 | + [], |
| 120 | + [], |
| 121 | + ['id' => 'render'], |
| 122 | + '<svg id="render">', |
| 123 | + ]; |
| 124 | + yield 'default_attributes_take_precedence_on_icon' => [ |
| 125 | + ['id' => 'icon'], |
| 126 | + ['id' => 'default'], |
| 127 | + [], |
| 128 | + '<svg id="default">', |
| 129 | + ]; |
| 130 | + yield 'default_attributes_are_merged_with_icon_attributes' => [ |
| 131 | + ['id' => 'icon', 'foo' => 'bar'], |
| 132 | + ['id' => 'default', 'baz' => 'qux'], |
| 133 | + [], |
| 134 | + '<svg id="default" foo="bar" baz="qux">', |
| 135 | + ]; |
| 136 | + yield 'render_attributes_take_precedence_on_default' => [ |
| 137 | + [], |
| 138 | + ['id' => 'default'], |
| 139 | + ['id' => 'render'], |
| 140 | + '<svg id="render">', |
| 141 | + ]; |
| 142 | + yield 'render_attributes_are_merged_with_default_attributes' => [ |
| 143 | + [], |
| 144 | + ['id' => 'default', 'foo' => 'bar'], |
| 145 | + ['id' => 'render', 'baz' => 'qux'], |
| 146 | + '<svg id="render" foo="bar" baz="qux">', |
| 147 | + ]; |
| 148 | + yield 'render_attributes_take_precedence_on_icon' => [ |
| 149 | + ['id' => 'icon'], |
| 150 | + [], |
| 151 | + ['id' => 'render'], |
| 152 | + '<svg id="render">', |
| 153 | + ]; |
| 154 | + yield 'render_attributes_are_merged_with_icon_attributes' => [ |
| 155 | + ['id' => 'icon', 'foo' => 'bar'], |
| 156 | + [], |
| 157 | + ['id' => 'render', 'baz' => 'qux'], |
| 158 | + '<svg id="render" foo="bar" baz="qux">', |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + private function createRegistry(array $icons): IconRegistryInterface |
| 163 | + { |
| 164 | + $registryIcons = []; |
| 165 | + foreach ($icons as $name => $data) { |
| 166 | + $data = (array) $data; |
| 167 | + if (array_is_list($data)) { |
| 168 | + $data = ['innerSvg' => $data[0], 'attributes' => $data[1] ?? []]; |
| 169 | + } |
| 170 | + $registryIcons[$name] = new Icon(...$data); |
| 171 | + } |
| 172 | + |
| 173 | + return new InMemoryIconRegistry($registryIcons); |
| 174 | + } |
| 175 | +} |
0 commit comments