RouteCollectorTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace FastRoute;
  3. use PHPUnit\Framework\TestCase;
  4. class RouteCollectorTest extends TestCase
  5. {
  6. public function testShortcuts()
  7. {
  8. $r = new DummyRouteCollector();
  9. $r->delete('/delete', 'delete');
  10. $r->get('/get', 'get');
  11. $r->head('/head', 'head');
  12. $r->patch('/patch', 'patch');
  13. $r->post('/post', 'post');
  14. $r->put('/put', 'put');
  15. $expected = [
  16. ['DELETE', '/delete', 'delete'],
  17. ['GET', '/get', 'get'],
  18. ['HEAD', '/head', 'head'],
  19. ['PATCH', '/patch', 'patch'],
  20. ['POST', '/post', 'post'],
  21. ['PUT', '/put', 'put'],
  22. ];
  23. $this->assertSame($expected, $r->routes);
  24. }
  25. public function testGroups()
  26. {
  27. $r = new DummyRouteCollector();
  28. $r->delete('/delete', 'delete');
  29. $r->get('/get', 'get');
  30. $r->head('/head', 'head');
  31. $r->patch('/patch', 'patch');
  32. $r->post('/post', 'post');
  33. $r->put('/put', 'put');
  34. $r->addGroup('/group-one', function (DummyRouteCollector $r) {
  35. $r->delete('/delete', 'delete');
  36. $r->get('/get', 'get');
  37. $r->head('/head', 'head');
  38. $r->patch('/patch', 'patch');
  39. $r->post('/post', 'post');
  40. $r->put('/put', 'put');
  41. $r->addGroup('/group-two', function (DummyRouteCollector $r) {
  42. $r->delete('/delete', 'delete');
  43. $r->get('/get', 'get');
  44. $r->head('/head', 'head');
  45. $r->patch('/patch', 'patch');
  46. $r->post('/post', 'post');
  47. $r->put('/put', 'put');
  48. });
  49. });
  50. $r->addGroup('/admin', function (DummyRouteCollector $r) {
  51. $r->get('-some-info', 'admin-some-info');
  52. });
  53. $r->addGroup('/admin-', function (DummyRouteCollector $r) {
  54. $r->get('more-info', 'admin-more-info');
  55. });
  56. $expected = [
  57. ['DELETE', '/delete', 'delete'],
  58. ['GET', '/get', 'get'],
  59. ['HEAD', '/head', 'head'],
  60. ['PATCH', '/patch', 'patch'],
  61. ['POST', '/post', 'post'],
  62. ['PUT', '/put', 'put'],
  63. ['DELETE', '/group-one/delete', 'delete'],
  64. ['GET', '/group-one/get', 'get'],
  65. ['HEAD', '/group-one/head', 'head'],
  66. ['PATCH', '/group-one/patch', 'patch'],
  67. ['POST', '/group-one/post', 'post'],
  68. ['PUT', '/group-one/put', 'put'],
  69. ['DELETE', '/group-one/group-two/delete', 'delete'],
  70. ['GET', '/group-one/group-two/get', 'get'],
  71. ['HEAD', '/group-one/group-two/head', 'head'],
  72. ['PATCH', '/group-one/group-two/patch', 'patch'],
  73. ['POST', '/group-one/group-two/post', 'post'],
  74. ['PUT', '/group-one/group-two/put', 'put'],
  75. ['GET', '/admin-some-info', 'admin-some-info'],
  76. ['GET', '/admin-more-info', 'admin-more-info'],
  77. ];
  78. $this->assertSame($expected, $r->routes);
  79. }
  80. }
  81. class DummyRouteCollector extends RouteCollector
  82. {
  83. public $routes = [];
  84. public function __construct()
  85. {
  86. }
  87. public function addRoute($method, $route, $handler)
  88. {
  89. $route = $this->currentGroupPrefix . $route;
  90. $this->routes[] = [$method, $route, $handler];
  91. }
  92. }