Skip to content

Commit 36478e3

Browse files
committed
Merge branch '7.1' into 7.2
* 7.1: [TwigBridge] Remove `VersionAwareTest` from `AbstractLayoutTestCase` [DependencyInjection] Add coverage for error cases of `LazyClosure` and `AutowireLocator` [TwigBridge] Fixed a parameterized choice label translation Fix extracting of message from ->trans() method with named params [TwigBridge] Remove usage of Node() instantiations Update security.bg.xlf [Dotenv] Default value can be empty [Emoji] Update data to support emoji 16 Add missing Albanian translations for Security and Validator components [HttpClient] Add `crypto_method` to scoped client options suppress proc_open errors [DependencyInjection] Fix `XmlFileLoader` not respecting when env for services
2 parents 72f787f + 796486e commit 36478e3

File tree

8 files changed

+171
-6
lines changed

8 files changed

+171
-6
lines changed

Loader/XmlFileLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function parseImports(\DOMDocument $xml, string $file, ?\DOMNode $root =
118118
$xpath = new \DOMXPath($xml);
119119
$xpath->registerNamespace('container', self::NS);
120120

121-
if (false === $imports = $xpath->query('.//container:imports/container:import', $root)) {
121+
if (false === $imports = $xpath->query('./container:imports/container:import', $root)) {
122122
return;
123123
}
124124

@@ -134,14 +134,14 @@ private function parseDefinitions(\DOMDocument $xml, string $file, Definition $d
134134
$xpath = new \DOMXPath($xml);
135135
$xpath->registerNamespace('container', self::NS);
136136

137-
if (false === $services = $xpath->query('.//container:services/container:service|.//container:services/container:prototype|.//container:services/container:stack', $root)) {
137+
if (false === $services = $xpath->query('./container:services/container:service|./container:services/container:prototype|./container:services/container:stack', $root)) {
138138
return;
139139
}
140140
$this->setCurrentDir(\dirname($file));
141141

142142
$this->instanceof = [];
143143
$this->isLoadingInstanceof = true;
144-
$instanceof = $xpath->query('.//container:services/container:instanceof', $root);
144+
$instanceof = $xpath->query('./container:services/container:instanceof', $root);
145145
foreach ($instanceof as $service) {
146146
$this->setDefinition((string) $service->getAttribute('id'), $this->parseDefinition($service, $file, new Definition()));
147147
}
@@ -192,7 +192,7 @@ private function getServiceDefaults(\DOMDocument $xml, string $file, ?\DOMNode $
192192
$xpath = new \DOMXPath($xml);
193193
$xpath->registerNamespace('container', self::NS);
194194

195-
if (null === $defaultsNode = $xpath->query('.//container:services/container:defaults', $root)->item(0)) {
195+
if (null === $defaultsNode = $xpath->query('./container:services/container:defaults', $root)->item(0)) {
196196
return new Definition();
197197
}
198198

Tests/Argument/LazyClosureTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\DependencyInjection\Tests\Argument;
13+
14+
use InvalidArgumentException;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\DependencyInjection\Argument\LazyClosure;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Definition;
19+
20+
class LazyClosureTest extends TestCase
21+
{
22+
public function testMagicGetThrows()
23+
{
24+
$closure = new LazyClosure(fn () => null);
25+
26+
$this->expectException(InvalidArgumentException::class);
27+
$this->expectExceptionMessage('Cannot read property "foo" from a lazy closure.');
28+
29+
$closure->foo;
30+
}
31+
32+
public function testThrowsWhenNotUsingInterface()
33+
{
34+
$this->expectException(\RuntimeException::class);
35+
$this->expectExceptionMessage('Cannot create adapter for service "foo" because "Symfony\Component\DependencyInjection\Tests\Argument\LazyClosureTest" is not an interface.');
36+
37+
LazyClosure::getCode('foo', [new \stdClass(), 'bar'], new Definition(LazyClosureTest::class), new ContainerBuilder(), 'foo');
38+
}
39+
40+
public function testThrowsOnNonFunctionalInterface()
41+
{
42+
$this->expectException(\RuntimeException::class);
43+
$this->expectExceptionMessage('Cannot create adapter for service "foo" because interface "Symfony\Component\DependencyInjection\Tests\Argument\NonFunctionalInterface" doesn\'t have exactly one method.');
44+
45+
LazyClosure::getCode('foo', [new \stdClass(), 'bar'], new Definition(NonFunctionalInterface::class), new ContainerBuilder(), 'foo');
46+
}
47+
48+
public function testThrowsOnUnknownMethodInInterface()
49+
{
50+
$this->expectException(\RuntimeException::class);
51+
$this->expectExceptionMessage('Cannot create lazy closure for service "bar" because its corresponding callable is invalid.');
52+
53+
LazyClosure::getCode('bar', [new Definition(FunctionalInterface::class), 'bar'], new Definition(\Closure::class), new ContainerBuilder(), 'bar');
54+
}
55+
}
56+
57+
interface FunctionalInterface
58+
{
59+
public function foo();
60+
}
61+
62+
interface NonFunctionalInterface
63+
{
64+
public function foo();
65+
public function bar();
66+
}

Tests/Attribute/AutowireLocatorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ public function testComplexLocator()
4646
$locator->value,
4747
);
4848
}
49+
50+
public function testInvalidTypeLocator()
51+
{
52+
$this->expectException(\InvalidArgumentException::class);
53+
$this->expectExceptionMessage('"bool" is not a PHP type for key "stdClass".');
54+
55+
new AutowireLocator([
56+
\stdClass::class => true,
57+
]);
58+
}
4959
}

Tests/Fixtures/RemoteCaller.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\DependencyInjection\Tests\Fixtures;
13+
14+
interface RemoteCaller
15+
{
16+
}

Tests/Fixtures/RemoteCallerHttp.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerHttp implements RemoteCaller
15+
{
16+
}

Tests/Fixtures/RemoteCallerSocket.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\DependencyInjection\Tests\Fixtures;
13+
14+
class RemoteCallerSocket implements RemoteCaller
15+
{
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
8+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
9+
10+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"
11+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp"/>
12+
13+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"
14+
class="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
15+
</services>
16+
17+
<when env="dev">
18+
<services>
19+
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller"
20+
alias="Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket"/>
21+
</services>
22+
</when>
23+
</container>

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4646
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
4747
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
48+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller;
49+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp;
50+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket;
4851
use Symfony\Component\ExpressionLanguage\Expression;
4952

5053
class XmlFileLoaderTest extends TestCase
@@ -1277,7 +1280,7 @@ public function testStaticConstructor()
12771280
public function testStaticConstructorWithFactoryThrows()
12781281
{
12791282
$container = new ContainerBuilder();
1280-
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1283+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
12811284

12821285
$this->expectException(LogicException::class);
12831286
$this->expectExceptionMessage('The "static_constructor" service cannot declare a factory as well as a constructor.');
@@ -1338,10 +1341,25 @@ public function testUnknownConstantAsKey()
13381341
public function testDeprecatedTagged()
13391342
{
13401343
$container = new ContainerBuilder();
1341-
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1344+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
13421345

13431346
$this->expectUserDeprecationMessage(\sprintf('Since symfony/dependency-injection 7.2: Type "tagged" is deprecated for tag <argument>, use "tagged_iterator" instead in "%s/xml%sservices_with_deprecated_tagged.xml".', self::$fixturesPath, \DIRECTORY_SEPARATOR));
13441347

13451348
$loader->load('services_with_deprecated_tagged.xml');
13461349
}
1350+
1351+
public function testLoadServicesWithEnvironment()
1352+
{
1353+
$container = new ContainerBuilder();
1354+
1355+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'prod');
1356+
$loader->load('when-env-services.xml');
1357+
1358+
self::assertInstanceOf(RemoteCallerHttp::class, $container->get(RemoteCaller::class));
1359+
1360+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'dev');
1361+
$loader->load('when-env-services.xml');
1362+
1363+
self::assertInstanceOf(RemoteCallerSocket::class, $container->get(RemoteCaller::class));
1364+
}
13471365
}

0 commit comments

Comments
 (0)