|
| 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 | +} |
0 commit comments