|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Framework\Bootstrap; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use Fixtures\CustomGacelaConfig; |
|
9
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
|
10
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
|
11
|
|
|
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; |
|
12
|
|
|
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher; |
|
13
|
|
|
use Gacela\Framework\Event\GacelaEventInterface; |
|
14
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithConstructor; |
|
15
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithoutConstructor; |
|
16
|
|
|
use GacelaTest\Unit\Framework\Config\GacelaFileConfig\Factory\FakeEvent; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use stdClass; |
|
19
|
|
|
|
|
20
|
|
|
final class SetupGacelaTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function test_null_event_dispatcher(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$config = new GacelaConfig(); |
|
25
|
|
|
$setup = SetupGacela::fromGacelaConfig($config); |
|
26
|
|
|
|
|
27
|
|
|
self::assertInstanceOf(NullEventDispatcher::class, $setup->getEventDispatcher()); |
|
28
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function test_combine_event_dispatcher(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$listenerDispatched1 = false; |
|
34
|
|
|
$listenerDispatched2 = false; |
|
35
|
|
|
|
|
36
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
37
|
|
|
(new GacelaConfig())->registerSpecificListener( |
|
38
|
|
|
FakeEvent::class, |
|
39
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched1): void { |
|
40
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
|
41
|
|
|
$listenerDispatched1 = true; |
|
42
|
|
|
}, |
|
43
|
|
|
), |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
self::assertInstanceOf(ConfigurableEventDispatcher::class, $setup->getEventDispatcher()); |
|
47
|
|
|
|
|
48
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
49
|
|
|
(new GacelaConfig())->registerSpecificListener( |
|
50
|
|
|
FakeEvent::class, |
|
51
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched2): void { |
|
52
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
|
53
|
|
|
$listenerDispatched2 = true; |
|
54
|
|
|
}, |
|
55
|
|
|
), |
|
56
|
|
|
); |
|
57
|
|
|
$setup->merge($setup2); |
|
58
|
|
|
|
|
59
|
|
|
self::assertFalse($listenerDispatched1); |
|
60
|
|
|
self::assertFalse($listenerDispatched2); |
|
61
|
|
|
|
|
62
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
|
63
|
|
|
|
|
64
|
|
|
self::assertTrue($listenerDispatched1); |
|
65
|
|
|
self::assertTrue($listenerDispatched2); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function test_combine_config_key_values(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
71
|
|
|
(new GacelaConfig())->addAppConfigKeyValue('key1', 1), |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
75
|
|
|
(new GacelaConfig())->addAppConfigKeyValues(['key2' => 'value2']), |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$setup->merge($setup2); |
|
79
|
|
|
|
|
80
|
|
|
self::assertSame([ |
|
81
|
|
|
'key1' => 1, |
|
82
|
|
|
'key2' => 'value2', |
|
83
|
|
|
], $setup->getConfigKeyValues()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function test_combine_project_namespaces(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
89
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App1']), |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
93
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App2']), |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
$setup->merge($setup2); |
|
97
|
|
|
|
|
98
|
|
|
self::assertSame(['App1', 'App2'], $setup->getProjectNamespaces()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function test_override_file_cache_settings(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
104
|
|
|
(new GacelaConfig()) |
|
105
|
|
|
->setFileCache(false, 'original/dir'), |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
109
|
|
|
(new GacelaConfig()) |
|
110
|
|
|
->setFileCache(true, 'override/dir'), |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
self::assertFalse($setup->isFileCacheEnabled()); |
|
114
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
|
115
|
|
|
|
|
116
|
|
|
$setup->merge($setup2); |
|
117
|
|
|
|
|
118
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
|
119
|
|
|
self::assertSame('override/dir', $setup->getFileCacheDirectory()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function test_not_override_file_cache_settings_when_using_default(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
125
|
|
|
(new GacelaConfig()) |
|
126
|
|
|
->setFileCache(true, 'original/dir'), |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$setup2 = SetupGacela::fromGacelaConfig(new GacelaConfig()); |
|
130
|
|
|
|
|
131
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
|
132
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
|
133
|
|
|
|
|
134
|
|
|
$setup->merge($setup2); |
|
135
|
|
|
|
|
136
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
|
137
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function test_override_reset_in_memory_cache(): void |
|
141
|
|
|
{ |
|
142
|
|
|
$setup = SetupGacela::fromGacelaConfig(new GacelaConfig()); |
|
143
|
|
|
|
|
144
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
145
|
|
|
(new GacelaConfig())->resetInMemoryCache(), |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
self::assertFalse($setup->shouldResetInMemoryCache()); |
|
149
|
|
|
$setup->merge($setup2); |
|
150
|
|
|
self::assertTrue($setup->shouldResetInMemoryCache()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function test_combine_external_services(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
156
|
|
|
(new GacelaConfig()) |
|
157
|
|
|
->addExternalService('service1', static fn (): int => 1), |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
161
|
|
|
(new GacelaConfig()) |
|
162
|
|
|
->addExternalService('service2', static fn (): int => 2) |
|
163
|
|
|
->addExternalService('service3', new stdClass()), |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
self::assertEquals([ |
|
167
|
|
|
'service1' => static fn (): int => 1, |
|
168
|
|
|
], $setup->externalServices()); |
|
169
|
|
|
|
|
170
|
|
|
$setup->merge($setup2); |
|
171
|
|
|
|
|
172
|
|
|
self::assertEquals([ |
|
173
|
|
|
'service1' => static fn (): int => 1, |
|
174
|
|
|
'service2' => static fn (): int => 2, |
|
175
|
|
|
'service3' => new stdClass(), |
|
176
|
|
|
], $setup->externalServices()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function test_combine_extend_service(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
182
|
|
|
(new GacelaConfig()) |
|
183
|
|
|
->extendService('service', static fn (ArrayObject $ao) => $ao->append(1)), |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
187
|
|
|
(new GacelaConfig()) |
|
188
|
|
|
->extendService('service', static fn (ArrayObject $ao) => $ao->append(2)) |
|
189
|
|
|
->extendService('service-2', static fn (ArrayObject $ao) => $ao->append(3)), |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
$setup->merge($setup2); |
|
193
|
|
|
|
|
194
|
|
|
self::assertEquals([ |
|
195
|
|
|
'service' => [ |
|
196
|
|
|
static fn (ArrayObject $ao) => $ao->append(1), |
|
197
|
|
|
static fn (ArrayObject $ao) => $ao->append(2), |
|
198
|
|
|
], |
|
199
|
|
|
'service-2' => [ |
|
200
|
|
|
static fn (ArrayObject $ao) => $ao->append(3), |
|
201
|
|
|
], |
|
202
|
|
|
], $setup->getServicesToExtend()); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function test_plugins(): void |
|
206
|
|
|
{ |
|
207
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
208
|
|
|
(new GacelaConfig()) |
|
209
|
|
|
->addPlugin(ExamplePluginWithoutConstructor::class), |
|
210
|
|
|
); |
|
211
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
212
|
|
|
(new GacelaConfig()) |
|
213
|
|
|
->addPlugin(ExamplePluginWithConstructor::class), |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$setup->merge($setup2); |
|
217
|
|
|
|
|
218
|
|
|
self::assertSame([ |
|
219
|
|
|
ExamplePluginWithoutConstructor::class, |
|
220
|
|
|
ExamplePluginWithConstructor::class, |
|
221
|
|
|
], $setup->getPlugins()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function test_gacela_configs_to_extends(): void |
|
225
|
|
|
{ |
|
226
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
227
|
|
|
(new GacelaConfig()) |
|
228
|
|
|
->extendGacelaConfigs([CustomGacelaConfig::class]), |
|
229
|
|
|
); |
|
230
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
231
|
|
|
(new GacelaConfig()) |
|
232
|
|
|
->extendGacelaConfig(CustomGacelaConfig::class), |
|
233
|
|
|
); |
|
234
|
|
|
|
|
235
|
|
|
$setup->merge($setup2); |
|
236
|
|
|
|
|
237
|
|
|
self::assertSame([ |
|
238
|
|
|
CustomGacelaConfig::class, |
|
239
|
|
|
], $setup->getGacelaConfigsToExtend()); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function test_register_generic_listener_multiple_times(): void |
|
243
|
|
|
{ |
|
244
|
|
|
$listener1 = static function (GacelaEventInterface $event): void {}; |
|
|
|
|
|
|
245
|
|
|
$listener2 = static function (GacelaEventInterface $event): void {}; |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
$config = (new GacelaConfig()) |
|
248
|
|
|
->registerGenericListener($listener1) |
|
249
|
|
|
->registerGenericListener($listener2); |
|
250
|
|
|
|
|
251
|
|
|
$transfer = $config->toTransfer(); |
|
252
|
|
|
|
|
253
|
|
|
self::assertSame([$listener1, $listener2], $transfer->genericListeners); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function test_register_specific_listener_multiple_times(): void |
|
257
|
|
|
{ |
|
258
|
|
|
$listener1 = static function (GacelaEventInterface $event): void {}; |
|
|
|
|
|
|
259
|
|
|
$listener2 = static function (GacelaEventInterface $event): void {}; |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
$config = (new GacelaConfig()) |
|
262
|
|
|
->registerSpecificListener(FakeEvent::class, $listener1) |
|
263
|
|
|
->registerSpecificListener(FakeEvent::class, $listener2); |
|
264
|
|
|
|
|
265
|
|
|
$transfer = $config->toTransfer(); |
|
266
|
|
|
|
|
267
|
|
|
self::assertSame([$listener1, $listener2], $transfer->specificListeners[FakeEvent::class]); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function test_extend_service_multiple_times(): void |
|
271
|
|
|
{ |
|
272
|
|
|
$service1 = static fn (mixed $s): mixed => $s; |
|
273
|
|
|
$service2 = static fn (mixed $s): mixed => $s; |
|
274
|
|
|
|
|
275
|
|
|
$config = (new GacelaConfig()) |
|
276
|
|
|
->extendService('service1', $service1) |
|
277
|
|
|
->extendService('service1', $service2); |
|
278
|
|
|
|
|
279
|
|
|
$transfer = $config->toTransfer(); |
|
280
|
|
|
|
|
281
|
|
|
self::assertSame([$service1, $service2], $transfer->servicesToExtend['service1']); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function test_add_plugins_merges_with_existing(): void |
|
285
|
|
|
{ |
|
286
|
|
|
$config = (new GacelaConfig()) |
|
287
|
|
|
->addPlugin(ExamplePluginWithoutConstructor::class) |
|
288
|
|
|
->addPlugins([ExamplePluginWithConstructor::class]); |
|
289
|
|
|
|
|
290
|
|
|
$transfer = $config->toTransfer(); |
|
291
|
|
|
|
|
292
|
|
|
self::assertSame([ |
|
293
|
|
|
ExamplePluginWithoutConstructor::class, |
|
294
|
|
|
ExamplePluginWithConstructor::class, |
|
295
|
|
|
], $transfer->plugins); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
public function test_extend_gacela_configs_merges_with_existing(): void |
|
299
|
|
|
{ |
|
300
|
|
|
$config = (new GacelaConfig()) |
|
301
|
|
|
->extendGacelaConfig(CustomGacelaConfig::class) |
|
302
|
|
|
->extendGacelaConfigs([CustomGacelaConfig::class]); |
|
303
|
|
|
|
|
304
|
|
|
$transfer = $config->toTransfer(); |
|
305
|
|
|
|
|
306
|
|
|
self::assertCount(2, $transfer->gacelaConfigsToExtend); |
|
|
|
|
|
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.