StaticPrefixCollectionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. $collection->optimizeGroups();
  20. $dumped = $this->dumpCollection($collection);
  21. $this->assertEquals($expected, $dumped);
  22. }
  23. public function routeProvider()
  24. {
  25. return [
  26. 'Simple - not nested' => [
  27. [
  28. ['/', 'root'],
  29. ['/prefix/segment/', 'prefix_segment'],
  30. ['/leading/segment/', 'leading_segment'],
  31. ],
  32. <<<EOF
  33. / root
  34. /prefix/segment prefix_segment
  35. /leading/segment leading_segment
  36. EOF
  37. ],
  38. 'Not nested - group too small' => [
  39. [
  40. ['/', 'root'],
  41. ['/prefix/segment/aa', 'prefix_segment'],
  42. ['/prefix/segment/bb', 'leading_segment'],
  43. ],
  44. <<<EOF
  45. / root
  46. /prefix/segment/aa prefix_segment
  47. /prefix/segment/bb 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 prefix_segment
  60. -> /prefix/segment/bb 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. -> /group/segment nested_segment
  74. -> /group/thing some_segment
  75. -> /group/other 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. -> /group/aa aa
  91. -> /group/bb bb
  92. -> /group/cc cc
  93. / root
  94. /group
  95. -> /group/dd dd
  96. -> /group/ee ee
  97. -> /group/ff 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/group/ff/', 'ff'],
  110. ['/aaa/222/', 'second_aaa'],
  111. ['/aaa/333/', 'third_aaa'],
  112. ],
  113. <<<EOF
  114. /aaa
  115. -> /aaa/111 first_aaa
  116. -> /aaa/222 second_aaa
  117. -> /aaa/333 third_aaa
  118. /prefixed
  119. -> /prefixed/group
  120. -> -> /prefixed/group/aa aa
  121. -> -> /prefixed/group/bb bb
  122. -> -> /prefixed/group/cc cc
  123. -> /prefixed root
  124. -> /prefixed/group
  125. -> -> /prefixed/group/dd dd
  126. -> -> /prefixed/group/ee ee
  127. -> -> /prefixed/group/ff ff
  128. EOF
  129. ],
  130. 'Group regardless of segments' => [
  131. [
  132. ['/aaa-111/', 'a1'],
  133. ['/aaa-222/', 'a2'],
  134. ['/aaa-333/', 'a3'],
  135. ['/group-aa/', 'g1'],
  136. ['/group-bb/', 'g2'],
  137. ['/group-cc/', 'g3'],
  138. ],
  139. <<<EOF
  140. /aaa-
  141. -> /aaa-111 a1
  142. -> /aaa-222 a2
  143. -> /aaa-333 a3
  144. /group-
  145. -> /group-aa g1
  146. -> /group-bb g2
  147. -> /group-cc g3
  148. EOF
  149. ],
  150. ];
  151. }
  152. private function dumpCollection(StaticPrefixCollection $collection, $prefix = '')
  153. {
  154. $lines = [];
  155. foreach ($collection->getItems() as $item) {
  156. if ($item instanceof StaticPrefixCollection) {
  157. $lines[] = $prefix.$item->getPrefix();
  158. $lines[] = $this->dumpCollection($item, $prefix.'-> ');
  159. } else {
  160. $lines[] = $prefix.implode(' ', $item);
  161. }
  162. }
  163. return implode("\n", $lines);
  164. }
  165. }