Skip to content

Commit e86616a

Browse files
minor #58369 [DependencyInjection] Add coverage for error cases of LazyClosure and AutowireLocator (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [DependencyInjection] Add coverage for error cases of `LazyClosure` and `AutowireLocator` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Commits ------- 5f3459994cd [DependencyInjection] Add coverage for error cases of `LazyClosure` and `AutowireLocator`
2 parents 142dd92 + 5ff5443 commit e86616a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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
}

0 commit comments

Comments
 (0)