|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Integration\Framework\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
|
8
|
|
|
use Gacela\Framework\Container\Container; |
|
9
|
|
|
use Gacela\Framework\Gacela; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
final class FactoryServicesTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
protected function tearDown(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$reflection = new ReflectionClass(Gacela::class); |
|
19
|
|
|
$method = $reflection->getMethod('resetCache'); |
|
20
|
|
|
$method->invoke(null); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function test_factory_service_returns_new_instance_each_time(): void |
|
24
|
|
|
{ |
|
25
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
26
|
|
|
$config->addFactory('test-service', static fn (): stdClass => new stdClass()); |
|
27
|
|
|
}); |
|
28
|
|
|
|
|
29
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
30
|
|
|
|
|
31
|
|
|
$instance1 = $container->get('test-service'); |
|
32
|
|
|
$instance2 = $container->get('test-service'); |
|
33
|
|
|
|
|
34
|
|
|
self::assertInstanceOf(stdClass::class, $instance1); |
|
35
|
|
|
self::assertInstanceOf(stdClass::class, $instance2); |
|
36
|
|
|
self::assertNotSame($instance1, $instance2, 'Factory should return a new instance each time'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function test_factory_service_with_dependencies(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$counter = 0; |
|
42
|
|
|
|
|
43
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use (&$counter): void { |
|
44
|
|
|
$config->addFactory('counter-service', static function () use (&$counter): stdClass { |
|
45
|
|
|
++$counter; |
|
46
|
|
|
$obj = new stdClass(); |
|
47
|
|
|
$obj->count = $counter; |
|
48
|
|
|
return $obj; |
|
49
|
|
|
}); |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
53
|
|
|
|
|
54
|
|
|
$instance1 = $container->get('counter-service'); |
|
55
|
|
|
$instance2 = $container->get('counter-service'); |
|
56
|
|
|
$instance3 = $container->get('counter-service'); |
|
57
|
|
|
|
|
58
|
|
|
self::assertSame(1, $instance1->count); |
|
59
|
|
|
self::assertSame(2, $instance2->count); |
|
60
|
|
|
self::assertSame(3, $instance3->count); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function test_multiple_factory_services(): void |
|
64
|
|
|
{ |
|
65
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
66
|
|
|
$config->addFactory('service-a', static fn () => (object)['type' => 'A']); |
|
67
|
|
|
$config->addFactory('service-b', static fn () => (object)['type' => 'B']); |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
71
|
|
|
|
|
72
|
|
|
$serviceA1 = $container->get('service-a'); |
|
73
|
|
|
$serviceA2 = $container->get('service-a'); |
|
74
|
|
|
$serviceB1 = $container->get('service-b'); |
|
75
|
|
|
|
|
76
|
|
|
self::assertSame('A', $serviceA1->type); |
|
77
|
|
|
self::assertSame('A', $serviceA2->type); |
|
78
|
|
|
self::assertSame('B', $serviceB1->type); |
|
79
|
|
|
self::assertNotSame($serviceA1, $serviceA2); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|