|
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 AliasServicesTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
protected function setUp(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$reflection = new ReflectionClass(Gacela::class); |
|
19
|
|
|
$method = $reflection->getMethod('resetCache'); |
|
20
|
|
|
$method->invoke(null); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function tearDown(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$reflection = new ReflectionClass(Gacela::class); |
|
26
|
|
|
$method = $reflection->getMethod('resetCache'); |
|
27
|
|
|
$method->invoke(null); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function test_alias_resolves_to_original_service(): void |
|
31
|
|
|
{ |
|
32
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
33
|
|
|
$config->addBinding('original-service', static fn (): stdClass => new stdClass()); |
|
34
|
|
|
$config->addAlias('alias-service', 'original-service'); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
38
|
|
|
|
|
39
|
|
|
$alias = $container->get('alias-service'); |
|
40
|
|
|
|
|
41
|
|
|
self::assertInstanceOf(stdClass::class, $alias, 'Alias should resolve using the binding'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function test_multiple_aliases_for_same_factory(): void |
|
45
|
|
|
{ |
|
46
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
47
|
|
|
$config->addFactory('factory', static fn (): stdClass => new stdClass()); |
|
48
|
|
|
$config->addAlias('alias1', 'factory'); |
|
49
|
|
|
$config->addAlias('alias2', 'factory'); |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
53
|
|
|
|
|
54
|
|
|
$instance1 = $container->get('alias1'); |
|
55
|
|
|
$instance2 = $container->get('alias2'); |
|
56
|
|
|
|
|
57
|
|
|
self::assertInstanceOf(stdClass::class, $instance1); |
|
58
|
|
|
self::assertInstanceOf(stdClass::class, $instance2); |
|
59
|
|
|
self::assertNotSame($instance1, $instance2, 'Aliases to factory should each return new instances'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function test_alias_with_factory_service(): void |
|
63
|
|
|
{ |
|
64
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
65
|
|
|
$config->addFactory('factory-service', static fn (): stdClass => new stdClass()); |
|
66
|
|
|
$config->addAlias('alias-factory', 'factory-service'); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$container = Container::withConfig(\Gacela\Framework\Config\Config::getInstance()); |
|
70
|
|
|
|
|
71
|
|
|
$instance1 = $container->get('factory-service'); |
|
72
|
|
|
$instance2 = $container->get('alias-factory'); |
|
73
|
|
|
|
|
74
|
|
|
self::assertInstanceOf(stdClass::class, $instance1); |
|
75
|
|
|
self::assertInstanceOf(stdClass::class, $instance2); |
|
76
|
|
|
self::assertNotSame($instance1, $instance2, 'Factory service should return new instances even through alias'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|