StaticPrefixCollectionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Component\Routing\Matcher\Dumper\StaticPrefixCollection;
  5. use Symfony\Component\Routing\Route;
  6. class StaticPrefixCollectionTest extends TestCase
  7. {
  8. /**
  9. * @dataProvider routeProvider
  10. */
  11. public function testGrouping(array $routes, $expected)
  12. {
  13. $collection = new StaticPrefixCollection('/');
  14. foreach ($routes as $route) {
  15. list($path, $name) = $route;
  16. $staticPrefix = (new Route($path))->compile()->getStaticPrefix();
  17. $collection->addRoute($staticPrefix, [$name]);
  18. }
  19. $dumped = $this->dumpCollection($collection);
  20. $this->assertEquals($expected, $dumped);
  21. }
  22. public function routeProvider()
  23. {
  24. return [
  25. 'Simple - not nested' => [
  26. [
  27. ['/', 'root'],
  28. ['/prefix/segment/', 'prefix_segment'],
  29. ['/leading/segment/', 'leading_segment'],
  30. ],
  31. <<<EOF
  32. root
  33. prefix_segment
  34. leading_segment
  35. EOF
  36. ],
  37. 'Nested - small group' => [
  38. [
  39. ['/', 'root'],
  40. ['/prefix/segment/aa', 'prefix_segment'],
  41. ['/prefix/segment/bb', 'leading_segment'],
  42. ],
  43. <<<EOF
  44. root
  45. /prefix/segment/
  46. -> prefix_segment
  47. -> leading_segment
  48. EOF
  49. ],
  50. 'Nested - contains item at intersection' => [
  51. [
  52. ['/', 'root'],
  53. ['/prefix/segment/', 'prefix_segment'],
  54. ['/prefix/segment/bb', 'leading_segment'],
  55. ],
  56. <<<EOF
  57. root
  58. /prefix/segment/
  59. -> prefix_segment
  60. -> leading_segment
  61. EOF
  62. ],
  63. 'Simple one level nesting' => [
  64. [
  65. ['/', 'root'],
  66. ['/group/segment/', 'nested_segment'],
  67. ['/group/thing/', 'some_segment'],
  68. ['/group/other/', 'other_segment'],
  69. ],
  70. <<<EOF
  71. root
  72. /group/
  73. -> nested_segment
  74. -> some_segment
  75. -> other_segment
  76. EOF
  77. ],
  78. 'Retain matching order with groups' => [
  79. [
  80. ['/group/aa/', 'aa'],
  81. ['/group/bb/', 'bb'],
  82. ['/group/cc/', 'cc'],
  83. ['/(.*)', 'root'],
  84. ['/group/dd/', 'dd'],
  85. ['/group/ee/', 'ee'],
  86. ['/group/ff/', 'ff'],
  87. ],
  88. <<<EOF
  89. /group/
  90. -> aa
  91. -> bb
  92. -> cc
  93. root
  94. /group/
  95. -> dd
  96. -> ee
  97. -> ff
  98. EOF
  99. ],
  100. 'Retain complex matching order with groups at base' => [
  101. [
  102. ['/aaa/111/', 'first_aaa'],
  103. ['/prefixed/group/aa/', 'aa'],
  104. ['/prefixed/group/bb/', 'bb'],
  105. ['/prefixed/group/cc/', 'cc'],
  106. ['/prefixed/(.*)', 'root'],
  107. ['/prefixed/group/dd/', 'dd'],
  108. ['/prefixed/group/ee/', 'ee'],
  109. ['/prefixed/', 'parent'],
  110. ['/prefixed/group/ff/', 'ff'],
  111. ['/aaa/222/', 'second_aaa'],
  112. ['/aaa/333/', 'third_aaa'],
  113. ],
  114. <<<EOF
  115. /aaa/
  116. -> first_aaa
  117. -> second_aaa
  118. -> third_aaa
  119. /prefixed/
  120. -> /prefixed/group/
  121. -> -> aa
  122. -> -> bb
  123. -> -> cc
  124. -> root
  125. -> /prefixed/group/
  126. -> -> dd
  127. -> -> ee
  128. -> -> ff
  129. -> parent
  130. EOF
  131. ],
  132. 'Group regardless of segments' => [
  133. [
  134. ['/aaa-111/', 'a1'],
  135. ['/aaa-222/', 'a2'],
  136. ['/aaa-333/', 'a3'],
  137. ['/group-aa/', 'g1'],
  138. ['/group-bb/', 'g2'],
  139. ['/group-cc/', 'g3'],
  140. ],
  141. <<<EOF
  142. /aaa-
  143. -> a1
  144. -> a2
  145. -> a3
  146. /group-
  147. -> g1
  148. -> g2
  149. -> g3
  150. EOF
  151. ],
  152. ];
  153. }
  154. private function dumpCollection(StaticPrefixCollection $collection, $prefix = '')
  155. {
  156. $lines = [];
  157. foreach ($collection->getRoutes() as $item) {
  158. if ($item instanceof StaticPrefixCollection) {
  159. $lines[] = $prefix.$item->getPrefix();
  160. $lines[] = $this->dumpCollection($item, $prefix.'-> ');
  161. } else {
  162. $lines[] = $prefix.implode(' ', $item);
  163. }
  164. }
  165. return implode("\n", $lines);
  166. }
  167. }