|
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\Unit\Framework\Config\GacelaFileConfig\Factory\FakeEvent; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use stdClass; |
|
16
|
|
|
|
|
17
|
|
|
final class SetupGacelaTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function test_null_event_dispatcher(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$config = new GacelaConfig(); |
|
22
|
|
|
$setup = SetupGacela::fromGacelaConfig($config); |
|
23
|
|
|
|
|
24
|
|
|
self::assertInstanceOf(NullEventDispatcher::class, $setup->getEventDispatcher()); |
|
25
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function test_combine_event_dispatcher(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$listenerDispatched1 = false; |
|
31
|
|
|
$listenerDispatched2 = false; |
|
32
|
|
|
|
|
33
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
34
|
|
|
(new GacelaConfig())->registerSpecificListener( |
|
35
|
|
|
FakeEvent::class, |
|
36
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched1): void { |
|
37
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
|
38
|
|
|
$listenerDispatched1 = true; |
|
39
|
|
|
}, |
|
40
|
|
|
), |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
self::assertInstanceOf(ConfigurableEventDispatcher::class, $setup->getEventDispatcher()); |
|
44
|
|
|
|
|
45
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
46
|
|
|
(new GacelaConfig())->registerSpecificListener( |
|
47
|
|
|
FakeEvent::class, |
|
48
|
|
|
static function (GacelaEventInterface $event) use (&$listenerDispatched2): void { |
|
49
|
|
|
self::assertInstanceOf(FakeEvent::class, $event); |
|
50
|
|
|
$listenerDispatched2 = true; |
|
51
|
|
|
}, |
|
52
|
|
|
), |
|
53
|
|
|
); |
|
54
|
|
|
$setup->combine($setup2); |
|
55
|
|
|
|
|
56
|
|
|
self::assertFalse($listenerDispatched1); |
|
57
|
|
|
self::assertFalse($listenerDispatched2); |
|
58
|
|
|
|
|
59
|
|
|
$setup->getEventDispatcher()->dispatch(new FakeEvent()); |
|
60
|
|
|
|
|
61
|
|
|
self::assertTrue($listenerDispatched1); |
|
62
|
|
|
self::assertTrue($listenerDispatched2); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function test_combine_config_key_values(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
68
|
|
|
(new GacelaConfig())->addAppConfigKeyValue('key1', 1), |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
72
|
|
|
(new GacelaConfig())->addAppConfigKeyValues(['key2' => 'value2']), |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$setup->combine($setup2); |
|
76
|
|
|
|
|
77
|
|
|
self::assertSame([ |
|
78
|
|
|
'key1' => 1, |
|
79
|
|
|
'key2' => 'value2', |
|
80
|
|
|
], $setup->getConfigKeyValues()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function test_combine_project_namespaces(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
86
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App1']), |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
90
|
|
|
(new GacelaConfig())->setProjectNamespaces(['App2']), |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$setup->combine($setup2); |
|
94
|
|
|
|
|
95
|
|
|
self::assertSame(['App1', 'App2'], $setup->getProjectNamespaces()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function test_override_file_cache_settings(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$setup = SetupGacela::fromGacelaConfig( |
|
101
|
|
|
(new GacelaConfig()) |
|
102
|
|
|
->setFileCacheEnabled(false) |
|
103
|
|
|
->setFileCacheDirectory('original/dir'), |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
107
|
|
|
(new GacelaConfig()) |
|
108
|
|
|
->setFileCacheEnabled(true) |
|
109
|
|
|
->setFileCacheDirectory('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
|
|
|
->setFileCacheEnabled(true) |
|
126
|
|
|
->setFileCacheDirectory('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->combine($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->combine($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 () => 1), |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$setup2 = SetupGacela::fromGacelaConfig( |
|
161
|
|
|
(new GacelaConfig()) |
|
162
|
|
|
->addExternalService('service2', static fn () => 2) |
|
163
|
|
|
->addExternalService('service3', new stdClass()), |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
self::assertEquals([ |
|
167
|
|
|
'service1' => static fn () => 1, |
|
168
|
|
|
], $setup->externalServices()); |
|
169
|
|
|
|
|
170
|
|
|
$setup->combine($setup2); |
|
171
|
|
|
|
|
172
|
|
|
self::assertEquals([ |
|
173
|
|
|
'service1' => static fn () => 1, |
|
174
|
|
|
'service2' => static fn () => 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->combine($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
|
|
|
|