|
15 | 15 |
|
16 | 16 | namespace FastForward\Container\Tests\Factory; |
17 | 17 |
|
| 18 | +use FastForward\Container\Exception\RuntimeException; |
18 | 19 | use FastForward\Container\Factory\CallableFactory; |
| 20 | +use FastForward\Container\Factory\FactoryInterface; |
| 21 | +use Interop\Container\ServiceProviderInterface; |
19 | 22 | use PHPUnit\Framework\Attributes\CoversClass; |
| 23 | +use PHPUnit\Framework\Attributes\UsesClass; |
20 | 24 | use PHPUnit\Framework\TestCase; |
21 | 25 | use Prophecy\PhpUnit\ProphecyTrait; |
22 | 26 | use Psr\Container\ContainerInterface; |
|
25 | 29 | * @internal |
26 | 30 | */ |
27 | 31 | #[CoversClass(CallableFactory::class)] |
| 32 | +#[UsesClass(RuntimeException::class)] |
28 | 33 | final class CallableFactoryTest extends TestCase |
29 | 34 | { |
30 | 35 | use ProphecyTrait; |
31 | 36 |
|
32 | | - public function testInvokeExecutesProvidedClosure(): void |
| 37 | + public function testInvokeWillReturnProvidedCallableReturns(): void |
33 | 38 | { |
34 | 39 | $container = $this->prophesize(ContainerInterface::class)->reveal(); |
35 | 40 |
|
36 | | - $factory = new CallableFactory(static fn (ContainerInterface $c) => (object) ['resolved' => true]); |
| 41 | + $factory = new CallableFactory(fn () => (object) ['resolved' => true]); |
37 | 42 |
|
38 | 43 | $result = $factory($container); |
39 | 44 |
|
40 | 45 | self::assertIsObject($result); |
41 | 46 | self::assertTrue($result->resolved); |
42 | 47 | } |
43 | 48 |
|
44 | | - public function testClosureReceivesContainerAsArgument(): void |
| 49 | + public function testInvokeWillBindToContainer(): void |
45 | 50 | { |
46 | | - $expected = $this->prophesize(ContainerInterface::class)->reveal(); |
| 51 | + $container = $this->prophesize(ContainerInterface::class); |
| 52 | + $expected = $container->reveal(); |
47 | 53 |
|
48 | | - $factory = new CallableFactory(static fn (ContainerInterface $container) => $container); |
| 54 | + $factory = new CallableFactory(fn () => $this); |
49 | 55 |
|
50 | 56 | $actual = $factory($expected); |
51 | 57 |
|
52 | 58 | self::assertSame($expected, $actual); |
53 | 59 | } |
| 60 | + |
| 61 | + public function testClosureReceivesContainerDependenciesAsArgument(): void |
| 62 | + { |
| 63 | + $container = $this->prophesize(ContainerInterface::class); |
| 64 | + $factoryInterface = $this->prophesize(FactoryInterface::class)->reveal(); |
| 65 | + $serviceProvider = $this->prophesize(ServiceProviderInterface::class)->reveal(); |
| 66 | + |
| 67 | + $container->get(ServiceProviderInterface::class)->willReturn($serviceProvider); |
| 68 | + $container->get(FactoryInterface::class)->willReturn($factoryInterface); |
| 69 | + |
| 70 | + $factory = new CallableFactory(fn ( |
| 71 | + ServiceProviderInterface $serviceProvider, |
| 72 | + FactoryInterface $factoryInterface |
| 73 | + ) => compact('serviceProvider', 'factoryInterface')); |
| 74 | + |
| 75 | + $actual = $factory($container->reveal()); |
| 76 | + |
| 77 | + self::assertSame(compact('serviceProvider', 'factoryInterface'), $actual); |
| 78 | + } |
| 79 | + |
| 80 | + public function testInvokeWillThrowRuntimeExceptionIfParameterIsNotAClass(): void |
| 81 | + { |
| 82 | + $container = $this->prophesize(ContainerInterface::class)->reveal(); |
| 83 | + |
| 84 | + $factory = new CallableFactory(fn (string $notAClass) => $notAClass); |
| 85 | + |
| 86 | + $this->expectException(RuntimeException::class); |
| 87 | + |
| 88 | + $factory($container); |
| 89 | + } |
54 | 90 | } |
0 commit comments