StdTest.php 3.8 KB

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