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