1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Integration\Framework\GlobalServices; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\AbstractConfig; |
8
|
|
|
use Gacela\Framework\AbstractFacade; |
9
|
|
|
use Gacela\Framework\AbstractFactory; |
10
|
|
|
use Gacela\Framework\AbstractProvider; |
11
|
|
|
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal; |
12
|
|
|
use Gacela\Framework\Container\Container; |
13
|
|
|
use Gacela\Framework\Gacela; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
final class AnonymousClassesTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
Gacela::bootstrap(__DIR__); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function test_class_resolver_using_anonymous_classes(): void |
26
|
|
|
{ |
27
|
|
|
$this->registerConfig(); |
28
|
|
|
$this->registerFactory(); |
29
|
|
|
$this->registerAbstractProvider(); |
30
|
|
|
|
31
|
|
|
$facade = new class() extends AbstractFacade { |
32
|
|
|
public function getSomething(): array |
33
|
|
|
{ |
34
|
|
|
return $this->getFactory() |
35
|
|
|
->createDomainClass() |
|
|
|
|
36
|
|
|
->getConfigValues(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function greet(string $name): string |
40
|
|
|
{ |
41
|
|
|
return $this->getFactory() |
42
|
|
|
->createDomainClass() |
43
|
|
|
->greet($name); |
44
|
|
|
} |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
self::assertSame([1, 2, 3, 4, 5], $facade->getSomething()); |
48
|
|
|
self::assertSame('Hello, Chema!', $facade->greet('Chema')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Using $this object as context. |
53
|
|
|
*/ |
54
|
|
|
private function registerConfig(): void |
55
|
|
|
{ |
56
|
|
|
AnonymousGlobal::addGlobal( |
57
|
|
|
$this, |
58
|
|
|
new class() extends AbstractConfig { |
59
|
|
|
/** |
60
|
|
|
* @return int[] |
61
|
|
|
*/ |
62
|
|
|
public function getValues(): array |
63
|
|
|
{ |
64
|
|
|
return [1, 2, 3, 4, 5]; |
65
|
|
|
} |
66
|
|
|
}, |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Using a string (class name) as context. |
72
|
|
|
*/ |
73
|
|
|
private function registerFactory(): void |
74
|
|
|
{ |
75
|
|
|
AnonymousGlobal::addGlobal( |
76
|
|
|
'AnonymousClassesTest', |
77
|
|
|
new class() extends AbstractFactory { |
78
|
|
|
public function createDomainClass(): object |
79
|
|
|
{ |
80
|
|
|
/** @var object $myService */ |
81
|
|
|
$myService = $this->getProvidedDependency('my-greeter'); |
82
|
|
|
|
83
|
|
|
/** @var int[] $configValues */ |
84
|
|
|
$configValues = $this->getConfig()->getValues(); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return new class($myService, ...$configValues) { |
87
|
|
|
/** @var int[] */ |
88
|
|
|
private readonly array $configValues; |
89
|
|
|
|
90
|
|
|
public function __construct( |
91
|
|
|
private readonly object $myService, |
92
|
|
|
int ...$configValues, |
93
|
|
|
) { |
94
|
|
|
$this->configValues = $configValues; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getConfigValues(): array |
98
|
|
|
{ |
99
|
|
|
return $this->configValues; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function greet(string $name): string |
103
|
|
|
{ |
104
|
|
|
return $this->myService->greet($name); |
105
|
|
|
} |
106
|
|
|
}; |
107
|
|
|
} |
108
|
|
|
}, |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function registerAbstractProvider(): void |
113
|
|
|
{ |
114
|
|
|
AnonymousGlobal::addGlobal( |
115
|
|
|
$this, |
116
|
|
|
new class() extends AbstractProvider { |
117
|
|
|
public function provideModuleDependencies(Container $container): void |
118
|
|
|
{ |
119
|
|
|
$container->set('my-greeter', new class() { |
120
|
|
|
public function greet(string $name): string |
121
|
|
|
{ |
122
|
|
|
return sprintf('Hello, %s!', $name); |
123
|
|
|
} |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
}, |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|