Skip to content

Commit f007c2d

Browse files
committed
[Icons] Fix aliases and icon_attributes config types for config/reference.php
1 parent 4800dec commit f007c2d

File tree

3 files changed

+148
-18
lines changed

3 files changed

+148
-18
lines changed

src/Icons/src/DependencyInjection/UXIconsExtension.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\UX\Icons\DependencyInjection;
1313

1414
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent;
15+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1516
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1617
use Symfony\Component\Config\Definition\ConfigurationInterface;
18+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1719
use Symfony\Component\Config\FileLocator;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
@@ -38,10 +40,15 @@ public function getConfigTreeBuilder(): TreeBuilder
3840
->info('The local directory where icons are stored.')
3941
->defaultValue('%kernel.project_dir%/assets/icons')
4042
->end()
41-
->variableNode('default_icon_attributes')
43+
->arrayNode('default_icon_attributes')
4244
->info('Default attributes to add to all icons.')
4345
->defaultValue(['fill' => 'currentColor'])
4446
->example(['class' => 'icon'])
47+
->normalizeKeys(false)
48+
->useAttributeAsKey('key')
49+
->scalarPrototype()
50+
->cannotBeEmpty()
51+
->end()
4552
->end()
4653
->arrayNode('icon_sets')
4754
->info('Icon sets configuration.')
@@ -63,7 +70,9 @@ public function getConfigTreeBuilder(): TreeBuilder
6370
->info('Override default icon attributes for icons in this set.')
6471
->example(['class' => 'icon icon-acme', 'fill' => 'none'])
6572
->normalizeKeys(false)
66-
->variablePrototype()
73+
->useAttributeAsKey('key')
74+
->scalarPrototype()
75+
->cannotBeEmpty()
6776
->end()
6877
->end()
6978
->end()
@@ -80,7 +89,8 @@ public function getConfigTreeBuilder(): TreeBuilder
8089
'privacy' => 'bi:cookie',
8190
])
8291
->normalizeKeys(false)
83-
->scalarPrototype()
92+
->useAttributeAsKey('key')
93+
->{method_exists(ArrayNodeDefinition::class, 'stringPrototype') ? 'stringPrototype' : 'scalarPrototype'}()
8494
->cannotBeEmpty()
8595
->end()
8696
->end()

src/Icons/tests/Fixtures/TestKernel.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ final class TestKernel extends Kernel
2727
{
2828
use MicroKernelTrait;
2929

30+
public array $uxIconsConfig = [
31+
'icon_dir' => '%kernel.project_dir%/tests/Fixtures/icons',
32+
'aliases' => [
33+
'foo' => 'lucide:circle',
34+
'bar' => 'lu:circle-off',
35+
],
36+
'icon_sets' => [
37+
'fla' => [
38+
'path' => '%kernel.project_dir%/tests/Fixtures/images/flags',
39+
],
40+
'lu' => [
41+
'alias' => 'lucide',
42+
],
43+
],
44+
];
45+
3046
public function registerBundles(): iterable
3147
{
3248
yield new FrameworkBundle();
@@ -63,21 +79,7 @@ protected function configureContainer(ContainerConfigurator $container): void
6379
'anonymous_template_directory' => 'components',
6480
]);
6581

66-
$container->extension('ux_icons', [
67-
'icon_dir' => '%kernel.project_dir%/tests/Fixtures/icons',
68-
'aliases' => [
69-
'foo' => 'lucide:circle',
70-
'bar' => 'lu:circle-off',
71-
],
72-
'icon_sets' => [
73-
'fla' => [
74-
'path' => '%kernel.project_dir%/tests/Fixtures/images/flags',
75-
],
76-
'lu' => [
77-
'alias' => 'lucide',
78-
],
79-
],
80-
]);
82+
$container->extension('ux_icons', $this->uxIconsConfig);
8183

8284
$container->services()->set('logger', NullLogger::class);
8385
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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;
13+
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17+
use Symfony\Component\Config\Definition\Processor;
18+
use Symfony\UX\Icons\DependencyInjection\UXIconsExtension;
19+
20+
class UXIconsBundleTest extends TestCase
21+
{
22+
public static function provideTestInvalidAliasConfiguration(): iterable
23+
{
24+
yield ['', 'Invalid type for path "ux_icons.aliases". Expected "array", but got "string"'];
25+
yield [123, 'Invalid type for path "ux_icons.aliases". Expected "array", but got "int'];
26+
yield [false, 'Invalid type for path "ux_icons.aliases". Expected "array", but got "bool"'];
27+
yield [[1, 2, 3], 'Invalid type for path "ux_icons.aliases.0". Expected "string", but got "int"'];
28+
yield [['foo' => 123], 'Invalid type for path "ux_icons.aliases.foo". Expected "string", but got "int"'];
29+
}
30+
31+
/**
32+
* @dataProvider provideTestInvalidAliasConfiguration
33+
*/
34+
#[DataProvider('provideTestInvalidAliasConfiguration')]
35+
public function testInvalidAliasConfiguration(mixed $value, string $expectedMessage)
36+
{
37+
self::expectException(InvalidConfigurationException::class);
38+
self::expectExceptionMessage($expectedMessage);
39+
40+
$processor = new Processor();
41+
$configurableExtension = new UXIconsExtension();
42+
$processor->processConfiguration($configurableExtension, [
43+
[
44+
'aliases' => $value,
45+
]
46+
]);
47+
}
48+
49+
public static function provideTestValidAliasConfiguration(): iterable
50+
{
51+
yield [[]];
52+
yield [['foo' => 'bar']];
53+
}
54+
55+
/**
56+
* @dataProvider provideTestValidAliasConfiguration
57+
*/
58+
#[DataProvider('provideTestValidAliasConfiguration')]
59+
public function testValidAliasConfiguration(array $value)
60+
{
61+
$processor = new Processor();
62+
$configurableExtension = new UXIconsExtension();
63+
$processedConfig = $processor->processConfiguration($configurableExtension, [
64+
[
65+
'aliases' => $value,
66+
]
67+
]);
68+
69+
$this->assertSame($value, $processedConfig['aliases']);
70+
}
71+
72+
public static function provideTestInvalidIconAttributesConfiguration(): iterable
73+
{
74+
yield ['', 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "string"'];
75+
yield [123, 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "int"'];
76+
yield [false, 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "bool"'];
77+
}
78+
79+
/**
80+
* @dataProvider provideTestInvalidIconAttributesConfiguration
81+
*/
82+
#[DataProvider('provideTestInvalidIconAttributesConfiguration')]
83+
public function testInvalidIconAttributeConfiguration(mixed $value, string $expectedMessage)
84+
{
85+
self::expectException(InvalidConfigurationException::class);
86+
self::expectExceptionMessage($expectedMessage);
87+
88+
$processor = new Processor();
89+
$configurableExtension = new UXIconsExtension();
90+
$processor->processConfiguration($configurableExtension, [
91+
[
92+
'default_icon_attributes' => $value,
93+
]
94+
]);
95+
}
96+
97+
public static function provideTestValidIconAttributesConfiguration(): iterable
98+
{
99+
yield [[]];
100+
yield [['class' => 'icon-large', 'aria-hidden' => 'true', 'data-test' => 123]];
101+
}
102+
103+
/**
104+
* @dataProvider provideTestValidIconAttributesConfiguration
105+
*/
106+
#[DataProvider('provideTestValidIconAttributesConfiguration')]
107+
public function testValidIconAttributeConfiguration(array $value)
108+
{
109+
$processor = new Processor();
110+
$configurableExtension = new UXIconsExtension();
111+
$processedConfig = $processor->processConfiguration($configurableExtension, [
112+
[
113+
'default_icon_attributes' => $value,
114+
]
115+
]);
116+
$this->assertSame($value, $processedConfig['default_icon_attributes']);
117+
}
118+
}

0 commit comments

Comments
 (0)