-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
InjectTest.php
119 lines (103 loc) 路 4.15 KB
/
InjectTest.php
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di;
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\BetterReflectionManager;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\Exception\AnnotationException;
use Hyperf\Di\Exception\NotFoundException;
use Hyperf\Utils\ApplicationContext;
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 Mockery;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class InjectTest extends TestCase
{
protected function tearDown()
{
AspectCollector::clear();
AnnotationCollector::clear();
Mockery::close();
BetterReflectionManager::clear();
}
public function testInject()
{
$this->getContainer();
$ast = new Ast();
$code = $ast->proxy(DemoInject::class);
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
mkdir($dir, 0777, true);
}
file_put_contents($file = $dir . 'DemoInject.proxy.php', $code);
require $file;
$demoInject = new DemoInject();
$this->assertSame(Demo::class, get_class($demoInject->getDemo()));
$this->assertSame(null, $demoInject->getDemo1());
}
public function testInjectException()
{
$this->getContainer();
$ast = new Ast();
$code = $ast->proxy(DemoInjectException::class);
if (! is_dir($dir = BASE_PATH . '/runtime/container/proxy/')) {
mkdir($dir, 0777, true);
}
file_put_contents($file = $dir . 'DemoInjectException.proxy.php', $code);
require $file;
try {
new DemoInjectException();
} catch (\Exception $e) {
$this->assertSame(true, $e instanceof NotFoundException);
}
}
public function testInjectNotInitReflector()
{
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because The class reflector object does not init yet');
$inject = new Inject();
$inject->collectProperty(EmptyVarValue::class, 'demo');
}
public function testInjectEmptyVar()
{
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because Argument 1 passed to Roave\BetterReflection\TypesFinder\FindPropertyType::Roave\BetterReflection\TypesFinder\{closure}() must be an instance of phpDocumentor\Reflection\DocBlock\Tags\Var_, instance of phpDocumentor\Reflection\DocBlock\Tags\InvalidTag given');
BetterReflectionManager::initClassReflector([__DIR__ . '/Stub']);
$inject = new Inject();
$inject->collectProperty(EmptyVarValue::class, 'demo');
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
BetterReflectionManager::initClassReflector([__DIR__ . '/Stub']);
$scaner = new Scanner($loader = Mockery::mock(ClassLoader::class), new ScanConfig(false, '/'));
$reader = new AnnotationReader();
$scaner->collect($reader, BetterReflectionManager::reflectClass(DemoInject::class));
$scaner->collect($reader, BetterReflectionManager::reflectClass(DemoInjectException::class));
$container->shouldReceive('get')->with(Demo::class)->andReturn(new Demo());
$container->shouldReceive('has')->with(Demo::class)->andReturn(true);
$container->shouldReceive('has')->with('HyperfTest\Di\ExceptionStub\Demo1')->andReturn(false);
return $container;
}
}