3 namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Routing\Matcher\Dumper\StaticPrefixCollection;
7 use Symfony\Component\Routing\Route;
9 class StaticPrefixCollectionTest extends TestCase
12 * @dataProvider routeProvider
14 public function testGrouping(array $routes, $expected)
16 $collection = new StaticPrefixCollection('/');
18 foreach ($routes as $route) {
19 list($path, $name) = $route;
20 $staticPrefix = (new Route($path))->compile()->getStaticPrefix();
21 $collection->addRoute($staticPrefix, $name);
24 $collection->optimizeGroups();
25 $dumped = $this->dumpCollection($collection);
26 $this->assertEquals($expected, $dumped);
29 public function routeProvider()
32 'Simple - not nested' => array(
35 array('/prefix/segment/', 'prefix_segment'),
36 array('/leading/segment/', 'leading_segment'),
40 /prefix/segment prefix_segment
41 /leading/segment leading_segment
44 'Not nested - group too small' => array(
47 array('/prefix/segment/aa', 'prefix_segment'),
48 array('/prefix/segment/bb', 'leading_segment'),
52 /prefix/segment/aa prefix_segment
53 /prefix/segment/bb leading_segment
56 'Nested - contains item at intersection' => array(
59 array('/prefix/segment/', 'prefix_segment'),
60 array('/prefix/segment/bb', 'leading_segment'),
65 -> /prefix/segment prefix_segment
66 -> /prefix/segment/bb leading_segment
69 'Simple one level nesting' => array(
72 array('/group/segment/', 'nested_segment'),
73 array('/group/thing/', 'some_segment'),
74 array('/group/other/', 'other_segment'),
79 -> /group/segment nested_segment
80 -> /group/thing some_segment
81 -> /group/other other_segment
84 'Retain matching order with groups' => array(
86 array('/group/aa/', 'aa'),
87 array('/group/bb/', 'bb'),
88 array('/group/cc/', 'cc'),
90 array('/group/dd/', 'dd'),
91 array('/group/ee/', 'ee'),
92 array('/group/ff/', 'ff'),
106 'Retain complex matching order with groups at base' => array(
108 array('/aaa/111/', 'first_aaa'),
109 array('/prefixed/group/aa/', 'aa'),
110 array('/prefixed/group/bb/', 'bb'),
111 array('/prefixed/group/cc/', 'cc'),
112 array('/prefixed/', 'root'),
113 array('/prefixed/group/dd/', 'dd'),
114 array('/prefixed/group/ee/', 'ee'),
115 array('/prefixed/group/ff/', 'ff'),
116 array('/aaa/222/', 'second_aaa'),
117 array('/aaa/333/', 'third_aaa'),
121 -> /aaa/111 first_aaa
122 -> /aaa/222 second_aaa
123 -> /aaa/333 third_aaa
126 -> -> /prefixed/group/aa aa
127 -> -> /prefixed/group/bb bb
128 -> -> /prefixed/group/cc cc
131 -> -> /prefixed/group/dd dd
132 -> -> /prefixed/group/ee ee
133 -> -> /prefixed/group/ff ff
137 'Group regardless of segments' => array(
139 array('/aaa-111/', 'a1'),
140 array('/aaa-222/', 'a2'),
141 array('/aaa-333/', 'a3'),
142 array('/group-aa/', 'g1'),
143 array('/group-bb/', 'g2'),
144 array('/group-cc/', 'g3'),
160 private function dumpCollection(StaticPrefixCollection $collection, $prefix = '')
164 foreach ($collection->getItems() as $item) {
165 if ($item instanceof StaticPrefixCollection) {
166 $lines[] = $prefix.$item->getPrefix();
167 $lines[] = $this->dumpCollection($item, $prefix.'-> ');
169 $lines[] = $prefix.implode(' ', $item);
173 return implode("\n", $lines);