In this repo i store all my websites, each in a different branch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

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