|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace think\tests; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery as m; |
|
6
|
|
|
use org\bovigo\vfs\vfsStream; |
|
7
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use stdClass; |
|
10
|
|
|
use think\App; |
|
11
|
|
|
use think\Env; |
|
12
|
|
|
use think\Event; |
|
13
|
|
|
use think\event\AppInit; |
|
14
|
|
|
use think\exception\ClassNotFoundException; |
|
15
|
|
|
use think\Service; |
|
16
|
|
|
|
|
17
|
|
|
class SomeService extends Service |
|
18
|
|
|
{ |
|
19
|
|
|
public $bind = [ |
|
20
|
|
|
'some' => 'class', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
public function register() |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function boot() |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @property array initializers |
|
36
|
|
|
*/ |
|
37
|
|
|
class AppTest extends TestCase |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var App */ |
|
40
|
|
|
protected $app; |
|
41
|
|
|
|
|
42
|
|
|
protected function setUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->app = new App(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function tearDown(): void |
|
48
|
|
|
{ |
|
49
|
|
|
m::close(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testService() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->app->register(stdClass::class); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertInstanceOf(stdClass::class, $this->app->getService(stdClass::class)); |
|
57
|
|
|
|
|
58
|
|
|
$service = m::mock(SomeService::class); |
|
59
|
|
|
|
|
60
|
|
|
$service->shouldReceive('register')->once(); |
|
61
|
|
|
|
|
62
|
|
|
$this->app->register($service); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($service, $this->app->getService(SomeService::class)); |
|
65
|
|
|
|
|
66
|
|
|
$service2 = m::mock(SomeService::class); |
|
67
|
|
|
|
|
68
|
|
|
$service2->shouldReceive('register')->once(); |
|
69
|
|
|
|
|
70
|
|
|
$this->app->register($service2); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertEquals($service, $this->app->getService(SomeService::class)); |
|
73
|
|
|
|
|
74
|
|
|
$this->app->register($service2, true); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals($service2, $this->app->getService(SomeService::class)); |
|
77
|
|
|
|
|
78
|
|
|
$service->shouldReceive('boot')->once(); |
|
79
|
|
|
$service2->shouldReceive('boot')->once(); |
|
80
|
|
|
|
|
81
|
|
|
$this->app->boot(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testDebug() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->app->debug(false); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertFalse($this->app->isDebug()); |
|
89
|
|
|
|
|
90
|
|
|
$this->app->debug(true); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertTrue($this->app->isDebug()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testNamespace() |
|
96
|
|
|
{ |
|
97
|
|
|
$namespace = 'test'; |
|
98
|
|
|
|
|
99
|
|
|
$this->app->setNamespace($namespace); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals($namespace, $this->app->getNamespace()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testVersion() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->assertEquals(App::VERSION, $this->app->version()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testPath() |
|
110
|
|
|
{ |
|
111
|
|
|
$rootPath = __DIR__ . DIRECTORY_SEPARATOR; |
|
112
|
|
|
|
|
113
|
|
|
$app = new App($rootPath); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals($rootPath, $app->getRootPath()); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath()); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath()); |
|
120
|
|
|
|
|
121
|
|
|
$appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR; |
|
122
|
|
|
$app->setAppPath($appPath); |
|
123
|
|
|
$this->assertEquals($appPath, $app->getAppPath()); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath()); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath()); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath()); |
|
130
|
|
|
|
|
131
|
|
|
$runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR; |
|
132
|
|
|
$app->setRuntimePath($runtimePath); |
|
133
|
|
|
$this->assertEquals($runtimePath, $app->getRuntimePath()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param vfsStreamDirectory $root |
|
138
|
|
|
* @param bool $debug |
|
139
|
|
|
* @return App |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true) |
|
142
|
|
|
{ |
|
143
|
|
|
$rootPath = $root->url() . DIRECTORY_SEPARATOR; |
|
144
|
|
|
|
|
145
|
|
|
$app = new App($rootPath); |
|
146
|
|
|
|
|
147
|
|
|
$initializer = m::mock(); |
|
148
|
|
|
$initializer->shouldReceive('init')->once()->with($app); |
|
149
|
|
|
|
|
150
|
|
|
$app->instance($initializer->mockery_getName(), $initializer); |
|
151
|
|
|
|
|
152
|
|
|
(function () use ($initializer) { |
|
153
|
|
|
$this->initializers = [$initializer->mockery_getName()]; |
|
154
|
|
|
})->call($app); |
|
155
|
|
|
|
|
156
|
|
|
$env = m::mock(Env::class); |
|
157
|
|
|
$env->shouldReceive('load')->once()->with($rootPath . '.env'); |
|
158
|
|
|
$env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php'); |
|
159
|
|
|
$env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug); |
|
160
|
|
|
|
|
161
|
|
|
$event = m::mock(Event::class); |
|
162
|
|
|
$event->shouldReceive('trigger')->once()->with(AppInit::class); |
|
163
|
|
|
$event->shouldReceive('bind')->once()->with([]); |
|
164
|
|
|
$event->shouldReceive('listenEvents')->once()->with([]); |
|
165
|
|
|
$event->shouldReceive('subscribe')->once()->with([]); |
|
166
|
|
|
|
|
167
|
|
|
$app->instance('env', $env); |
|
168
|
|
|
$app->instance('event', $event); |
|
169
|
|
|
|
|
170
|
|
|
return $app; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testInitialize() |
|
174
|
|
|
{ |
|
175
|
|
|
$root = vfsStream::setup('rootDir', null, [ |
|
176
|
|
|
'.env' => '', |
|
177
|
|
|
'app' => [ |
|
178
|
|
|
'common.php' => '', |
|
179
|
|
|
'event.php' => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];', |
|
180
|
|
|
'provider.php' => '<?php return [];', |
|
181
|
|
|
], |
|
182
|
|
|
'config' => [ |
|
183
|
|
|
'app.php' => '<?php return [];', |
|
184
|
|
|
], |
|
185
|
|
|
]); |
|
186
|
|
|
|
|
187
|
|
|
$app = $this->prepareAppForInitialize($root, true); |
|
188
|
|
|
|
|
189
|
|
|
$app->debug(false); |
|
190
|
|
|
|
|
191
|
|
|
$app->initialize(); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertIsInt($app->getBeginMem()); |
|
194
|
|
|
$this->assertIsFloat($app->getBeginTime()); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertTrue($app->initialized()); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testFactory() |
|
200
|
|
|
{ |
|
201
|
|
|
$this->assertInstanceOf(stdClass::class, App::factory(stdClass::class)); |
|
202
|
|
|
|
|
203
|
|
|
$this->expectException(ClassNotFoundException::class); |
|
204
|
|
|
|
|
205
|
|
|
App::factory('SomeClass'); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testParseClass() |
|
209
|
|
|
{ |
|
210
|
|
|
$this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class')); |
|
211
|
|
|
$this->app->setNamespace('app2'); |
|
212
|
|
|
$this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class')); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|