8000 minor #46835 [DependencyInjection] Show error when a null value is pr… · enumag/symfony@667e635 · GitHub
[go: up one dir, main page]

Skip to content

Commit 667e635

Browse files
minor symfony#46835 [DependencyInjection] Show error when a null value is provided in the exclude list (alexislefebvre)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Show error when a null value is provided in the exclude list | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Test for symfony#46833 | License | MIT | Doc PR | Avoid an exception when null values are provided in the `$excludePatterns` array. Commits ------- e034be0 [DependencyInjection] Show error when a null value is provided in the exclude list
2 parents 77e8d91 + e034be0 commit 667e635

File tree

7 files changed

+90
-3
lines changed

7 files changed

+90
-3
lines changed

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public function registerClasses(Definition $prototype, string $namespace, string
100100
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
101101
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
102102
}
103+
// This can happen with YAML files
104+
if (\is_array($exclude) && \in_array(null, $exclude, true)) {
105+
throw new InvalidArgumentException('The exclude list must not contain a "null" value.');
106+
}
107+
// This can happen with XML files
108+
if (\is_array($exclude) && \in_array('', $exclude, true)) {
109+
throw new InvalidArgumentException('The exclude list must not contain an empty value.');
110+
}
103111

104112
$source = \func_num_args() > 4 ? func_get_arg(4) : null;
105113
$autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*">
5+
<exclude>../Prototype/OtherDir</exclude>
6+
<exclude>../Prototype/BadClasses</exclude>
7+
<exclude>../Prototype/SinglyImplementedInterface</exclude>
8+
<exclude></exclude>
9+
</prototype>
10+
</services>
11+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*">
5+
<exclude>../Prototype/OtherDir</exclude>
6+
<exclude>../Prototype/BadClasses</exclude>
7+
<exclude>../Prototype/SinglyImplementedInterface</exclude>
8+
<exclude> </exclude>
9+
</prototype>
10+
</services>
11+
</container>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
3+
resource: ../Prototype
4+
exclude:
5+
- '../Prototype/OtherDir'
6+
# the following node is empty
7+
-
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
3+
resource: ../Prototype
4+
exclude:
5+
- '../Prototype/OtherDir'
6+
# the following has only a space
7+
- # this is a comment to keep the space when editing through PhpStorm

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,11 +744,14 @@ public function testPrototype()
744744
$this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar', $resources);
745745
}
746746

747-
public function testPrototypeExcludeWithArray()
747+
/**
748+
* @dataProvider prototypeExcludeWithArrayDataProvider
749+
*/
750+
public function testPrototypeExcludeWithArray(string $fileName)
748751
{
749752
$container = new ContainerBuilder();
750753
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
751-
$loader->load('services_prototype_array.xml');
754+
$loader->load($fileName);
752755

753756
$ids = array_keys(array_filter($container->getDefinitions(), fn ($def) => !$def->hasTag('container.excluded')));
754757
sort($ids);
@@ -757,7 +760,7 @@ public function testPrototypeExcludeWithArray()
757760
$resources = array_map('strval', $container->getResources());
758761

759762
$fixturesDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR;
760-
$this->assertContains((string) new FileResource($fixturesDir.'xml'.\DIRECTORY_SEPARATOR.'services_prototype_array.xml'), $resources);
763+
$this->assertContains((string) new FileResource($fixturesDir.'xml'.\DIRECTORY_SEPARATOR.$fileName), $resources);
761764

762765
$prototypeRealPath = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'Prototype');
763766
$globResource = new GlobResource(
@@ -776,6 +779,25 @@ public function testPrototypeExcludeWithArray()
776779
$this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar', $resources);
777780
}
778781

782+
public function prototypeExcludeWithArrayDataProvider(): iterable
783+
{
784+
return [
785+
['services_prototype_array.xml'],
786+
// Same config than above but “<exclude> </exclude>” has been added
787+
['services_prototype_array_with_space_node.xml'],
788+
];
789+
}
790+
791+
public function testPrototypeExcludeWithArrayWithEmptyNode()
792+
{
793+
$this->expectException(InvalidArgumentException::class);
794+
$this->expectExceptionMessage('The exclude list must not contain an empty value.');
795+
796+
$container = new ContainerBuilder();
797+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
798+
$loader->load('services_prototype_array_with_empty_node.xml');
799+
}
800+
779801
public function testAliasDefinitionContainsUnsupportedElements()
780802
{
781803
$this->expectException(InvalidArgumentException::class);

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,27 @@ public function testPrototype()
522522
$this->assertContains('reflection.Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar', $resources);
523523
}
524524

525+
/**
526+
* @dataProvider prototypeWithNullOrEmptyNodeDataProvider
527+
*/
528+
public function testPrototypeWithNullOrEmptyNode(string $fileName)
529+
{
530+
$this->expectException(InvalidArgumentException::class);
531+
$this->expectExceptionMessage('The exclude list must not contain a "null" value.');
532+
533+
$container = new ContainerBuilder();
534+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
535+
$loader->load($fileName);
536+
}
537+
538+
public function prototypeWithNullOrEmptyNodeDataProvider(): iterable
539+
{
540+
return [
541+
['services_prototype_with_null_node.yml'],
542+
['services_prototype_with_empty_node.yml'],
543+
];
544+
}
545+
525546
public function testPrototypeWithNamespace()
526547
{
527548
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0