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->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR); |
81
|
|
|
$this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR); |
82
|
|
|
|
83
|
|
|
$request = m::mock(Request::class)->makePartial(); |
84
|
|
|
$response = m::mock(Response::class)->makePartial(); |
85
|
|
|
|
86
|
|
|
$this->prepareApp($request, $response); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function multiAppRunProvider() |
92
|
|
|
{ |
93
|
|
|
$request1 = m::mock(Request::class)->makePartial(); |
94
|
|
|
$request1->shouldReceive('subDomain')->andReturn('www'); |
95
|
|
|
$request1->shouldReceive('host')->andReturn('www.domain.com'); |
96
|
|
|
|
97
|
|
|
$request2 = m::mock(Request::class)->makePartial(); |
98
|
|
|
$request2->shouldReceive('subDomain')->andReturn('app2'); |
99
|
|
|
$request2->shouldReceive('host')->andReturn('app2.domain.com'); |
100
|
|
|
|
101
|
|
|
$request3 = m::mock(Request::class)->makePartial(); |
102
|
|
|
$request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c'); |
103
|
|
|
|
104
|
|
|
$request4 = m::mock(Request::class)->makePartial(); |
105
|
|
|
$request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c'); |
106
|
|
|
|
107
|
|
|
$request5 = m::mock(Request::class)->makePartial(); |
108
|
|
|
$request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c'); |
109
|
|
|
|
110
|
|
|
return [ |
111
|
|
|
[$request1, true, 'app1'], |
112
|
|
|
[$request2, true, 'app2'], |
113
|
|
|
[$request3, true, 'app3'], |
114
|
|
|
[$request4, true, null], |
115
|
|
|
[$request5, true, 'some2', 'path'], |
116
|
|
|
[$request1, false, 'some3'], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testRunWithException() |
121
|
|
|
{ |
122
|
|
|
$request = m::mock(Request::class); |
123
|
|
|
$response = m::mock(Response::class); |
124
|
|
|
|
125
|
|
|
$this->app->shouldReceive('instance')->once()->with('request', $request); |
126
|
|
|
|
127
|
|
|
$exception = new Exception(); |
128
|
|
|
|
129
|
|
|
$this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception); |
130
|
|
|
|
131
|
|
|
$handle = m::mock(Handle::class); |
132
|
|
|
|
133
|
|
|
$handle->shouldReceive('report')->once()->with($exception); |
134
|
|
|
$handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response); |
135
|
|
|
|
136
|
|
|
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle); |
137
|
|
|
|
138
|
|
|
$this->assertEquals($response, $this->http->run($request)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testEnd() |
142
|
|
|
{ |
143
|
|
|
$response = m::mock(Response::class); |
144
|
|
|
$event = m::mock(Event::class); |
145
|
|
|
$event->shouldReceive('trigger')->once()->with(HttpEnd::class, $response); |
146
|
|
|
$this->app->shouldReceive('get')->once()->with('event')->andReturn($event); |
147
|
|
|
$log = m::mock(Log::class); |
148
|
|
|
$log->shouldReceive('save')->once(); |
149
|
|
|
$this->app->shouldReceive('get')->once()->with('log')->andReturn($log); |
150
|
|
|
|
151
|
|
|
$this->http->end($response); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|