|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WebProject\PhpOpenApiMockServer\Tests\Unit; |
| 6 | + |
| 7 | +use Codeception\Test\Unit; |
| 8 | +use Psr\Container\ContainerInterface; |
| 9 | +use stdClass; |
| 10 | +use WebProject\PhpOpenApiMockServer\Container\SimpleContainer; |
| 11 | + |
| 12 | +class SimpleContainerFactoryTest extends Unit |
| 13 | +{ |
| 14 | + public function test0ArgClosureFactory(): void |
| 15 | + { |
| 16 | + $container = new SimpleContainer(); |
| 17 | + $container->setFactory('service', static function () { |
| 18 | + return new stdClass(); |
| 19 | + }); |
| 20 | + |
| 21 | + $instance = $container->get('service'); |
| 22 | + self::assertInstanceOf(stdClass::class, $instance); |
| 23 | + } |
| 24 | + |
| 25 | + public function test1ArgClosureFactory(): void |
| 26 | + { |
| 27 | + $container = new SimpleContainer(); |
| 28 | + $container->setFactory('service', function ($container) { |
| 29 | + $this->assertInstanceOf(ContainerInterface::class, $container); |
| 30 | + |
| 31 | + return new stdClass(); |
| 32 | + }); |
| 33 | + |
| 34 | + $instance = $container->get('service'); |
| 35 | + self::assertInstanceOf(stdClass::class, $instance); |
| 36 | + } |
| 37 | + |
| 38 | + public function test2ArgClosureFactory(): void |
| 39 | + { |
| 40 | + $container = new SimpleContainer(); |
| 41 | + $container->setFactory('service', function ($container, $id) { |
| 42 | + $this->assertInstanceOf(ContainerInterface::class, $container); |
| 43 | + $this->assertSame('service', $id); |
| 44 | + |
| 45 | + return new stdClass(); |
| 46 | + }); |
| 47 | + |
| 48 | + $instance = $container->get('service'); |
| 49 | + self::assertInstanceOf(stdClass::class, $instance); |
| 50 | + } |
| 51 | + |
| 52 | + public function testNative0ArgFunction(): void |
| 53 | + { |
| 54 | + $container = new SimpleContainer(); |
| 55 | + $container->setFactory('service', 'time'); |
| 56 | + |
| 57 | + $instance = $container->get('service'); |
| 58 | + self::assertIsInt($instance); |
| 59 | + } |
| 60 | + |
| 61 | + public function testInvokableClassFactory(): void |
| 62 | + { |
| 63 | + $container = new SimpleContainer(); |
| 64 | + $container->setInvokableClass('service', stdClass::class); |
| 65 | + |
| 66 | + $instance = $container->get('service'); |
| 67 | + self::assertInstanceOf(stdClass::class, $instance); |
| 68 | + } |
| 69 | + |
| 70 | + public function test1ArgInvokableObjectFactory(): void |
| 71 | + { |
| 72 | + $container = new SimpleContainer(); |
| 73 | + $factory = new class { |
| 74 | + public function __invoke(ContainerInterface $container): stdClass |
| 75 | + { |
| 76 | + return new stdClass(); |
| 77 | + } |
| 78 | + }; |
| 79 | + $container->setFactory('service', $factory); |
| 80 | + |
| 81 | + $instance = $container->get('service'); |
| 82 | + self::assertInstanceOf(stdClass::class, $instance); |
| 83 | + } |
| 84 | +} |
0 commit comments