|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Framework\Plugins; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
|
8
|
|
|
use Gacela\Framework\Container\Container; |
|
9
|
|
|
use Gacela\Framework\Gacela; |
|
10
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithConstructor; |
|
11
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithInvokeArgs; |
|
12
|
|
|
use GacelaTest\Feature\Framework\Plugins\Module\Infrastructure\ExamplePluginWithoutConstructor; |
|
13
|
|
|
use GacelaTest\Fixtures\StringValue; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
final class FeatureTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function test_singleton_altered_via_plugin_with_constructor(): void |
|
19
|
|
|
{ |
|
20
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
21
|
|
|
$config->addPlugin(ExamplePluginWithConstructor::class); |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
|
|
/** @var StringValue $singleton */ |
|
25
|
|
|
$singleton = Gacela::get(StringValue::class); |
|
26
|
|
|
|
|
27
|
|
|
self::assertSame('Set from plugin ExamplePluginWithConstructor', $singleton->value()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function test_singleton_altered_via_plugin_with_invoke_args(): void |
|
31
|
|
|
{ |
|
32
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
33
|
|
|
$config->addPlugin(ExamplePluginWithInvokeArgs::class); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
/** @var StringValue $singleton */ |
|
37
|
|
|
$singleton = Gacela::get(StringValue::class); |
|
38
|
|
|
|
|
39
|
|
|
self::assertSame('Set from plugin ExamplePluginWithInvokeArgs', $singleton->value()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function test_singleton_altered_via_plugin_without_constructor(): void |
|
43
|
|
|
{ |
|
44
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
45
|
|
|
$config->addPlugin(ExamplePluginWithoutConstructor::class); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
/** @var StringValue $singleton */ |
|
49
|
|
|
$singleton = Gacela::get(StringValue::class); |
|
50
|
|
|
|
|
51
|
|
|
self::assertSame('Set from plugin ExamplePluginWithoutConstructor', $singleton->value()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function test_multiple_plugins_latest_win(): void |
|
55
|
|
|
{ |
|
56
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
57
|
|
|
$config->addPlugins([ |
|
58
|
|
|
ExamplePluginWithConstructor::class, |
|
59
|
|
|
ExamplePluginWithoutConstructor::class, |
|
60
|
|
|
]); |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
/** @var StringValue $singleton */ |
|
64
|
|
|
$singleton = Gacela::get(StringValue::class); |
|
65
|
|
|
|
|
66
|
|
|
self::assertSame('Set from plugin ExamplePluginWithoutConstructor', $singleton->value()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function test_singleton_altered_via_plugin_as_callable(): void |
|
70
|
|
|
{ |
|
71
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
|
72
|
|
|
$config->addPlugin(static function (Container $container): void { |
|
73
|
|
|
$string = $container->getLocator()->get(StringValue::class); |
|
74
|
|
|
$string?->setValue('Set from callable'); |
|
75
|
|
|
}); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
/** @var StringValue $singleton */ |
|
79
|
|
|
$singleton = Gacela::get(StringValue::class); |
|
80
|
|
|
|
|
81
|
|
|
self::assertSame('Set from callable', $singleton->value()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|