StaticPrefixCollectionTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 array(
  26. 'Simple - not nested' => array(
  27. array(
  28. array('/', 'root'),
  29. array('/prefix/segment/', 'prefix_segment'),
  30. array('/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' => array(
  39. array(
  40. array('/', 'root'),
  41. array('/prefix/segment/aa', 'prefix_segment'),
  42. array('/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' => array(
  51. array(
  52. array('/', 'root'),
  53. array('/prefix/segment/', 'prefix_segment'),
  54. array('/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' => array(
  64. array(
  65. array('/', 'root'),
  66. array('/group/segment/', 'nested_segment'),
  67. array('/group/thing/', 'some_segment'),
  68. array('/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' => array(
  79. array(
  80. array('/group/aa/', 'aa'),
  81. array('/group/bb/', 'bb'),
  82. array('/group/cc/', 'cc'),
  83. array('/', 'root'),
  84. array('/group/dd/', 'dd'),
  85. array('/group/ee/', 'ee'),
  86. array('/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' => array(
  101. array(
  102. array('/aaa/111/', 'first_aaa'),
  103. array('/prefixed/group/aa/', 'aa'),
  104. array('/prefixed/group/bb/', 'bb'),
  105. array('/prefixed/group/cc/', 'cc'),
  106. array('/prefixed/', 'root'),
  107. array('/prefixed/group/dd/', 'dd'),
  108. array('/prefixed/group/ee/', 'ee'),
  109. array('/prefixed/group/ff/', 'ff'),
  110. array('/aaa/222/', 'second_aaa'),
  111. array('/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' => array(
  131. array(
  132. array('/aaa-111/', 'a1'),
  133. array('/aaa-222/', 'a2'),
  134. array('/aaa-333/', 'a3'),
  135. array('/group-aa/', 'g1'),
  136. array('/group-bb/', 'g2'),
  137. array('/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 = array();
  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. }