StaticPrefixCollectionTest.php 4.6 KB

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