$values
*/
function service_locator(array $values): ServiceLocatorArgument
{
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php
index 10f9bff443919..27e363a95dda8 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php
@@ -70,6 +70,7 @@ public function testProcessValue()
new Reference('bar'),
new Reference('baz'),
'some.service' => new Reference('bar'),
+ 'inlines.service' => new Definition(CustomDefinition::class),
]])
->addTag('container.service_locator')
;
@@ -82,6 +83,7 @@ public function testProcessValue()
$this->assertSame(CustomDefinition::class, $locator('bar')::class);
$this->assertSame(CustomDefinition::class, $locator('baz')::class);
$this->assertSame(CustomDefinition::class, $locator('some.service')::class);
+ $this->assertSame(CustomDefinition::class, \get_class($locator('inlines.service')));
}
public function testServiceWithKeyOverwritesPreviousInheritedKey()
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
index 91f8a20dcc224..ae3d1bbe04067 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
@@ -48,6 +48,7 @@
use Symfony\Component\DependencyInjection\Tests\Compiler\AInterface;
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
use Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation;
+use Symfony\Component\DependencyInjection\Tests\Compiler\FooVoid;
use Symfony\Component\DependencyInjection\Tests\Compiler\IInterface;
use Symfony\Component\DependencyInjection\Tests\Compiler\MyCallable;
use Symfony\Component\DependencyInjection\Tests\Compiler\SingleMethodInterface;
@@ -1874,12 +1875,18 @@ public function testAutowireClosure()
public function testLazyClosure()
{
$container = new ContainerBuilder();
- $container->register('closure', 'Closure')
+ $container->register('closure1', 'Closure')
->setPublic('true')
->setFactory(['Closure', 'fromCallable'])
->setLazy(true)
->setArguments([[new Reference('foo'), 'cloneFoo']]);
+ $container->register('closure2', 'Closure')
+ ->setPublic('true')
+ ->setFactory(['Closure', 'fromCallable'])
+ ->setLazy(true)
+ ->setArguments([[new Reference('foo_void'), '__invoke']]);
$container->register('foo', Foo::class);
+ $container->register('foo_void', FooVoid::class);
$container->compile();
$dumper = new PhpDumper($container);
@@ -1890,11 +1897,18 @@ public function testLazyClosure()
$container = new \Symfony_DI_PhpDumper_Test_Lazy_Closure();
$cloned = Foo::$counter;
- $this->assertInstanceOf(\Closure::class, $container->get('closure'));
+ $this->assertInstanceOf(\Closure::class, $container->get('closure1'));
$this->assertSame($cloned, Foo::$counter);
- $this->assertInstanceOf(Foo::class, $container->get('closure')());
+ $this->assertInstanceOf(Foo::class, $container->get('closure1')());
$this->assertSame(1 + $cloned, Foo::$counter);
- $this->assertSame(1, (new \ReflectionFunction($container->get('closure')))->getNumberOfParameters());
+ $this->assertSame(1, (new \ReflectionFunction($container->get('closure1')))->getNumberOfParameters());
+
+ $counter = FooVoid::$counter;
+ $this->assertInstanceOf(\Closure::class, $container->get('closure2'));
+ $this->assertSame($counter, FooVoid::$counter);
+ $container->get('closure2')('Hello');
+ $this->assertSame(1 + $counter, FooVoid::$counter);
+ $this->assertSame(1, (new \ReflectionFunction($container->get('closure2')))->getNumberOfParameters());
}
public function testLazyAutowireAttribute()
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
index c1ee343bcb8b9..e20892769f7f6 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
@@ -17,11 +17,15 @@
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
+use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
@@ -278,6 +282,32 @@ public function testDumpHandlesEnumeration()
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_enumeration.xml'), $dumper->dump());
}
+ /**
+ * @dataProvider provideDefaultClasses
+ */
+ public function testDumpHandlesDefaultAttribute($class, $expectedFile)
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register('foo', $class)
+ ->setPublic(true)
+ ->setAutowired(true)
+ ->setArguments([2 => true]);
+
+ (new AutowirePass())->process($container);
+
+ $dumper = new XmlDumper($container);
+
+ $this->assertSame(file_get_contents(self::$fixturesPath.'/xml/'.$expectedFile), $dumper->dump());
+ }
+
+ public static function provideDefaultClasses()
+ {
+ yield [FooClassWithDefaultArrayAttribute::class, 'services_with_default_array.xml'];
+ yield [FooClassWithDefaultObjectAttribute::class, 'services_with_default_object.xml'];
+ yield [FooClassWithDefaultEnumAttribute::class, 'services_with_default_enumeration.xml'];
+ }
+
public function testDumpServiceWithAbstractArgument()
{
$container = new ContainerBuilder();
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
index 18ed93a430b4e..0b5c125be8c9a 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
@@ -17,12 +17,16 @@
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
+use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute;
+use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
@@ -162,6 +166,34 @@ public function testDumpHandlesEnumeration()
$this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'), $dumper->dump());
}
+ /**
+ * @requires PHP 8.1
+ *
+ * @dataProvider provideDefaultClasses
+ */
+ public function testDumpHandlesDefaultAttribute($class, $expectedFile)
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register('foo', $class)
+ ->setPublic(true)
+ ->setAutowired(true)
+ ->setArguments([2 => true]);
+
+ (new AutowirePass())->process($container);
+
+ $dumper = new YamlDumper($container);
+
+ $this->assertSame(file_get_contents(self::$fixturesPath.'/yaml/'.$expectedFile), $dumper->dump());
+ }
+
+ public static function provideDefaultClasses()
+ {
+ yield [FooClassWithDefaultArrayAttribute::class, 'services_with_default_array.yml'];
+ yield [FooClassWithDefaultObjectAttribute::class, 'services_with_default_object.yml'];
+ yield [FooClassWithDefaultEnumAttribute::class, 'services_with_default_enumeration.yml'];
+ }
+
public function testDumpServiceWithAbstractArgument()
{
$container = new ContainerBuilder();
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithDefaultArrayAttribute.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithDefaultArrayAttribute.php
new file mode 100644
index 0000000000000..49275212281f1
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooClassWithDefaultArrayAttribute.php
@@ -0,0 +1,12 @@
+ service('foo_service'),
service('bar_service'),
])]);
+
+ $services->set('locator_dependent_inline_service', \ArrayObject::class)
+ ->args([service_locator([
+ 'foo' => inline_service(\stdClass::class),
+ 'bar' => inline_service(\stdClass::class),
+ ])]);
};
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php
index 31eb9d9bb68a5..d75b20bb77315 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php
@@ -38,6 +38,16 @@ public function cloneFoo(\stdClass $bar = null): static
}
}
+class FooVoid
+{
+ public static int $counter = 0;
+
+ public function __invoke(string $name): void
+ {
+ ++self::$counter;
+ }
+}
+
class Bar
{
public function __construct(Foo $foo)
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_closure.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_closure.php
index 3623366544fa0..0af28f2650147 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_closure.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_closure.php
@@ -20,7 +20,8 @@ public function __construct()
{
$this->services = $this->privates = [];
$this->methodMap = [
- 'closure' => 'getClosureService',
+ 'closure1' => 'getClosure1Service',
+ 'closure2' => 'getClosure2Service',
];
$this->aliases = [];
@@ -40,6 +41,7 @@ public function getRemovedIds(): array
{
return [
'foo' => true,
+ 'foo_void' => true,
];
}
@@ -49,12 +51,22 @@ protected function createProxy($class, \Closure $factory)
}
/**
- * Gets the public 'closure' shared service.
+ * Gets the public 'closure1' shared service.
*
* @return \Closure
*/
- protected static function getClosureService($container, $lazyLoad = true)
+ protected static function getClosure1Service($container, $lazyLoad = true)
{
- return $container->services['closure'] = (new class(fn () => new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()) extends \Symfony\Component\DependencyInjection\Argument\LazyClosure { public function cloneFoo(?\stdClass $bar = null): \Symfony\Component\DependencyInjection\Tests\Compiler\Foo { return $this->service->cloneFoo(...\func_get_args()); } })->cloneFoo(...);
+ return $container->services['closure1'] = (new class(fn () => new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()) extends \Symfony\Component\DependencyInjection\Argument\LazyClosure { public function cloneFoo(?\stdClass $bar = null): \Symfony\Component\DependencyInjection\Tests\Compiler\Foo { return $this->service->cloneFoo(...\func_get_args()); } })->cloneFoo(...);
+ }
+
+ /**
+ * Gets the public 'closure2' shared service.
+ *
+ * @return \Closure
+ */
+ protected static function getClosure2Service($container, $lazyLoad = true)
+ {
+ return $container->services['closure2'] = (new class(fn () => new \Symfony\Component\DependencyInjection\Tests\Compiler\FooVoid()) extends \Symfony\Component\DependencyInjection\Argument\LazyClosure { public function __invoke(string $name): void { $this->service->__invoke(...\func_get_args()); } })->__invoke(...);
}
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_array.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_array.xml
new file mode 100644
index 0000000000000..431af77e6bdf5
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_array.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+ true
+
+
+
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_enumeration.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_enumeration.xml
new file mode 100644
index 0000000000000..2248d31bd07b0
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_enumeration.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+ true
+
+
+
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_object.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_object.xml
new file mode 100644
index 0000000000000..fb5c0a8103257
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_default_object.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+ true
+
+
+
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_service_locator_argument.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_service_locator_argument.xml
index f98ca9e5a01d9..773bad5187b72 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_service_locator_argument.xml
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_service_locator_argument.xml
@@ -25,5 +25,16 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy_invalid_alias_definition.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy_invalid_alias_definition.yml
deleted file mode 100644
index 00c011c1ddd09..0000000000000
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/legacy_invalid_alias_definition.yml
+++ /dev/null
@@ -1,5 +0,0 @@
-services:
- foo:
- alias: bar
- factory: foo
- parent: quz
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_array.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_array.yml
new file mode 100644
index 0000000000000..27c13bf95c5a7
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_array.yml
@@ -0,0 +1,11 @@
+
+services:
+ service_container:
+ class: Symfony\Component\DependencyInjection\ContainerInterface
+ public: true
+ synthetic: true
+ foo:
+ class: Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute
+ public: true
+ autowire: true
+ arguments: { secondOptional: true }
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_enumeration.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_enumeration.yml
new file mode 100644
index 0000000000000..15932618f7c4b
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_enumeration.yml
@@ -0,0 +1,11 @@
+
+services:
+ service_container:
+ class: Symfony\Component\DependencyInjection\ContainerInterface
+ public: true
+ synthetic: true
+ foo:
+ class: Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute
+ public: true
+ autowire: true
+ arguments: { secondOptional: true }
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_object.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_object.yml
new file mode 100644
index 0000000000000..014b40aab7158
--- /dev/null
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_default_object.yml
@@ -0,0 +1,11 @@
+
+services:
+ service_container:
+ class: Symfony\Component\DependencyInjection\ContainerInterface
+ public: true
+ synthetic: true
+ foo:
+ class: Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute
+ public: true
+ autowire: true
+ arguments: { secondOptional: true }
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_service_locator_argument.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_service_locator_argument.yml
index b0309d3eeab9a..57570c2d01efa 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_service_locator_argument.yml
+++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_service_locator_argument.yml
@@ -26,3 +26,14 @@ services:
- !service_locator
'foo': '@foo_service'
'0': '@bar_service'
+
+ locator_dependent_inline_service:
+ class: ArrayObject
+ arguments:
+ - !service_locator
+ 'foo':
+ - !service
+ class: stdClass
+ 'bar':
+ - !service
+ class: stdClass
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php
index 7b24f5e2248e6..ec193bce005ec 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php
@@ -19,6 +19,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -231,5 +232,8 @@ public function testServiceWithServiceLocatorArgument()
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
+
+ $values = ['foo' => new Definition(\stdClass::class), 'bar' => new Definition(\stdClass::class)];
+ $this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_inline_service')->getArguments());
}
}
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
index a7c6df66fec3d..7b398277bfda2 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
@@ -447,6 +447,10 @@ public function testServiceWithServiceLocatorArgument()
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
+
+ $inlinedServiceArguments = $container->getDefinition('locator_dependent_inline_service')->getArguments();
+ $this->assertEquals((new Definition(\stdClass::class))->setPublic(false), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['foo']));
+ $this->assertEquals((new Definition(\stdClass::class))->setPublic(false), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['bar']));
}
public function testParseServiceClosure()
diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
index 7027cdb232e3c..2b51ffcca524b 100644
--- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
@@ -441,6 +441,10 @@ public function testServiceWithServiceLocatorArgument()
$values = ['foo' => new Reference('foo_service'), 0 => new Reference('bar_service')];
$this->assertEquals([new ServiceLocatorArgument($values)], $container->getDefinition('locator_dependent_service_mixed')->getArguments());
+
+ $inlinedServiceArguments = $container->getDefinition('locator_dependent_inline_service')->getArguments();
+ $this->assertEquals(new Definition(\stdClass::class), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['foo'][0]));
+ $this->assertEquals(new Definition(\stdClass::class), $container->getDefinition((string) $inlinedServiceArguments[0]->getValues()['bar'][0]));
}
public function testParseServiceClosure()
diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php
index 274aeee5fc803..8274ee3ee5bf3 100644
--- a/src/Symfony/Component/DomCrawler/Crawler.php
+++ b/src/Symfony/Component/DomCrawler/Crawler.php
@@ -588,7 +588,7 @@ public function innerText(/* bool $normalizeWhitespace = true */): string
$normalizeWhitespace = 1 <= \func_num_args() ? func_get_arg(0) : true;
foreach ($this->getNode(0)->childNodes as $childNode) {
- if (\XML_TEXT_NODE !== $childNode->nodeType) {
+ if (\XML_TEXT_NODE !== $childNode->nodeType && \XML_CDATA_SECTION_NODE !== $childNode->nodeType) {
continue;
}
if (!$normalizeWhitespace) {
diff --git a/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTestCase.php b/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTestCase.php
index e682ff405a349..2a227b10574f9 100644
--- a/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTestCase.php
+++ b/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTestCase.php
@@ -377,6 +377,12 @@ public static function provideInnerTextExamples()
'',
' ',
],
+ [
+ '//*[@id="complex-elements"]/*[@class="six"]',
+ 'console.log("Test JavaScript content");',
+ 'console.log("Test JavaScript content");',
+ ' console.log("Test JavaScript content"); ',
+ ],
];
}
@@ -1311,6 +1317,7 @@ public function createTestCrawler($uri = null)
Parent text Child text Parent text
Child text
Child text Another child
+