Skip to content

Commit 796486e

Browse files
committed
Merge branch '6.4' into 7.1
* 6.4: [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 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 709c0eb + 4c1b449 commit 796486e

File tree

8 files changed

+170
-5
lines changed

8 files changed

+170
-5
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
4545
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
4646
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
47+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCaller;
48+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerHttp;
49+
use Symfony\Component\DependencyInjection\Tests\Fixtures\RemoteCallerSocket;
4750
use Symfony\Component\ExpressionLanguage\Expression;
4851

4952
class XmlFileLoaderTest extends TestCase
@@ -1272,10 +1275,25 @@ public function testStaticConstructor()
12721275
public function testStaticConstructorWithFactoryThrows()
12731276
{
12741277
$container = new ContainerBuilder();
1275-
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
1278+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath . '/xml'));
12761279

12771280
$this->expectException(LogicException::class);
12781281
$this->expectExceptionMessage('The "static_constructor" service cannot declare a factory as well as a constructor.');
12791282
$loader->load('static_constructor_and_factory.xml');
12801283
}
1284+
1285+
public function testLoadServicesWithEnvironment()
1286+
{
1287+
$container = new ContainerBuilder();
1288+
1289+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'prod');
1290+
$loader->load('when-env-services.xml');
1291+
1292+
self::assertInstanceOf(RemoteCallerHttp::class, $container->get(RemoteCaller::class));
1293+
1294+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'), 'dev');
1295+
$loader->load('when-env-services.xml');
1296+
1297+
self::assertInstanceOf(RemoteCallerSocket::class, $container->get(RemoteCaller::class));
1298+
}
12811299
}

0 commit comments

Comments
 (0)