|
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\Config\Config; |
|
9
|
|
|
use Gacela\Framework\Container\Container; |
|
10
|
|
|
use Gacela\Framework\Gacela; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
use stdClass; |
|
14
|
|
|
|
|
15
|
|
|
final class ContextualBindingsTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
protected function tearDown(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$reflection = new ReflectionClass(Gacela::class); |
|
20
|
|
|
$method = $reflection->getMethod('resetCache'); |
|
21
|
|
|
$method->invoke(null); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function test_contextual_binding_resolves_based_on_requesting_class(): void |
|
25
|
|
|
{ |
|
26
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
27
|
|
|
$config->addBinding('LoggerInterface', 'DefaultLogger'); |
|
28
|
|
|
$config->when(stdClass::class) |
|
29
|
|
|
->needs('LoggerInterface') |
|
30
|
|
|
->give('SpecificLogger'); |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
Container::withConfig(Config::getInstance()); |
|
34
|
|
|
|
|
35
|
|
|
// Verify the contextual binding was registered |
|
36
|
|
|
$contextualBindings = Config::getInstance() |
|
37
|
|
|
->getSetupGacela() |
|
38
|
|
|
->getContextualBindings(); |
|
39
|
|
|
|
|
40
|
|
|
self::assertArrayHasKey(stdClass::class, $contextualBindings); |
|
41
|
|
|
self::assertSame('SpecificLogger', $contextualBindings[stdClass::class]['LoggerInterface']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function test_contextual_binding_with_callable(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$callable = static fn (): stdClass => new stdClass(); |
|
47
|
|
|
|
|
48
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($callable): void { |
|
49
|
|
|
$config->when('UserController') |
|
50
|
|
|
->needs('LoggerInterface') |
|
51
|
|
|
->give($callable); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
Container::withConfig(Config::getInstance()); |
|
55
|
|
|
|
|
56
|
|
|
$contextualBindings = Config::getInstance() |
|
57
|
|
|
->getSetupGacela() |
|
58
|
|
|
->getContextualBindings(); |
|
59
|
|
|
|
|
60
|
|
|
self::assertArrayHasKey('UserController', $contextualBindings); |
|
61
|
|
|
self::assertSame($callable, $contextualBindings['UserController']['LoggerInterface']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function test_multiple_contextual_bindings_for_same_class(): void |
|
65
|
|
|
{ |
|
66
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
67
|
|
|
$config->when(stdClass::class) |
|
68
|
|
|
->needs('LoggerInterface') |
|
69
|
|
|
->give('FileLogger'); |
|
70
|
|
|
|
|
71
|
|
|
$config->when(stdClass::class) |
|
72
|
|
|
->needs('CacheInterface') |
|
73
|
|
|
->give('RedisCache'); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
Container::withConfig(Config::getInstance()); |
|
77
|
|
|
|
|
78
|
|
|
$contextualBindings = Config::getInstance() |
|
79
|
|
|
->getSetupGacela() |
|
80
|
|
|
->getContextualBindings(); |
|
81
|
|
|
|
|
82
|
|
|
self::assertArrayHasKey(stdClass::class, $contextualBindings); |
|
83
|
|
|
self::assertCount(2, $contextualBindings[stdClass::class]); |
|
84
|
|
|
self::assertSame('FileLogger', $contextualBindings[stdClass::class]['LoggerInterface']); |
|
85
|
|
|
self::assertSame('RedisCache', $contextualBindings[stdClass::class]['CacheInterface']); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function test_contextual_binding_with_multiple_classes(): void |
|
89
|
|
|
{ |
|
90
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
91
|
|
|
$config->when([stdClass::class, 'OtherClass']) |
|
92
|
|
|
->needs('LoggerInterface') |
|
93
|
|
|
->give('SharedLogger'); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
Container::withConfig(Config::getInstance()); |
|
97
|
|
|
|
|
98
|
|
|
$contextualBindings = Config::getInstance() |
|
99
|
|
|
->getSetupGacela() |
|
100
|
|
|
->getContextualBindings(); |
|
101
|
|
|
|
|
102
|
|
|
self::assertArrayHasKey(stdClass::class, $contextualBindings); |
|
103
|
|
|
self::assertArrayHasKey('OtherClass', $contextualBindings); |
|
104
|
|
|
self::assertSame('SharedLogger', $contextualBindings[stdClass::class]['LoggerInterface']); |
|
105
|
|
|
self::assertSame('SharedLogger', $contextualBindings['OtherClass']['LoggerInterface']); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function test_contextual_bindings_can_be_combined(): void |
|
109
|
|
|
{ |
|
110
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
111
|
|
|
$config->when('ClassA') |
|
112
|
|
|
->needs('LoggerInterface') |
|
113
|
|
|
->give('FileLogger'); |
|
114
|
|
|
|
|
115
|
|
|
$config->when('ClassB') |
|
116
|
|
|
->needs('CacheInterface') |
|
117
|
|
|
->give('RedisCache'); |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
Container::withConfig(Config::getInstance()); |
|
121
|
|
|
|
|
122
|
|
|
$contextualBindings = Config::getInstance() |
|
123
|
|
|
->getSetupGacela() |
|
124
|
|
|
->getContextualBindings(); |
|
125
|
|
|
|
|
126
|
|
|
// Both bindings should be present |
|
127
|
|
|
self::assertArrayHasKey('ClassA', $contextualBindings); |
|
128
|
|
|
self::assertArrayHasKey('ClassB', $contextualBindings); |
|
129
|
|
|
self::assertSame('FileLogger', $contextualBindings['ClassA']['LoggerInterface']); |
|
130
|
|
|
self::assertSame('RedisCache', $contextualBindings['ClassB']['CacheInterface']); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|