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