|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of php-fast-forward/container. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @link https://github.com/php-fast-forward/container |
| 12 | + * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 13 | + * @license https://opensource.org/licenses/MIT MIT License |
| 14 | + */ |
| 15 | + |
| 16 | +namespace FastForward\Container\Tests; |
| 17 | + |
| 18 | +use FastForward\Config\ConfigInterface; |
| 19 | +use FastForward\Container\AggregateContainer; |
| 20 | +use FastForward\Container\Exception\NotFoundException; |
| 21 | +use FastForward\Container\Factory\ContainerFactory; |
| 22 | +use PHPUnit\Framework\Attributes\CoversFunction; |
| 23 | +use PHPUnit\Framework\Attributes\Test; |
| 24 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 27 | +use Psr\Container\ContainerInterface; |
| 28 | + |
| 29 | +use function FastForward\Container\container; |
| 30 | + |
| 31 | +/** |
| 32 | + * @internal |
| 33 | + */ |
| 34 | +#[CoversFunction('FastForward\Container\container')] |
| 35 | +#[UsesClass(AggregateContainer::class)] |
| 36 | +#[UsesClass(NotFoundException::class)] |
| 37 | +#[UsesClass(ContainerFactory::class)] |
| 38 | +final class ContainerFunctionTest extends TestCase |
| 39 | +{ |
| 40 | + use ProphecyTrait; |
| 41 | + |
| 42 | + #[Test] |
| 43 | + public function testContainerReturnsAggregateContainerWithDependencies(): void |
| 44 | + { |
| 45 | + $key = uniqid('svc_', true); |
| 46 | + $value = uniqid('val_', true); |
| 47 | + |
| 48 | + $config = $this->prophesize(ConfigInterface::class); |
| 49 | + $config->get('dependencies', [])->willReturn([ |
| 50 | + $key => static fn () => $value, |
| 51 | + ]); |
| 52 | + $config->has($key)->willReturn(false); |
| 53 | + |
| 54 | + $result = container($config->reveal()); |
| 55 | + |
| 56 | + self::assertInstanceOf(AggregateContainer::class, $result); |
| 57 | + self::assertTrue($result->has($key)); |
| 58 | + self::assertSame($value, $result->get($key)); |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + public function testContainerCanMergeExternalContainers(): void |
| 63 | + { |
| 64 | + $key = uniqid('ext_', true); |
| 65 | + $val = uniqid('external_', true); |
| 66 | + |
| 67 | + $config = $this->prophesize(ConfigInterface::class); |
| 68 | + $config->get('dependencies', [])->willReturn([]); |
| 69 | + $config->has($key)->willReturn(false); |
| 70 | + |
| 71 | + $external = $this->prophesize(ContainerInterface::class); |
| 72 | + $external->has($key)->willReturn(true); |
| 73 | + $external->get($key)->willReturn($val); |
| 74 | + |
| 75 | + $result = container($config->reveal(), $external->reveal()); |
| 76 | + |
| 77 | + self::assertInstanceOf(AggregateContainer::class, $result); |
| 78 | + self::assertTrue($result->has($key)); |
| 79 | + self::assertSame($val, $result->get($key)); |
| 80 | + } |
| 81 | +} |
0 commit comments