|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Framework\Bootstrap\Setup; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
|
8
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use stdClass; |
|
11
|
|
|
|
|
12
|
|
|
final class SetupMergerTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function test_merge_factories_from_two_setups(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
17
|
|
|
$config->addFactory('service-a', static fn (): stdClass => new stdClass()); |
|
18
|
|
|
}); |
|
19
|
|
|
|
|
20
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
21
|
|
|
$config->addFactory('service-b', static fn (): stdClass => new stdClass()); |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
$merged = $setup1->merge($setup2); |
|
25
|
|
|
|
|
26
|
|
|
$factories = $merged->getFactories(); |
|
27
|
|
|
self::assertArrayHasKey('service-a', $factories); |
|
28
|
|
|
self::assertArrayHasKey('service-b', $factories); |
|
29
|
|
|
self::assertCount(2, $factories); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function test_merge_factories_later_values_override_earlier(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$factory1 = static fn (): stdClass => new stdClass(); |
|
35
|
|
|
$factory2 = static fn (): stdClass => new stdClass(); |
|
36
|
|
|
|
|
37
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config) use ($factory1): void { |
|
38
|
|
|
$config->addFactory('service', $factory1); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config) use ($factory2): void { |
|
42
|
|
|
$config->addFactory('service', $factory2); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
$merged = $setup1->merge($setup2); |
|
46
|
|
|
|
|
47
|
|
|
$factories = $merged->getFactories(); |
|
48
|
|
|
self::assertSame($factory2, $factories['service'], 'Later factory should override earlier one'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function test_merge_protected_services_from_two_setups(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
54
|
|
|
$config->addProtected('service-a', static fn (): string => 'A'); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
58
|
|
|
$config->addProtected('service-b', static fn (): string => 'B'); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
$merged = $setup1->merge($setup2); |
|
62
|
|
|
|
|
63
|
|
|
$protectedServices = $merged->getProtectedServices(); |
|
64
|
|
|
self::assertArrayHasKey('service-a', $protectedServices); |
|
65
|
|
|
self::assertArrayHasKey('service-b', $protectedServices); |
|
66
|
|
|
self::assertCount(2, $protectedServices); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function test_merge_protected_services_later_values_override_earlier(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$protected1 = static fn (): string => 'first'; |
|
72
|
|
|
$protected2 = static fn (): string => 'second'; |
|
73
|
|
|
|
|
74
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config) use ($protected1): void { |
|
75
|
|
|
$config->addProtected('service', $protected1); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config) use ($protected2): void { |
|
79
|
|
|
$config->addProtected('service', $protected2); |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$merged = $setup1->merge($setup2); |
|
83
|
|
|
|
|
84
|
|
|
$protectedServices = $merged->getProtectedServices(); |
|
85
|
|
|
self::assertSame($protected2, $protectedServices['service'], 'Later protected service should override earlier one'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function test_merge_aliases_from_two_setups(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
91
|
|
|
$config->addAlias('alias-a', 'service-a'); |
|
92
|
|
|
}); |
|
93
|
|
|
|
|
94
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
95
|
|
|
$config->addAlias('alias-b', 'service-b'); |
|
96
|
|
|
}); |
|
97
|
|
|
|
|
98
|
|
|
$merged = $setup1->merge($setup2); |
|
99
|
|
|
|
|
100
|
|
|
$aliases = $merged->getAliases(); |
|
101
|
|
|
self::assertArrayHasKey('alias-a', $aliases); |
|
102
|
|
|
self::assertArrayHasKey('alias-b', $aliases); |
|
103
|
|
|
self::assertSame('service-a', $aliases['alias-a']); |
|
104
|
|
|
self::assertSame('service-b', $aliases['alias-b']); |
|
105
|
|
|
self::assertCount(2, $aliases); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function test_merge_aliases_later_values_override_earlier(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
111
|
|
|
$config->addAlias('my-alias', 'original-service'); |
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
115
|
|
|
$config->addAlias('my-alias', 'new-service'); |
|
116
|
|
|
}); |
|
117
|
|
|
|
|
118
|
|
|
$merged = $setup1->merge($setup2); |
|
119
|
|
|
|
|
120
|
|
|
$aliases = $merged->getAliases(); |
|
121
|
|
|
self::assertSame('new-service', $aliases['my-alias'], 'Later alias should override earlier one'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function test_merge_all_container_features_together(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
127
|
|
|
$config->addFactory('factory-1', static fn (): stdClass => new stdClass()); |
|
128
|
|
|
$config->addProtected('protected-1', static fn (): string => 'P1'); |
|
129
|
|
|
$config->addAlias('alias-1', 'service-1'); |
|
130
|
|
|
}); |
|
131
|
|
|
|
|
132
|
|
|
$setup2 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
133
|
|
|
$config->addFactory('factory-2', static fn (): stdClass => new stdClass()); |
|
134
|
|
|
$config->addProtected('protected-2', static fn (): string => 'P2'); |
|
135
|
|
|
$config->addAlias('alias-2', 'service-2'); |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
$merged = $setup1->merge($setup2); |
|
139
|
|
|
|
|
140
|
|
|
self::assertCount(2, $merged->getFactories()); |
|
141
|
|
|
self::assertCount(2, $merged->getProtectedServices()); |
|
142
|
|
|
self::assertCount(2, $merged->getAliases()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function test_merge_only_if_property_changed(): void |
|
146
|
|
|
{ |
|
147
|
|
|
// Setup1 with a factory |
|
148
|
|
|
$setup1 = SetupGacela::fromCallable(static function (GacelaConfig $config): void { |
|
149
|
|
|
$config->addFactory('service-a', static fn (): stdClass => new stdClass()); |
|
150
|
|
|
}); |
|
151
|
|
|
|
|
152
|
|
|
// Setup2 with no changes (empty config) |
|
153
|
|
|
$setup2 = new SetupGacela(); |
|
154
|
|
|
|
|
155
|
|
|
$merged = $setup1->merge($setup2); |
|
156
|
|
|
|
|
157
|
|
|
// Should still have setup1's factory since setup2 didn't change the property |
|
158
|
|
|
$factories = $merged->getFactories(); |
|
159
|
|
|
self::assertArrayHasKey('service-a', $factories); |
|
160
|
|
|
self::assertCount(1, $factories); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|