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\AbstractDependencyProvider; |
9
|
|
|
use Gacela\Framework\AbstractFacade; |
10
|
|
|
use Gacela\Framework\AbstractFactory; |
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
|
|
|
final class AnonymousClassesTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected function setUp(): void |
19
|
|
|
{ |
20
|
|
|
Gacela::bootstrap(__DIR__); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function test_class_resolver_using_anonymous_classes(): void |
24
|
|
|
{ |
25
|
|
|
$this->registerConfig(); |
26
|
|
|
$this->registerFactory(); |
27
|
|
|
$this->registerDependencyProvider(); |
28
|
|
|
|
29
|
|
|
$facade = new class() extends AbstractFacade { |
30
|
|
|
public function getSomething(): array |
31
|
|
|
{ |
32
|
|
|
return $this->getFactory() |
33
|
|
|
->createDomainClass() |
|
|
|
|
34
|
|
|
->getConfigValues(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function greet(string $name): string |
38
|
|
|
{ |
39
|
|
|
return $this->getFactory() |
40
|
|
|
->createDomainClass() |
41
|
|
|
->greet($name); |
42
|
|
|
} |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
self::assertSame([1, 2, 3, 4, 5], $facade->getSomething()); |
46
|
|
|
self::assertSame('Hello, Chema!', $facade->greet('Chema')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Using $this object as context. |
51
|
|
|
*/ |
52
|
|
|
private function registerConfig(): void |
53
|
|
|
{ |
54
|
|
|
AnonymousGlobal::addGlobal( |
55
|
|
|
$this, |
56
|
|
|
new class() extends AbstractConfig { |
57
|
|
|
/** |
58
|
|
|
* @return int[] |
59
|
|
|
*/ |
60
|
|
|
public function getValues(): array |
61
|
|
|
{ |
62
|
|
|
return [1, 2, 3, 4, 5]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Using a string (class name) as context. |
70
|
|
|
*/ |
71
|
|
|
private function registerFactory(): void |
72
|
|
|
{ |
73
|
|
|
AnonymousGlobal::addGlobal( |
74
|
|
|
'AnonymousClassesTest', |
75
|
|
|
new class() extends AbstractFactory { |
76
|
|
|
public function createDomainClass(): object |
77
|
|
|
{ |
78
|
|
|
/** @var object $myService */ |
79
|
|
|
$myService = $this->getProvidedDependency('my-greeter'); |
80
|
|
|
|
81
|
|
|
/** @var int[] $configValues */ |
82
|
|
|
$configValues = $this->getConfig()->getValues(); |
83
|
|
|
|
84
|
|
|
return new class($myService, ...$configValues) { |
85
|
|
|
private object $myService; |
86
|
|
|
/** @var int[] */ |
87
|
|
|
private array $configValues; |
88
|
|
|
|
89
|
|
|
public function __construct( |
90
|
|
|
object $myService, |
91
|
|
|
int ...$configValues |
92
|
|
|
) { |
93
|
|
|
$this->myService = $myService; |
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 registerDependencyProvider(): void |
113
|
|
|
{ |
114
|
|
|
AnonymousGlobal::addGlobal( |
115
|
|
|
$this, |
116
|
|
|
new class() extends AbstractDependencyProvider { |
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 "Hello, {$name}!"; |
123
|
|
|
} |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|