MiddlewareTest.php 3.1 KB

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