MiddlewareTest.php 3.6 KB

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