|
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\Console; |
|
11
|
|
|
use think\Event; |
|
12
|
|
|
use think\event\HttpEnd; |
|
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
|
|
|
|
|
21
|
|
|
class HttpTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var App|MockInterface */ |
|
|
|
|
|
|
24
|
|
|
protected $app; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Http|MockInterface */ |
|
|
|
|
|
|
27
|
|
|
protected $http; |
|
28
|
|
|
|
|
29
|
|
|
protected function tearDown(): void |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
m::close(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp() |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$this->app = m::mock(App::class)->makePartial(); |
|
37
|
|
|
|
|
38
|
|
|
$this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function prepareApp($request, $response) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$this->app->shouldReceive('instance')->once()->with('request', $request); |
|
|
|
|
|
|
44
|
|
|
$this->app->shouldReceive('initialized')->once()->andReturnFalse(); |
|
|
|
|
|
|
45
|
|
|
$this->app->shouldReceive('initialize')->once(); |
|
46
|
|
|
$this->app->shouldReceive('get')->with('request')->andReturn($request); |
|
47
|
|
|
|
|
48
|
|
|
$route = m::mock(Route::class); |
|
49
|
|
|
|
|
50
|
|
|
$route->shouldReceive('dispatch')->withArgs(function ($req, $withRoute) use ($request) { |
|
|
|
|
|
|
51
|
|
|
if ($withRoute) { |
|
52
|
|
|
$withRoute(); |
|
53
|
|
|
} |
|
54
|
|
|
return $req === $request; |
|
55
|
|
|
})->andReturn($response); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$route->shouldReceive('config')->with('route_annotation')->andReturn(true); |
|
58
|
|
|
|
|
59
|
|
|
$this->app->shouldReceive('get')->with('route')->andReturn($route); |
|
60
|
|
|
|
|
61
|
|
|
$console = m::mock(Console::class); |
|
62
|
|
|
|
|
63
|
|
|
$console->shouldReceive('call'); |
|
64
|
|
|
|
|
65
|
|
|
$this->app->shouldReceive('get')->with('console')->andReturn($console); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testRun() |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$root = vfsStream::setup('rootDir', null, [ |
|
|
|
|
|
|
71
|
|
|
'app' => [ |
|
72
|
|
|
'controller' => [], |
|
73
|
|
|
'middleware.php' => '<?php return [];', |
|
74
|
|
|
], |
|
75
|
|
|
'route' => [ |
|
76
|
|
|
'route.php' => '<?php return [];', |
|
77
|
|
|
], |
|
78
|
|
|
]); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$this->http->multi(false); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR); |
|
83
|
|
|
$this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR); |
|
84
|
|
|
|
|
85
|
|
|
$request = m::mock(Request::class)->makePartial(); |
|
86
|
|
|
$response = m::mock(Response::class)->makePartial(); |
|
87
|
|
|
|
|
88
|
|
|
$this->prepareApp($request, $response); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function multiAppRunProvider() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$request1 = m::mock(Request::class)->makePartial(); |
|
96
|
|
|
$request1->shouldReceive('subDomain')->andReturn('www'); |
|
97
|
|
|
$request1->shouldReceive('host')->andReturn('www.domain.com'); |
|
98
|
|
|
|
|
99
|
|
|
$request2 = m::mock(Request::class)->makePartial(); |
|
100
|
|
|
$request2->shouldReceive('subDomain')->andReturn('app2'); |
|
101
|
|
|
$request2->shouldReceive('host')->andReturn('app2.domain.com'); |
|
102
|
|
|
|
|
103
|
|
|
$request3 = m::mock(Request::class)->makePartial(); |
|
104
|
|
|
$request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c'); |
|
105
|
|
|
|
|
106
|
|
|
$request4 = m::mock(Request::class)->makePartial(); |
|
107
|
|
|
$request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c'); |
|
108
|
|
|
|
|
109
|
|
|
$request5 = m::mock(Request::class)->makePartial(); |
|
110
|
|
|
$request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c'); |
|
111
|
|
|
|
|
112
|
|
|
return [ |
|
113
|
|
|
[$request1, true, 'app1'], |
|
114
|
|
|
[$request2, true, 'app2'], |
|
115
|
|
|
[$request3, true, 'app3'], |
|
116
|
|
|
[$request4, true, null], |
|
117
|
|
|
[$request5, true, 'some2', 'path'], |
|
118
|
|
|
[$request1, false, 'some3'], |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testRunWithException() |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$request = m::mock(Request::class); |
|
125
|
|
|
$response = m::mock(Response::class); |
|
126
|
|
|
|
|
127
|
|
|
$this->app->shouldReceive('instance')->once()->with('request', $request); |
|
128
|
|
|
|
|
129
|
|
|
$exception = new Exception(); |
|
130
|
|
|
|
|
131
|
|
|
$this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$handle = m::mock(Handle::class); |
|
134
|
|
|
|
|
135
|
|
|
$handle->shouldReceive('report')->once()->with($exception); |
|
136
|
|
|
$handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response); |
|
137
|
|
|
|
|
138
|
|
|
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle); |
|
139
|
|
|
|
|
140
|
|
|
$response->shouldReceive('setCookie')->andReturn($response); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testEnd() |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$response = m::mock(Response::class); |
|
148
|
|
|
$event = m::mock(Event::class); |
|
149
|
|
|
$event->shouldReceive('trigger')->once()->with(HttpEnd::class, $response); |
|
150
|
|
|
$this->app->shouldReceive('get')->once()->with('event')->andReturn($event); |
|
151
|
|
|
$log = m::mock(Log::class); |
|
152
|
|
|
$log->shouldReceive('save')->once(); |
|
153
|
|
|
$this->app->shouldReceive('get')->once()->with('log')->andReturn($log); |
|
154
|
|
|
|
|
155
|
|
|
$this->http->end($response); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|