1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Framework\Bootstrap; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
9
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
10
|
|
|
use Gacela\Framework\Event\Dispatcher\ConfigurableEventDispatcher; |
11
|
|
|
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher; |
12
|
|
|
use Gacela\Framework\Event\GacelaEventInterface; |
13
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithConstructor; |
14
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithoutConstructor; |
15
|
|
|
use GacelaTest\Unit\Framework\Config\GacelaFileConfig\Factory\FakeEvent; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use stdClass; |
18
|
|
|
|
19
|
|
|
final class SetupGacelaTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function test_null_event_dispatcher(): void |
22
|
|
|
{ |
23
|
|
|
$config = new GacelaConfig(); |
24
|
|
|
$setup = SetupGacela::fromGacelaConfig($config); |
25
|
|
|
|
26
|
|
|
self::assertInstanceOf(NullEventDispatcher::class, $setup->getEventDispatcher()); |
27
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_combine_event_dispatcher(): void |
31
|
|
|
{ |
32
|
|
|
$listenerDispatched1 = false; |
33
|
|
|
$listenerDispatched2 = false; |
34
|
|
|
|
35
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
36
|
|
|
(new GacelaConfig())->registerSpecificListener( |
37
|
|
|
FakeEvent::class, |
38
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched1): void { |
39
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
40
|
|
|
$listenerDispatched1 = true; |
41
|
|
|
}, |
42
|
|
|
), |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
self::assertInstanceOf(ConfigurableEventDispatcher::class, $setup->getEventDispatcher()); |
46
|
|
|
|
47
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
48
|
|
|
(new GacelaConfig())->registerSpecificListener( |
49
|
|
|
FakeEvent::class, |
50
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched2): void { |
51
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
52
|
|
|
$listenerDispatched2 = true; |
53
|
|
|
}, |
54
|
|
|
), |
55
|
|
|
); |
56
|
|
|
$setup->combine($setup2); |
57
|
|
|
|
58
|
|
|
self::assertFalse($listenerDispatched1); |
59
|
|
|
self::assertFalse($listenerDispatched2); |
60
|
|
|
|
61
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
62
|
|
|
|
63
|
|
|
self::assertTrue($listenerDispatched1); |
64
|
|
|
self::assertTrue($listenerDispatched2); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_combine_config_key_values(): void |
68
|
|
|
{ |
69
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
70
|
|
|
(new GacelaConfig())->addAppConfigKeyValue('key1', 1), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
74
|
|
|
(new GacelaConfig())->addAppConfigKeyValues(['key2' => 'value2']), |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$setup->combine($setup2); |
78
|
|
|
|
79
|
|
|
self::assertSame([ |
80
|
|
|
'key1' => 1, |
81
|
|
|
'key2' => 'value2', |
82
|
|
|
], $setup->getConfigKeyValues()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function test_combine_project_namespaces(): void |
86
|
|
|
{ |
87
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
88
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App1']), |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
92
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App2']), |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$setup->combine($setup2); |
96
|
|
|
|
97
|
|
|
self::assertSame(['App1', 'App2'], $setup->getProjectNamespaces()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function test_override_file_cache_settings(): void |
101
|
|
|
{ |
102
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
103
|
|
|
(new GacelaConfig()) |
104
|
|
|
->setFileCache(false, 'original/dir'), |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
108
|
|
|
(new GacelaConfig()) |
109
|
|
|
->setFileCache(true, 'override/dir'), |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
self::assertFalse($setup->isFileCacheEnabled()); |
113
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
114
|
|
|
|
115
|
|
|
$setup->combine($setup2); |
116
|
|
|
|
117
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
118
|
|
|
self::assertSame('override/dir', $setup->getFileCacheDirectory()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function test_not_override_file_cache_settings_when_using_default(): void |
122
|
|
|
{ |
123
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
124
|
|
|
(new GacelaConfig()) |
125
|
|
|
->setFileCache(true, 'original/dir'), |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$setup2 = SetupGacela::fromGacelaConfig(new GacelaConfig()); |
129
|
|
|
|
130
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
131
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
132
|
|
|
|
133
|
|
|
$setup->combine($setup2); |
134
|
|
|
|
135
|
|
|
self::assertTrue($setup->isFileCacheEnabled()); |
136
|
|
|
self::assertSame('original/dir', $setup->getFileCacheDirectory()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function test_override_reset_in_memory_cache(): void |
140
|
|
|
{ |
141
|
|
|
$setup = SetupGacela::fromGacelaConfig(new GacelaConfig()); |
142
|
|
|
|
143
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
144
|
|
|
(new GacelaConfig())->resetInMemoryCache(), |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
self::assertFalse($setup->shouldResetInMemoryCache()); |
148
|
|
|
$setup->combine($setup2); |
149
|
|
|
self::assertTrue($setup->shouldResetInMemoryCache()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function test_combine_external_services(): void |
153
|
|
|
{ |
154
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
155
|
|
|
(new GacelaConfig()) |
156
|
|
|
->addExternalService('service1', static fn () => 1), |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
160
|
|
|
(new GacelaConfig()) |
161
|
|
|
->addExternalService('service2', static fn () => 2) |
162
|
|
|
->addExternalService('service3', new stdClass()), |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
self::assertEquals([ |
166
|
|
|
'service1' => static fn () => 1, |
167
|
|
|
], $setup->externalServices()); |
168
|
|
|
|
169
|
|
|
$setup->combine($setup2); |
170
|
|
|
|
171
|
|
|
self::assertEquals([ |
172
|
|
|
'service1' => static fn () => 1, |
173
|
|
|
'service2' => static fn () => 2, |
174
|
|
|
'service3' => new stdClass(), |
175
|
|
|
], $setup->externalServices()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function test_combine_extend_service(): void |
179
|
|
|
{ |
180
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
181
|
|
|
(new GacelaConfig()) |
182
|
|
|
->extendService('service', static fn (ArrayObject $ao) => $ao->append(1)), |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
186
|
|
|
(new GacelaConfig()) |
187
|
|
|
->extendService('service', static fn (ArrayObject $ao) => $ao->append(2)) |
188
|
|
|
->extendService('service-2', static fn (ArrayObject $ao) => $ao->append(3)), |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$setup->combine($setup2); |
192
|
|
|
|
193
|
|
|
self::assertEquals([ |
194
|
|
|
'service' => [ |
195
|
|
|
static fn (ArrayObject $ao) => $ao->append(1), |
196
|
|
|
static fn (ArrayObject $ao) => $ao->append(2), |
197
|
|
|
], |
198
|
|
|
'service-2' => [ |
199
|
|
|
static fn (ArrayObject $ao) => $ao->append(3), |
200
|
|
|
], |
201
|
|
|
], $setup->getServicesToExtend()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function test_plugins(): void |
205
|
|
|
{ |
206
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
207
|
|
|
(new GacelaConfig()) |
208
|
|
|
->addPlugin(ExamplePluginWithoutConstructor::class), |
209
|
|
|
); |
210
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
211
|
|
|
(new GacelaConfig()) |
212
|
|
|
->addPlugin(ExamplePluginWithConstructor::class), |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
$setup->combine($setup2); |
216
|
|
|
|
217
|
|
|
self::assertEquals([ |
218
|
|
|
ExamplePluginWithoutConstructor::class, |
219
|
|
|
ExamplePluginWithConstructor::class, |
220
|
|
|
], $setup->getPlugins()); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|