|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace think\tests; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery as m; |
|
6
|
|
|
use Mockery\MockInterface; |
|
7
|
|
|
use org\bovigo\vfs\vfsStream; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use think\App; |
|
10
|
|
|
use think\Config; |
|
11
|
|
|
use think\Console; |
|
12
|
|
|
use think\Event; |
|
13
|
|
|
use think\Exception; |
|
14
|
|
|
use think\exception\Handle; |
|
15
|
|
|
use think\Http; |
|
16
|
|
|
use think\Log; |
|
17
|
|
|
use think\Request; |
|
18
|
|
|
use think\Response; |
|
19
|
|
|
use think\Route; |
|
20
|
|
|
use think\Session; |
|
21
|
|
|
|
|
22
|
|
|
class HttpTest extends TestCase |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
/** @var App|MockInterface */ |
|
|
|
|
|
|
25
|
|
|
protected $app; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Http|MockInterface */ |
|
|
|
|
|
|
28
|
|
|
protected $http; |
|
29
|
|
|
|
|
30
|
|
|
protected function tearDown(): void |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
m::close(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function setUp() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->app = m::mock(App::class)->makePartial(); |
|
38
|
|
|
|
|
39
|
|
|
$this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function prepareApp($request, $response) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$this->app->shouldReceive('instance')->once()->with('request', $request); |
|
|
|
|
|
|
45
|
|
|
$this->app->shouldReceive('initialized')->once()->andReturnFalse(); |
|
|
|
|
|
|
46
|
|
|
$this->app->shouldReceive('initialize')->once(); |
|
47
|
|
|
$this->app->shouldReceive('get')->with('request')->andReturn($request); |
|
48
|
|
|
|
|
49
|
|
|
$route = m::mock(Route::class); |
|
50
|
|
|
|
|
51
|
|
|
$route->shouldReceive('dispatch')->withArgs(function ($req, $withRoute) use ($request) { |
|
|
|
|
|
|
52
|
|
|
if ($withRoute) { |
|
53
|
|
|
$withRoute(); |
|
54
|
|
|
} |
|
55
|
|
|
return $req === $request; |
|
56
|
|
|
})->andReturn($response); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$route->shouldReceive('config')->with('route_annotation')->andReturn(true); |
|
59
|
|
|
|
|
60
|
|
|
$this->app->shouldReceive('get')->with('route')->andReturn($route); |
|
61
|
|
|
|
|
62
|
|
|
$console = m::mock(Console::class); |
|
63
|
|
|
|
|
64
|
|
|
$console->shouldReceive('call'); |
|
65
|
|
|
|
|
66
|
|
|
$this->app->shouldReceive('get')->with('console')->andReturn($console); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testRun() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$root = vfsStream::setup('rootDir', null, [ |
|
|
|
|
|
|
72
|
|
|
'app' => [ |
|
73
|
|
|
'controller' => [], |
|
74
|
|
|
'middleware.php' => '<?php return [];', |
|
75
|
|
|
], |
|
76
|
|
|
'route' => [ |
|
77
|
|
|
'route.php' => '<?php return [];', |
|
78
|
|
|
], |
|
79
|
|
|
]); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$this->http->multi(false); |
|
82
|
|
|
|
|
83
|
|
|
$this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR); |
|
84
|
|
|
$this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR); |
|
85
|
|
|
|
|
86
|
|
|
$request = m::mock(Request::class)->makePartial(); |
|
87
|
|
|
$response = m::mock(Response::class)->makePartial(); |
|
88
|
|
|
|
|
89
|
|
|
$this->prepareApp($request, $response); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$this->assertFalse($this->http->isMulti()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
|
|
|
|
|
97
|
|
|
* @param $request |
|
|
|
|
|
|
98
|
|
|
* @param $auto |
|
|
|
|
|
|
99
|
|
|
* @param $name |
|
|
|
|
|
|
100
|
|
|
* @dataProvider multiAppRunProvider |
|
101
|
|
|
*/ |
|
|
|
|
|
|
102
|
|
|
public function testMultiAppRun($request, $auto, $name, $path = null) |
|
103
|
|
|
{ |
|
104
|
|
|
$root = vfsStream::setup('rootDir', null, [ |
|
|
|
|
|
|
105
|
|
|
'app' => [ |
|
106
|
|
|
'middleware.php' => '<?php return [];', |
|
107
|
|
|
'app1' => [ |
|
108
|
|
|
'common.php' => '', |
|
109
|
|
|
'event.php' => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];', |
|
110
|
|
|
'provider.php' => '<?php return [];', |
|
111
|
|
|
'middleware.php' => '<?php return [];', |
|
112
|
|
|
'config' => [ |
|
113
|
|
|
'app.php' => '<?php return [];', |
|
114
|
|
|
], |
|
115
|
|
|
], |
|
116
|
|
|
], |
|
117
|
|
|
'config' => [ |
|
118
|
|
|
'app.php' => '<?php return [];', |
|
119
|
|
|
'app1' => [ |
|
120
|
|
|
'app.php' => '<?php return [];', |
|
121
|
|
|
], |
|
122
|
|
|
], |
|
123
|
|
|
'route' => [ |
|
124
|
|
|
'route.php' => '<?php return [];', |
|
125
|
|
|
], |
|
126
|
|
|
]); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$config = m::mock(Config::class)->makePartial(); |
|
129
|
|
|
|
|
130
|
|
|
$config->shouldReceive('get')->with('app.auto_multi_app', false)->andReturn($auto); |
|
131
|
|
|
|
|
132
|
|
|
$config->shouldReceive('get')->with('app.domain_bind', [])->andReturn([ |
|
|
|
|
|
|
133
|
|
|
'www.domain.com' => 'app1', |
|
134
|
|
|
'app2' => 'app2', |
|
135
|
|
|
]); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$config->shouldReceive('get')->with('app.app_map', [])->andReturn([ |
|
|
|
|
|
|
138
|
|
|
'some1' => 'app3', |
|
139
|
|
|
]); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$this->app->shouldReceive('get')->with('config')->andReturn($config); |
|
142
|
|
|
|
|
143
|
|
|
$this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR); |
|
144
|
|
|
$this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR); |
|
145
|
|
|
$this->app->shouldReceive('getConfigPath')->andReturn($root->getChild('config')->url() . DIRECTORY_SEPARATOR); |
|
146
|
|
|
|
|
147
|
|
|
$response = m::mock(Response::class)->makePartial(); |
|
148
|
|
|
|
|
149
|
|
|
$this->prepareApp($request, $response); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertTrue($this->http->isMulti()); |
|
152
|
|
|
|
|
153
|
|
|
if ($name === null) { |
|
154
|
|
|
$this->http->shouldReceive('reportException')->once(); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
$this->http->shouldReceive('renderException')->once()->andReturn($response); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (!$auto) { |
|
160
|
|
|
$this->http->name($name); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ($path) { |
|
164
|
|
|
$this->http->path($path); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
|
168
|
|
|
|
|
169
|
|
|
if ($name !== null) { |
|
170
|
|
|
$this->assertEquals($name, $this->http->getName()); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if ($name === 'app1' || $name === 'app2') { |
|
174
|
|
|
$this->assertTrue($this->http->isBindDomain()); |
|
175
|
|
|
} |
|
176
|
|
|
if ($path) { |
|
177
|
|
|
$this->assertEquals($path, $this->app->getAppPath()); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function multiAppRunProvider() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
$request1 = m::mock(Request::class)->makePartial(); |
|
184
|
|
|
$request1->shouldReceive('subDomain')->andReturn('www'); |
|
185
|
|
|
$request1->shouldReceive('host')->andReturn('www.domain.com'); |
|
186
|
|
|
|
|
187
|
|
|
$request2 = m::mock(Request::class)->makePartial(); |
|
188
|
|
|
$request2->shouldReceive('subDomain')->andReturn('app2'); |
|
189
|
|
|
$request2->shouldReceive('host')->andReturn('app2.domain.com'); |
|
190
|
|
|
|
|
191
|
|
|
$request3 = m::mock(Request::class)->makePartial(); |
|
192
|
|
|
$request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c'); |
|
193
|
|
|
|
|
194
|
|
|
$request4 = m::mock(Request::class)->makePartial(); |
|
195
|
|
|
$request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c'); |
|
196
|
|
|
|
|
197
|
|
|
$request5 = m::mock(Request::class)->makePartial(); |
|
198
|
|
|
$request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c'); |
|
199
|
|
|
|
|
200
|
|
|
return [ |
|
201
|
|
|
[$request1, true, 'app1'], |
|
202
|
|
|
[$request2, true, 'app2'], |
|
203
|
|
|
[$request3, true, 'app3'], |
|
204
|
|
|
[$request4, true, null], |
|
205
|
|
|
[$request5, true, 'some2', 'path'], |
|
206
|
|
|
[$request1, false, 'some3'], |
|
207
|
|
|
]; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function testRunWithException() |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
$request = m::mock(Request::class); |
|
213
|
|
|
$response = m::mock(Response::class); |
|
214
|
|
|
|
|
215
|
|
|
$this->app->shouldReceive('instance')->once()->with('request', $request); |
|
216
|
|
|
|
|
217
|
|
|
$exception = new Exception(); |
|
218
|
|
|
|
|
219
|
|
|
$this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception); |
|
220
|
|
|
|
|
221
|
|
|
$handle = m::mock(Handle::class); |
|
222
|
|
|
|
|
223
|
|
|
$handle->shouldReceive('report')->once()->with($exception); |
|
224
|
|
|
$handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response); |
|
225
|
|
|
|
|
226
|
|
|
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle); |
|
227
|
|
|
|
|
228
|
|
|
$response->shouldReceive('setCookie')->andReturn($response); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function testEnd() |
|
|
|
|
|
|
234
|
|
|
{ |
|
235
|
|
|
$response = m::mock(Response::class); |
|
236
|
|
|
$event = m::mock(Event::class); |
|
237
|
|
|
$event->shouldReceive('trigger')->once()->with('HttpEnd', $response); |
|
238
|
|
|
$this->app->shouldReceive('get')->once()->with('event')->andReturn($event); |
|
239
|
|
|
$log = m::mock(Log::class); |
|
240
|
|
|
$log->shouldReceive('save')->once(); |
|
241
|
|
|
$this->app->shouldReceive('get')->once()->with('log')->andReturn($log); |
|
242
|
|
|
$session = m::mock(Session::class); |
|
243
|
|
|
$session->shouldReceive('save')->once(); |
|
244
|
|
|
$this->app->shouldReceive('get')->once()->with('session')->andReturn($session); |
|
245
|
|
|
|
|
246
|
|
|
$this->http->end($response); |
|
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
|