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.

147 lines
3.8 KiB

8 years ago
  1. <?php
  2. namespace FastRoute\RouteParser;
  3. class StdTest extends \PhpUnit_Framework_TestCase {
  4. /** @dataProvider provideTestParse */
  5. public function testParse($routeString, $expectedRouteDatas) {
  6. $parser = new Std();
  7. $routeDatas = $parser->parse($routeString);
  8. $this->assertSame($expectedRouteDatas, $routeDatas);
  9. }
  10. /** @dataProvider provideTestParseError */
  11. public function testParseError($routeString, $expectedExceptionMessage) {
  12. $parser = new Std();
  13. $this->setExpectedException('FastRoute\\BadRouteException', $expectedExceptionMessage);
  14. $parser->parse($routeString);
  15. }
  16. public function provideTestParse() {
  17. return [
  18. [
  19. '/test',
  20. [
  21. ['/test'],
  22. ]
  23. ],
  24. [
  25. '/test/{param}',
  26. [
  27. ['/test/', ['param', '[^/]+']],
  28. ]
  29. ],
  30. [
  31. '/te{ param }st',
  32. [
  33. ['/te', ['param', '[^/]+'], 'st']
  34. ]
  35. ],
  36. [
  37. '/test/{param1}/test2/{param2}',
  38. [
  39. ['/test/', ['param1', '[^/]+'], '/test2/', ['param2', '[^/]+']]
  40. ]
  41. ],
  42. [
  43. '/test/{param:\d+}',
  44. [
  45. ['/test/', ['param', '\d+']]
  46. ]
  47. ],
  48. [
  49. '/test/{ param : \d{1,9} }',
  50. [
  51. ['/test/', ['param', '\d{1,9}']]
  52. ]
  53. ],
  54. [
  55. '/test[opt]',
  56. [
  57. ['/test'],
  58. ['/testopt'],
  59. ]
  60. ],
  61. [
  62. '/test[/{param}]',
  63. [
  64. ['/test'],
  65. ['/test/', ['param', '[^/]+']],
  66. ]
  67. ],
  68. [
  69. '/{param}[opt]',
  70. [
  71. ['/', ['param', '[^/]+']],
  72. ['/', ['param', '[^/]+'], 'opt']
  73. ]
  74. ],
  75. [
  76. '/test[/{name}[/{id:[0-9]+}]]',
  77. [
  78. ['/test'],
  79. ['/test/', ['name', '[^/]+']],
  80. ['/test/', ['name', '[^/]+'], '/', ['id', '[0-9]+']],
  81. ]
  82. ],
  83. [
  84. '',
  85. [
  86. [''],
  87. ]
  88. ],
  89. [
  90. '[test]',
  91. [
  92. [''],
  93. ['test'],
  94. ]
  95. ],
  96. [
  97. '/{foo-bar}',
  98. [
  99. ['/', ['foo-bar', '[^/]+']]
  100. ]
  101. ],
  102. [
  103. '/{_foo:.*}',
  104. [
  105. ['/', ['_foo', '.*']]
  106. ]
  107. ],
  108. ];
  109. }
  110. public function provideTestParseError() {
  111. return [
  112. [
  113. '/test[opt',
  114. "Number of opening '[' and closing ']' does not match"
  115. ],
  116. [
  117. '/test[opt[opt2]',
  118. "Number of opening '[' and closing ']' does not match"
  119. ],
  120. [
  121. '/testopt]',
  122. "Number of opening '[' and closing ']' does not match"
  123. ],
  124. [
  125. '/test[]',
  126. "Empty optional part"
  127. ],
  128. [
  129. '/test[[opt]]',
  130. "Empty optional part"
  131. ],
  132. [
  133. '[[test]]',
  134. "Empty optional part"
  135. ],
  136. [
  137. '/test[/opt]/required',
  138. "Optional segments can only occur at the end of a route"
  139. ],
  140. ];
  141. }
  142. }