HttpTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace think\tests;
  3. use Mockery as m;
  4. use Mockery\MockInterface;
  5. use org\bovigo\vfs\vfsStream;
  6. use PHPUnit\Framework\TestCase;
  7. use think\App;
  8. use think\Console;
  9. use think\Event;
  10. use think\event\HttpEnd;
  11. use think\Exception;
  12. use think\exception\Handle;
  13. use think\Http;
  14. use think\Log;
  15. use think\Request;
  16. use think\Response;
  17. use think\Route;
  18. class HttpTest extends TestCase
  19. {
  20. /** @var App|MockInterface */
  21. protected $app;
  22. /** @var Http|MockInterface */
  23. protected $http;
  24. protected function tearDown(): void
  25. {
  26. m::close();
  27. }
  28. protected function setUp()
  29. {
  30. $this->app = m::mock(App::class)->makePartial();
  31. $this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial();
  32. }
  33. protected function prepareApp($request, $response)
  34. {
  35. $this->app->shouldReceive('instance')->once()->with('request', $request);
  36. $this->app->shouldReceive('initialized')->once()->andReturnFalse();
  37. $this->app->shouldReceive('initialize')->once();
  38. $this->app->shouldReceive('get')->with('request')->andReturn($request);
  39. $route = m::mock(Route::class);
  40. $route->shouldReceive('dispatch')->withArgs(function ($req, $withRoute) use ($request) {
  41. if ($withRoute) {
  42. $withRoute();
  43. }
  44. return $req === $request;
  45. })->andReturn($response);
  46. $route->shouldReceive('config')->with('route_annotation')->andReturn(true);
  47. $this->app->shouldReceive('get')->with('route')->andReturn($route);
  48. $console = m::mock(Console::class);
  49. $console->shouldReceive('call');
  50. $this->app->shouldReceive('get')->with('console')->andReturn($console);
  51. }
  52. public function testRun()
  53. {
  54. $root = vfsStream::setup('rootDir', null, [
  55. 'app' => [
  56. 'controller' => [],
  57. 'middleware.php' => '<?php return [];',
  58. ],
  59. 'route' => [
  60. 'route.php' => '<?php return [];',
  61. ],
  62. ]);
  63. $this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
  64. $this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
  65. $request = m::mock(Request::class)->makePartial();
  66. $response = m::mock(Response::class)->makePartial();
  67. $this->prepareApp($request, $response);
  68. $this->assertEquals($response, $this->http->run($request));
  69. }
  70. public function multiAppRunProvider()
  71. {
  72. $request1 = m::mock(Request::class)->makePartial();
  73. $request1->shouldReceive('subDomain')->andReturn('www');
  74. $request1->shouldReceive('host')->andReturn('www.domain.com');
  75. $request2 = m::mock(Request::class)->makePartial();
  76. $request2->shouldReceive('subDomain')->andReturn('app2');
  77. $request2->shouldReceive('host')->andReturn('app2.domain.com');
  78. $request3 = m::mock(Request::class)->makePartial();
  79. $request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c');
  80. $request4 = m::mock(Request::class)->makePartial();
  81. $request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c');
  82. $request5 = m::mock(Request::class)->makePartial();
  83. $request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c');
  84. return [
  85. [$request1, true, 'app1'],
  86. [$request2, true, 'app2'],
  87. [$request3, true, 'app3'],
  88. [$request4, true, null],
  89. [$request5, true, 'some2', 'path'],
  90. [$request1, false, 'some3'],
  91. ];
  92. }
  93. public function testRunWithException()
  94. {
  95. $request = m::mock(Request::class);
  96. $response = m::mock(Response::class);
  97. $this->app->shouldReceive('instance')->once()->with('request', $request);
  98. $exception = new Exception();
  99. $this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);
  100. $handle = m::mock(Handle::class);
  101. $handle->shouldReceive('report')->once()->with($exception);
  102. $handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response);
  103. $this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
  104. $this->assertEquals($response, $this->http->run($request));
  105. }
  106. public function testEnd()
  107. {
  108. $response = m::mock(Response::class);
  109. $event = m::mock(Event::class);
  110. $event->shouldReceive('trigger')->once()->with(HttpEnd::class, $response);
  111. $this->app->shouldReceive('get')->once()->with('event')->andReturn($event);
  112. $log = m::mock(Log::class);
  113. $log->shouldReceive('save')->once();
  114. $this->app->shouldReceive('get')->once()->with('log')->andReturn($log);
  115. $this->http->end($response);
  116. }
  117. }