RouteTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. 'middleware' => [$this->createMiddleware()->mockery_getName(), [new \stdClass()]],
  158. 'options' => ['only' => 'bar'],
  159. ],
  160. ];
  161. $this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
  162. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  163. $controller->shouldReceive('bar')->once()->andReturn('bar');
  164. $request = $this->makeRequest('foo');
  165. $response = $this->route->dispatch($request);
  166. $this->assertEquals('bar', $response->getContent());
  167. }
  168. public function testUrlDispatch()
  169. {
  170. $controller = m::mock(FooClass::class);
  171. $controller->shouldReceive('index')->andReturn('bar');
  172. $this->app->shouldReceive('parseClass')->once()->with('controller', 'Foo')
  173. ->andReturn($controller->mockery_getName());
  174. $this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
  175. $request = $this->makeRequest('foo');
  176. $response = $this->route->dispatch($request);
  177. $this->assertEquals('bar', $response->getContent());
  178. }
  179. public function testRedirectDispatch()
  180. {
  181. $this->route->redirect('foo', 'http://localhost', 302);
  182. $request = $this->makeRequest('foo');
  183. $this->app->shouldReceive('make')->with(Request::class)->andReturn($request);
  184. $response = $this->route->dispatch($request);
  185. $this->assertInstanceOf(Redirect::class, $response);
  186. $this->assertEquals(302, $response->getCode());
  187. $this->assertEquals('http://localhost', $response->getData());
  188. }
  189. public function testViewDispatch()
  190. {
  191. $this->route->view('foo', 'index/hello', ['city' => 'shanghai']);
  192. $request = $this->makeRequest('foo');
  193. $response = $this->route->dispatch($request);
  194. $this->assertInstanceOf(View::class, $response);
  195. $this->assertEquals(['city' => 'shanghai'], $response->getVars());
  196. $this->assertEquals('index/hello', $response->getData());
  197. }
  198. public function testResponseDispatch()
  199. {
  200. $this->route->get('hello/:name', response()
  201. ->data('Hello,ThinkPHP')
  202. ->code(200)
  203. ->contentType('text/plain'));
  204. $request = $this->makeRequest('hello/some');
  205. $response = $this->route->dispatch($request);
  206. $this->assertEquals('Hello,ThinkPHP', $response->getContent());
  207. $this->assertEquals(200, $response->getCode());
  208. }
  209. public function testDomainBindResponse()
  210. {
  211. $this->route->domain('test', function () {
  212. $this->route->get('/', function () {
  213. return 'Hello,ThinkPHP';
  214. });
  215. });
  216. $request = $this->makeRequest('', 'get', 'test.domain.com');
  217. $response = $this->route->dispatch($request);
  218. $this->assertEquals('Hello,ThinkPHP', $response->getContent());
  219. $this->assertEquals(200, $response->getCode());
  220. }
  221. }
  222. class FooClass
  223. {
  224. public $middleware = [];
  225. public function bar()
  226. {
  227. }
  228. }