Repository navigation
Expand file tree
/
Copy pathInjectTest.php
More file actions
320 lines (274 loc) · 9.72 KB
/
Copy pathInjectTest.php
File metadata and controls
320 lines (274 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di;
use Exception;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\AnnotationReader;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\Aop\Ast;
use Hyperf\Di\Exception\AnnotationException;
use Hyperf\Di\ReflectionManager;
use Hyperf\Di\ScanHandler\NullScanHandler;
use HyperfTest\Di\ExceptionStub\DemoInjectException;
use HyperfTest\Di\Stub\AnnotationCollector;
use HyperfTest\Di\Stub\AspectCollector;
use HyperfTest\Di\Stub\Demo;
use HyperfTest\Di\Stub\DemoInject;
use HyperfTest\Di\Stub\EmptyVarValue;
use HyperfTest\Di\Stub\Inject\Bar;
use HyperfTest\Di\Stub\Inject\Foo;
use HyperfTest\Di\Stub\Inject\Foo2Trait;
use HyperfTest\Di\Stub\Inject\Foo3Trait;
use HyperfTest\Di\Stub\Inject\FooTrait;
use HyperfTest\Di\Stub\Inject\Origin2Class;
use HyperfTest\Di\Stub\Inject\Origin3Class;
use HyperfTest\Di\Stub\Inject\Origin4Class;
use HyperfTest\Di\Stub\Inject\Origin5Class;
use HyperfTest\Di\Stub\Inject\Origin6Class;
use HyperfTest\Di\Stub\Inject\Origin7Class;
use HyperfTest\Di\Stub\Inject\OriginClass;
use HyperfTest\Di\Stub\Inject\Parent2Class;
use HyperfTest\Di\Stub\Inject\Parent3Class;
use HyperfTest\Di\Stub\Inject\Parent4Class;
use HyperfTest\Di\Stub\Inject\ParentClass;
use HyperfTest\Di\Stub\Inject\Tar;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class InjectTest extends TestCase
{
protected function tearDown(): void
{
AspectCollector::clear();
AnnotationCollector::clear();
Mockery::close();
ReflectionManager::clear();
}
#[Group('NonCoroutine')]
public function testInject()
{
$this->getContainer();
require_once BASE_PATH . '/runtime/container/proxy/' . md5(DemoInject::class) . '.proxy.php';
$demoInject = new DemoInject();
$this->assertSame(Demo::class, get_class($demoInject->getDemo()));
$this->assertSame(null, $demoInject->getDemo1());
}
#[Group('NonCoroutine')]
public function testInjectWithTraitAndParent()
{
$this->getContainer();
$classes = [
ParentClass::class,
FooTrait::class,
OriginClass::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new OriginClass();
$this->assertInstanceOf(Tar::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInjectTraitAndParent()
{
$this->markTestSkipped('@var does not works as expect.');
$this->getContainer();
$classes = [
ParentClass::class,
FooTrait::class,
Origin2Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin2Class();
$this->assertInstanceOf(Bar::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInjectParent()
{
$this->getContainer();
$classes = [
ParentClass::class,
FooTrait::class,
Origin3Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin3Class();
$this->assertInstanceOf(Foo::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInject2Trait()
{
$this->markTestSkipped('@var does not works as expect.');
$this->getContainer();
$classes = [
ParentClass::class,
FooTrait::class,
Origin4Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin4Class();
$this->assertInstanceOf(Tar::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInjectTraitNesting()
{
$this->getContainer();
$classes = [
FooTrait::class,
Foo3Trait::class,
Origin7Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin7Class();
$this->assertInstanceOf(Bar::class, $origin->getFoo());
$this->assertInstanceOf(Bar::class, $origin->getBar());
$this->assertSame('foo3', $origin->getValue());
}
#[Group('NonCoroutine')]
public function testInjectParentParent()
{
$this->getContainer();
$classes = [
Parent2Class::class,
Origin5Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin3Class();
$this->assertInstanceOf(Foo::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInjectParentNoRunParent()
{
$this->getContainer();
$classes = [
Parent3Class::class,
ParentClass::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Parent3Class();
$this->assertInstanceOf(Foo::class, $origin->getFoo());
}
#[Group('NonCoroutine')]
public function testInjectParentPrivateProperty()
{
$this->getContainer();
$classes = [
Parent4Class::class,
Origin6Class::class,
];
foreach ($classes as $class) {
require_once BASE_PATH . '/runtime/container/proxy/' . md5($class) . '.proxy.php';
}
$origin = new Origin6Class();
$this->assertInstanceOf(Foo::class, $origin->getFoo());
$this->assertInstanceOf(Bar::class, $origin->getBar());
}
public function testInjectException()
{
try {
$scanner = new Scanner(new ScanConfig(false, '/'), new NullScanHandler());
$reader = new AnnotationReader();
$scanner->collect($reader, ReflectionManager::reflectClass(DemoInjectException::class));
} catch (Exception $e) {
$this->assertSame('The @var annotation on HyperfTest\Di\ExceptionStub\DemoInjectException::demo contains a non existent class "Demo1". Did you maybe forget to add a "use" statement for this annotation?', $e->getMessage());
$this->assertSame(true, $e instanceof AnnotationException);
}
}
public function testInjectEmptyVar()
{
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo');
$inject = new Inject();
$inject->collectProperty(EmptyVarValue::class, 'demo');
}
protected function getContainer(array $classes = [])
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$path = BASE_PATH . '/runtime/scan.cache';
$pid = pcntl_fork();
if ($pid == -1) {
throw new Exception('The process fork failed');
}
if ($pid === 0) {
$scanner = new Scanner(new ScanConfig(false, '/'), new NullScanHandler());
$reader = new AnnotationReader();
if (empty($classes)) {
$classes = [
DemoInject::class,
OriginClass::class,
ParentClass::class,
FooTrait::class,
Origin2Class::class,
Origin3Class::class,
Foo2Trait::class,
Foo3Trait::class,
Origin4Class::class,
Origin5Class::class,
Parent2Class::class,
Parent3Class::class,
Parent4Class::class,
Origin6Class::class,
Origin7Class::class,
];
}
foreach ($classes as $class) {
$scanner->collect($reader, ReflectionManager::reflectClass($class));
}
file_put_contents($path, AnnotationCollector::serialize());
$ast = new Ast();
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
mkdir($dir, 0777, true);
}
foreach ($classes as $class) {
$code = $ast->proxy($class);
$id = md5($class);
file_put_contents($dir . $id . '.proxy.php', $code);
}
exit;
}
pcntl_wait($status);
AnnotationCollector::deserialize(file_get_contents($path));
$classes = [
Bar::class,
Foo::class,
Tar::class,
Demo::class,
];
foreach ($classes as $class) {
$container->shouldReceive('has')->with($class)->andReturn(true);
$container->shouldReceive('get')->with($class)->andReturn(new $class());
}
$container->shouldReceive('has')->with('HyperfTest\Di\ExceptionStub\Demo1')->andReturn(false);
return $container;
}
}