Skip to content

Commit 3bcc9d6

Browse files
committed
feature #1597 [Icons] Add InMemory registry to allow easier tests (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Icons] Add InMemory registry to allow easier tests Will come handy to test the IconRenderer Commits ------- dfd6a2a [Icons] Add InMemory registry to allow easier tests
2 parents feeefa4 + dfd6a2a commit 3bcc9d6

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Registry;
13+
14+
use Symfony\UX\Icons\Exception\IconNotFoundException;
15+
use Symfony\UX\Icons\IconRegistryInterface;
16+
use Symfony\UX\Icons\Svg\Icon;
17+
18+
/**
19+
* @author Simon André <[email protected]>
20+
*/
21+
final class InMemoryIconRegistry implements IconRegistryInterface
22+
{
23+
public function __construct(
24+
/**
25+
* @var array<string, Icon> $icons
26+
*/
27+
private array $icons = [],
28+
) {
29+
}
30+
31+
public function set(string $name, Icon $icon): void
32+
{
33+
$this->icons[$name] = $icon;
34+
}
35+
36+
public function get(string $name): Icon
37+
{
38+
return $this->icons[$name] ?? throw new IconNotFoundException(sprintf('Icon "%s" not found.', $name));
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Exception\IconNotFoundException;
16+
use Symfony\UX\Icons\Registry\InMemoryIconRegistry;
17+
use Symfony\UX\Icons\Svg\Icon;
18+
19+
/**
20+
* @author Simon André <[email protected]>
21+
*/
22+
final class InMemoryIconRegistryTest extends TestCase
23+
{
24+
public function testRegistryConstructor(): void
25+
{
26+
$icon = new Icon('foo', ['bar' => 'foobar']);
27+
$registry = new InMemoryIconRegistry(['foo' => $icon]);
28+
29+
$this->assertSame($icon, $registry->get('foo'));
30+
}
31+
32+
public function testRegistryReplaceIcon(): void
33+
{
34+
$registry = new InMemoryIconRegistry();
35+
$foo = new Icon('foo', []);
36+
$bar = new Icon('bar', []);
37+
38+
$registry->set('foo', $foo);
39+
$this->assertSame($foo, $registry->get('foo'));
40+
41+
$registry->set('bar', $bar);
42+
$this->assertSame($bar, $registry->get('bar'));
43+
44+
$registry->set('bar', $foo);
45+
$this->assertSame($foo, $registry->get('bar'));
46+
}
47+
48+
public function testRegistryThrowsExceptionOnUnknownIcon(): void
49+
{
50+
$this->expectException(IconNotFoundException::class);
51+
$this->expectExceptionMessage('Icon "foo" not found.');
52+
53+
$registry = new InMemoryIconRegistry();
54+
$registry->get('foo');
55+
}
56+
}

0 commit comments

Comments
 (0)