Skip to content

Commit 0c63ac0

Browse files
committed
bug #3200 [Icons] Fix aliases and default_attributes config types for config/reference.php (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [Icons] Fix `aliases` and `default_attributes` config types for `config/reference.php` | Q | A | -------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | no <!-- required for new features, or documentation updates --> | Issues | Fix #3193 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Types are now correct: <img width="1126" height="349" alt="image" src="https://github.com/user-attachments/assets/2d776f45-8062-46dc-aa5c-3fcbd1c389ff" /> Commits ------- 7a5fed1 [Icons] Fix `aliases` and `icon_attributes` config types for `config/reference.php`
2 parents 4800dec + 7a5fed1 commit 0c63ac0

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

src/Icons/src/DependencyInjection/UXIconsExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1718
use Symfony\Component\Config\FileLocator;
@@ -38,10 +39,15 @@ public function getConfigTreeBuilder(): TreeBuilder
3839
->info('The local directory where icons are stored.')
3940
->defaultValue('%kernel.project_dir%/assets/icons')
4041
->end()
41-
->variableNode('default_icon_attributes')
42+
->arrayNode('default_icon_attributes')
4243
->info('Default attributes to add to all icons.')
4344
->defaultValue(['fill' => 'currentColor'])
4445
->example(['class' => 'icon'])
46+
->normalizeKeys(false)
47+
->useAttributeAsKey('key')
48+
->scalarPrototype()
49+
->cannotBeEmpty()
50+
->end()
4551
->end()
4652
->arrayNode('icon_sets')
4753
->info('Icon sets configuration.')
@@ -63,7 +69,9 @@ public function getConfigTreeBuilder(): TreeBuilder
6369
->info('Override default icon attributes for icons in this set.')
6470
->example(['class' => 'icon icon-acme', 'fill' => 'none'])
6571
->normalizeKeys(false)
66-
->variablePrototype()
72+
->useAttributeAsKey('key')
73+
->scalarPrototype()
74+
->cannotBeEmpty()
6775
->end()
6876
->end()
6977
->end()
@@ -80,7 +88,8 @@ public function getConfigTreeBuilder(): TreeBuilder
8088
'privacy' => 'bi:cookie',
8189
])
8290
->normalizeKeys(false)
83-
->scalarPrototype()
91+
->useAttributeAsKey('key')
92+
->{method_exists(ArrayNodeDefinition::class, 'stringPrototype') ? 'stringPrototype' : 'scalarPrototype'}()
8493
->cannotBeEmpty()
8594
->end()
8695
->end()
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\Builder\ArrayNodeDefinition;
17+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18+
use Symfony\Component\Config\Definition\Processor;
19+
use Symfony\UX\Icons\DependencyInjection\UXIconsExtension;
20+
21+
class UXIconsBundleTest extends TestCase
22+
{
23+
public static function provideTestInvalidAliasConfiguration(): iterable
24+
{
25+
yield ['', 'Invalid type for path "ux_icons.aliases". Expected "array", but got "string"'];
26+
yield [123, 'Invalid type for path "ux_icons.aliases". Expected "array", but got "int'];
27+
yield [false, 'Invalid type for path "ux_icons.aliases". Expected "array", but got "bool"'];
28+
29+
if (method_exists(ArrayNodeDefinition::class, 'stringPrototype')) {
30+
yield [[1, 2, 3], 'Invalid type for path "ux_icons.aliases.0". Expected "string", but got "int"'];
31+
yield [['foo' => 123], 'Invalid type for path "ux_icons.aliases.foo". Expected "string", but got "int"'];
32+
}
33+
}
34+
35+
/**
36+
* @dataProvider provideTestInvalidAliasConfiguration
37+
*/
38+
#[DataProvider('provideTestInvalidAliasConfiguration')]
39+
public function testInvalidAliasConfiguration(mixed $value, string $expectedMessage)
40+
{
41+
self::expectException(InvalidConfigurationException::class);
42+
self::expectExceptionMessage($expectedMessage);
43+
44+
$processor = new Processor();
45+
$configurableExtension = new UXIconsExtension();
46+
$processor->processConfiguration($configurableExtension, [
47+
[
48+
'aliases' => $value,
49+
],
50+
]);
51+
}
52+
53+
public static function provideTestValidAliasConfiguration(): iterable
54+
{
55+
yield [[]];
56+
yield [['foo' => 'bar']];
57+
}
58+
59+
/**
60+
* @dataProvider provideTestValidAliasConfiguration
61+
*/
62+
#[DataProvider('provideTestValidAliasConfiguration')]
63+
public function testValidAliasConfiguration(array $value)
64+
{
65+
$processor = new Processor();
66+
$configurableExtension = new UXIconsExtension();
67+
$processedConfig = $processor->processConfiguration($configurableExtension, [
68+
[
69+
'aliases' => $value,
70+
],
71+
]);
72+
73+
$this->assertSame($value, $processedConfig['aliases']);
74+
}
75+
76+
public static function provideTestInvalidIconAttributesConfiguration(): iterable
77+
{
78+
yield ['', 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "string"'];
79+
yield [123, 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "int"'];
80+
yield [false, 'Invalid type for path "ux_icons.default_icon_attributes". Expected "array", but got "bool"'];
81+
}
82+
83+
/**
84+
* @dataProvider provideTestInvalidIconAttributesConfiguration
85+
*/
86+
#[DataProvider('provideTestInvalidIconAttributesConfiguration')]
87+
public function testInvalidIconAttributeConfiguration(mixed $value, string $expectedMessage)
88+
{
89+
self::expectException(InvalidConfigurationException::class);
90+
self::expectExceptionMessage($expectedMessage);
91+
92+
$processor = new Processor();
93+
$configurableExtension = new UXIconsExtension();
94+
$processor->processConfiguration($configurableExtension, [
95+
[
96+
'default_icon_attributes' => $value,
97+
],
98+
]);
99+
}
100+
101+
public static function provideTestValidIconAttributesConfiguration(): iterable
102+
{
103+
yield [[]];
104+
yield [['class' => 'icon-large', 'aria-hidden' => 'true', 'data-test' => 123]];
105+
}
106+
107+
/**
108+
* @dataProvider provideTestValidIconAttributesConfiguration
109+
*/
110+
#[DataProvider('provideTestValidIconAttributesConfiguration')]
111+
public function testValidIconAttributeConfiguration(array $value)
112+
{
113+
$processor = new Processor();
114+
$configurableExtension = new UXIconsExtension();
115+
$processedConfig = $processor->processConfiguration($configurableExtension, [
116+
[
117+
'default_icon_attributes' => $value,
118+
],
119+
]);
120+
$this->assertSame($value, $processedConfig['default_icon_attributes']);
121+
}
122+
}

0 commit comments

Comments
 (0)