1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace think\tests; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Mockery\MockInterface; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use think\App; |
9
|
|
|
use think\Config; |
10
|
|
|
use think\Container; |
11
|
|
|
use think\Exception; |
12
|
|
|
use think\exception\Handle; |
13
|
|
|
use think\Middleware; |
14
|
|
|
use think\Pipeline; |
15
|
|
|
use think\Request; |
16
|
|
|
use think\Response; |
17
|
|
|
|
18
|
|
|
class MiddlewareTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var App|MockInterface */ |
|
|
|
|
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** @var Middleware|MockInterface */ |
|
|
|
|
24
|
|
|
protected $middleware; |
25
|
|
|
|
26
|
|
|
/** @var Config|MockInterface */ |
|
|
|
|
27
|
|
|
protected $config; |
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
|
|
|
Container::setInstance($this->app); |
38
|
|
|
|
39
|
|
|
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app); |
40
|
|
|
$this->config = m::mock(Config::class)->makePartial(); |
41
|
|
|
$this->app->shouldReceive('get')->with('config')->andReturn($this->config); |
42
|
|
|
$this->app->shouldReceive('runningInConsole')->andReturn(false); |
43
|
|
|
|
44
|
|
|
$this->middleware = new Middleware($this->app); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSetMiddleware() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->middleware->add('BarMiddleware', 'bar'); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(1, count($this->middleware->all('bar'))); |
52
|
|
|
|
53
|
|
|
$this->middleware->controller('BarMiddleware'); |
54
|
|
|
$this->assertEquals(1, count($this->middleware->all('controller'))); |
55
|
|
|
|
56
|
|
|
$this->middleware->import(['FooMiddleware']); |
57
|
|
|
$this->assertEquals(1, count($this->middleware->all())); |
58
|
|
|
|
59
|
|
|
$this->middleware->unshift(['BazMiddleware', 'baz']); |
60
|
|
|
$this->assertEquals(2, count($this->middleware->all())); |
61
|
|
|
$this->assertEquals([['BazMiddleware', 'handle'], 'baz'], $this->middleware->all()[0]); |
62
|
|
|
|
63
|
|
|
$this->config->shouldReceive('get')->with('middleware.alias', [])->andReturn(['foo' => ['FooMiddleware', 'FarMiddleware']]); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->middleware->add('foo'); |
66
|
|
|
$this->assertEquals(4, count($this->middleware->all())); |
67
|
|
|
$this->middleware->add(function () { |
|
|
|
|
68
|
|
|
}); |
|
|
|
|
69
|
|
|
$this->assertEquals(5, count($this->middleware->all())); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testPipelineAndEnd() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$bar = m::mock("overload:BarMiddleware"); |
75
|
|
|
$foo = m::mock("overload:FooMiddleware", Foo::class); |
76
|
|
|
|
77
|
|
|
$request = m::mock(Request::class); |
78
|
|
|
$response = m::mock(Response::class); |
79
|
|
|
|
80
|
|
|
$e = new Exception(); |
81
|
|
|
|
82
|
|
|
$handle = m::mock(Handle::class); |
83
|
|
|
$handle->shouldReceive('report')->with($e)->andReturnNull(); |
|
|
|
|
84
|
|
|
$handle->shouldReceive('render')->with($request, $e)->andReturn($response); |
85
|
|
|
|
86
|
|
|
$foo->shouldReceive('handle')->once()->andReturnUsing(function ($request, $next) { |
|
|
|
|
87
|
|
|
return $next($request); |
88
|
|
|
}); |
|
|
|
|
89
|
|
|
$bar->shouldReceive('handle')->once()->andReturnUsing(function ($request, $next) use ($e) { |
|
|
|
|
90
|
|
|
$next($request); |
91
|
|
|
throw $e; |
92
|
|
|
}); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$foo->shouldReceive('end')->once()->with($response)->andReturnNull(); |
95
|
|
|
|
96
|
|
|
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$this->config->shouldReceive('get')->once()->with('middleware.priority', [])->andReturn(['FooMiddleware', 'BarMiddleware']); |
99
|
|
|
|
100
|
|
|
$this->middleware->import([function ($request, $next) { |
|
|
|
|
101
|
|
|
return $next($request); |
102
|
|
|
}, 'BarMiddleware', 'FooMiddleware']); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf(Pipeline::class, $pipeline = $this->middleware->pipeline()); |
105
|
|
|
|
106
|
|
|
$pipeline->send($request)->then(function ($request) use ($e, $response) { |
|
|
|
|
107
|
|
|
throw $e; |
108
|
|
|
}); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$this->middleware->end($response); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
class Foo |
115
|
|
|
{ |
116
|
|
|
public function end(Response $response) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|