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