-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathAppTest.php
207 lines (148 loc) · 5.54 KB
/
AppTest.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
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
<?php
namespace think\tests;
use Mockery as m;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
use stdClass;
use think\App;
use think\Env;
use think\Event;
use think\event\AppInit;
use think\exception\ClassNotFoundException;
use think\Service;
class SomeService extends Service
{
public $bind = [
'some' => 'class',
];
public function register()
{
}
public function boot()
{
}
}
/**
* @property array initializers
*/
class AppTest extends TestCase
{
/** @var App */
protected $app;
protected function setUp(): void
{
$this->app = new App();
}
protected function tearDown(): void
{
m::close();
}
public function testService()
{
$service = m::mock(SomeService::class);
$service->shouldReceive('register')->once();
$this->app->register($service);
$this->assertEquals($service, $this->app->getService(SomeService::class));
$service2 = m::mock(SomeService::class);
$service2->shouldReceive('register')->once();
$this->app->register($service2);
$this->assertEquals($service, $this->app->getService(SomeService::class));
$this->app->register($service2, true);
$this->assertEquals($service2, $this->app->getService(SomeService::class));
$service->shouldReceive('boot')->once();
$service2->shouldReceive('boot')->once();
$this->app->boot();
}
public function testDebug()
{
$this->app->debug(false);
$this->assertFalse($this->app->isDebug());
$this->app->debug(true);
$this->assertTrue($this->app->isDebug());
}
public function testNamespace()
{
$namespace = 'test';
$this->app->setNamespace($namespace);
$this->assertEquals($namespace, $this->app->getNamespace());
}
public function testPath()
{
$rootPath = __DIR__ . DIRECTORY_SEPARATOR;
$app = new App($rootPath);
$this->assertEquals($rootPath, $app->getRootPath());
$this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath());
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath());
$appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
$app->setAppPath($appPath);
$this->assertEquals($appPath, $app->getAppPath());
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath());
$this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath());
$this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath());
$runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
$app->setRuntimePath($runtimePath);
$this->assertEquals($runtimePath, $app->getRuntimePath());
}
/**
* @param vfsStreamDirectory $root
* @param bool $debug
* @return App
*/
protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true)
{
$rootPath = $root->url() . DIRECTORY_SEPARATOR;
$app = new App($rootPath);
$initializer = m::mock();
$initializer->shouldReceive('init')->once()->with($app);
$app->instance($initializer->mockery_getName(), $initializer);
(function () use ($initializer) {
$this->initializers = [$initializer->mockery_getName()];
})->call($app);
$env = m::mock(Env::class);
$env->shouldReceive('load')->once()->with($rootPath . '.env');
$env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php');
$env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug);
$env->shouldReceive('get')->once()->with('env_name', '')->andReturn('');
$event = m::mock(Event::class);
$event->shouldReceive('trigger')->once()->with(AppInit::class);
$event->shouldReceive('bind')->once()->with([]);
$event->shouldReceive('listenEvents')->once()->with([]);
$event->shouldReceive('subscribe')->once()->with([]);
$app->instance('env', $env);
$app->instance('event', $event);
return $app;
}
public function testInitialize()
{
$root = vfsStream::setup('rootDir', null, [
'.env' => '',
'app' => [
'common.php' => '',
'event.php' => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
'provider.php' => '<?php return [];',
],
'config' => [
'app.php' => '<?php return [];',
],
]);
$app = $this->prepareAppForInitialize($root, true);
$app->debug(false);
$app->initialize();
$this->assertIsInt($app->getBeginMem());
$this->assertIsFloat($app->getBeginTime());
$this->assertTrue($app->initialized());
}
public function testFactory()
{
$this->assertInstanceOf(stdClass::class, App::factory(stdClass::class));
$this->expectException(ClassNotFoundException::class);
App::factory('SomeClass');
}
public function testParseClass()
{
$this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
$this->app->setNamespace('app2');
$this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
}
}