RouteTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace think\tests;
  3. use Closure;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use PHPUnit\Framework\TestCase;
  7. use think\helper\Str;
  8. use think\Request;
  9. use think\response\Redirect;
  10. use think\response\View;
  11. use think\Route;
  12. class RouteTest extends TestCase
  13. {
  14. use InteractsWithApp;
  15. /** @var Route|MockInterface */
  16. protected $route;
  17. protected function tearDown(): void
  18. {
  19. m::close();
  20. }
  21. protected function setUp()
  22. {
  23. $this->prepareApp();
  24. $this->route = new Route($this->app);
  25. }
  26. /**
  27. * @param $path
  28. * @param string $method
  29. * @param string $host
  30. * @return m\Mock|Request
  31. */
  32. protected function makeRequest($path, $method = 'GET', $host = 'localhost')
  33. {
  34. $request = m::mock(Request::class)->makePartial();
  35. $request->shouldReceive('host')->andReturn($host);
  36. $request->shouldReceive('pathinfo')->andReturn($path);
  37. $request->shouldReceive('url')->andReturn('/' . $path);
  38. $request->shouldReceive('method')->andReturn(strtoupper($method));
  39. return $request;
  40. }
  41. public function testSimpleRequest()
  42. {
  43. $this->route->get('foo', function () {
  44. return 'get-foo';
  45. });
  46. $this->route->put('foo', function () {
  47. return 'put-foo';
  48. });
  49. $this->route->group(function () {
  50. $this->route->post('foo', function () {
  51. return 'post-foo';
  52. });
  53. });
  54. $request = $this->makeRequest('foo', 'post');
  55. $response = $this->route->dispatch($request);
  56. $this->assertEquals(200, $response->getCode());
  57. $this->assertEquals('post-foo', $response->getContent());
  58. $request = $this->makeRequest('foo', 'get');
  59. $response = $this->route->dispatch($request);
  60. $this->assertEquals(200, $response->getCode());
  61. $this->assertEquals('get-foo', $response->getContent());
  62. }
  63. public function testOptionsRequest()
  64. {
  65. $this->route->get('foo', function () {
  66. return 'get-foo';
  67. });
  68. $this->route->put('foo', function () {
  69. return 'put-foo';
  70. });
  71. $this->route->group(function () {
  72. $this->route->post('foo', function () {
  73. return 'post-foo';
  74. });
  75. });
  76. $this->route->group('abc', function () {
  77. $this->route->post('foo/:id', function () {
  78. return 'post-abc-foo';
  79. });
  80. });
  81. $this->route->post('foo/:id', function () {
  82. return 'post-abc-foo';
  83. });
  84. $this->route->resource('bar', 'SomeClass');
  85. $request = $this->makeRequest('foo', 'options');
  86. $response = $this->route->dispatch($request);
  87. $this->assertEquals(204, $response->getCode());
  88. $this->assertEquals('GET, PUT, POST', $response->getHeader('Allow'));
  89. $request = $this->makeRequest('bar', 'options');
  90. $response = $this->route->dispatch($request);
  91. $this->assertEquals(204, $response->getCode());
  92. $this->assertEquals('GET, POST', $response->getHeader('Allow'));
  93. $request = $this->makeRequest('bar/1', 'options');
  94. $response = $this->route->dispatch($request);
  95. $this->assertEquals(204, $response->getCode());
  96. $this->assertEquals('GET, PUT, DELETE', $response->getHeader('Allow'));
  97. $request = $this->makeRequest('xxxx', 'options');
  98. $response = $this->route->dispatch($request);
  99. $this->assertEquals(204, $response->getCode());
  100. $this->assertEquals('GET, POST, PUT, DELETE', $response->getHeader('Allow'));
  101. }
  102. public function testAllowCrossDomain()
  103. {
  104. $this->route->get('foo', function () {
  105. return 'get-foo';
  106. })->allowCrossDomain(['some' => 'bar']);
  107. $request = $this->makeRequest('foo', 'get');
  108. $response = $this->route->dispatch($request);
  109. $this->assertEquals('bar', $response->getHeader('some'));
  110. $this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
  111. $request = $this->makeRequest('foo2', 'options');
  112. $response = $this->route->dispatch($request);
  113. $this->assertEquals(204, $response->getCode());
  114. $this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
  115. $this->assertEquals('GET, POST, PUT, DELETE', $response->getHeader('Allow'));
  116. }
  117. public function testControllerDispatch()
  118. {
  119. $this->route->get('foo', 'foo/bar');
  120. $controller = m::Mock(\stdClass::class);
  121. $this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
  122. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  123. $controller->shouldReceive('bar')->andReturn('bar');
  124. $request = $this->makeRequest('foo');
  125. $response = $this->route->dispatch($request);
  126. $this->assertEquals('bar', $response->getContent());
  127. }
  128. public function testEmptyControllerDispatch()
  129. {
  130. $this->route->get('foo', 'foo/bar');
  131. $controller = m::Mock(\stdClass::class);
  132. $this->app->shouldReceive('parseClass')->with('controller', 'Error')->andReturn($controller->mockery_getName());
  133. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  134. $controller->shouldReceive('bar')->andReturn('bar');
  135. $request = $this->makeRequest('foo');
  136. $response = $this->route->dispatch($request);
  137. $this->assertEquals('bar', $response->getContent());
  138. }
  139. protected function createMiddleware($times = 1)
  140. {
  141. $middleware = m::mock(Str::random(5));
  142. $middleware->shouldReceive('handle')->times($times)->andReturnUsing(function ($request, Closure $next) {
  143. return $next($request);
  144. });
  145. $this->app->shouldReceive('make')->with($middleware->mockery_getName())->andReturn($middleware);
  146. return $middleware;
  147. }
  148. public function testControllerWithMiddleware()
  149. {
  150. $this->route->get('foo', 'foo/bar');
  151. $controller = m::mock(FooClass::class);
  152. $controller->middleware = [
  153. $this->createMiddleware()->mockery_getName() . ":params1:params2",
  154. $this->createMiddleware(0)->mockery_getName() => ['except' => 'bar'],
  155. $this->createMiddleware()->mockery_getName() => ['only' => 'bar'],
  156. ];
  157. $this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
  158. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  159. $controller->shouldReceive('bar')->once()->andReturn('bar');
  160. $request = $this->makeRequest('foo');
  161. $response = $this->route->dispatch($request);
  162. $this->assertEquals('bar', $response->getContent());
  163. }
  164. public function testUrlDispatch()
  165. {
  166. $controller = m::mock(FooClass::class);
  167. $controller->shouldReceive('index')->andReturn('bar');
  168. $this->app->shouldReceive('parseClass')->once()->with('controller', 'Foo')->andReturn($controller->mockery_getName());
  169. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  170. $request = $this->makeRequest('foo');
  171. $response = $this->route->dispatch($request);
  172. $this->assertEquals('bar', $response->getContent());
  173. }
  174. public function testRedirectDispatch()
  175. {
  176. $this->route->redirect('foo', 'http://localhost', 302);
  177. $request = $this->makeRequest('foo');
  178. $this->app->shouldReceive('make')->with(Request::class)->andReturn($request);
  179. $response = $this->route->dispatch($request);
  180. $this->assertInstanceOf(Redirect::class, $response);
  181. $this->assertEquals(302, $response->getCode());
  182. $this->assertEquals('http://localhost', $response->getData());
  183. }
  184. public function testViewDispatch()
  185. {
  186. $this->route->view('foo', 'index/hello', ['city' => 'shanghai']);
  187. $request = $this->makeRequest('foo');
  188. $response = $this->route->dispatch($request);
  189. $this->assertInstanceOf(View::class, $response);
  190. $this->assertEquals(['city' => 'shanghai'], $response->getVars());
  191. $this->assertEquals('index/hello', $response->getData());
  192. }
  193. public function testResponseDispatch()
  194. {
  195. $this->route->get('hello/:name', response()
  196. ->data('Hello,ThinkPHP')
  197. ->code(200)
  198. ->contentType('text/plain'));
  199. $request = $this->makeRequest('hello/some');
  200. $response = $this->route->dispatch($request);
  201. $this->assertEquals('Hello,ThinkPHP', $response->getContent());
  202. $this->assertEquals(200, $response->getCode());
  203. }
  204. public function testDomainBindResponse()
  205. {
  206. $this->route->domain('test', function () {
  207. $this->route->get('/', function () {
  208. return 'Hello,ThinkPHP';
  209. });
  210. });
  211. $request = $this->makeRequest('', 'get', 'test.domain.com');
  212. $response = $this->route->dispatch($request);
  213. $this->assertEquals('Hello,ThinkPHP', $response->getContent());
  214. $this->assertEquals(200, $response->getCode());
  215. }
  216. }
  217. class FooClass
  218. {
  219. public $middleware = [];
  220. public function bar()
  221. {
  222. }
  223. }