6f1b7dc981edeb3272355572bb417ee259e8db80
[yaffs-website] / vendor / symfony / routing / Tests / Matcher / Dumper / LegacyApacheMatcherDumperTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Routing\RouteCollection;
17 use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
18
19 /**
20  * @group legacy
21  */
22 class LegacyApacheMatcherDumperTest extends TestCase
23 {
24     protected static $fixturesPath;
25
26     public static function setUpBeforeClass()
27     {
28         self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
29     }
30
31     public function testDump()
32     {
33         $dumper = new ApacheMatcherDumper($this->getRouteCollection());
34
35         $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
36     }
37
38     /**
39      * @dataProvider provideEscapeFixtures
40      */
41     public function testEscapePattern($src, $dest, $char, $with, $message)
42     {
43         $r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
44         $r->setAccessible(true);
45         $this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
46     }
47
48     public function provideEscapeFixtures()
49     {
50         return array(
51             array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
52             array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
53             array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
54             array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
55             array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
56         );
57     }
58
59     public function testEscapeScriptName()
60     {
61         $collection = new RouteCollection();
62         $collection->add('foo', new Route('/foo'));
63         $dumper = new ApacheMatcherDumper($collection);
64         $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
65     }
66
67     private function getRouteCollection()
68     {
69         $collection = new RouteCollection();
70
71         // defaults and requirements
72         $collection->add('foo', new Route(
73             '/foo/{bar}',
74             array('def' => 'test'),
75             array('bar' => 'baz|symfony')
76         ));
77         // defaults parameters in pattern
78         $collection->add('foobar', new Route(
79             '/foo/{bar}',
80             array('bar' => 'toto')
81         ));
82         // method requirement
83         $collection->add('bar', new Route(
84             '/bar/{foo}',
85             array(),
86             array(),
87             array(),
88             '',
89             array(),
90             array('GET', 'head')
91         ));
92         // method requirement (again)
93         $collection->add('baragain', new Route(
94             '/baragain/{foo}',
95             array(),
96             array(),
97             array(),
98             '',
99             array(),
100             array('get', 'post')
101         ));
102         // simple
103         $collection->add('baz', new Route(
104             '/test/baz'
105         ));
106         // simple with extension
107         $collection->add('baz2', new Route(
108             '/test/baz.html'
109         ));
110         // trailing slash
111         $collection->add('baz3', new Route(
112             '/test/baz3/'
113         ));
114         // trailing slash with variable
115         $collection->add('baz4', new Route(
116             '/test/{foo}/'
117         ));
118         // trailing slash and safe method
119         $collection->add('baz5', new Route(
120             '/test/{foo}/',
121             array(),
122             array(),
123             array(),
124             '',
125             array(),
126             array('GET')
127         ));
128         // trailing slash and unsafe method
129         $collection->add('baz5unsafe', new Route(
130             '/testunsafe/{foo}/',
131             array(),
132             array(),
133             array(),
134             '',
135             array(),
136             array('post')
137         ));
138         // complex
139         $collection->add('baz6', new Route(
140             '/test/baz',
141             array('foo' => 'bar baz')
142         ));
143         // space in path
144         $collection->add('baz7', new Route(
145             '/te st/baz'
146         ));
147         // space preceded with \ in path
148         $collection->add('baz8', new Route(
149             '/te\\ st/baz'
150         ));
151         // space preceded with \ in requirement
152         $collection->add('baz9', new Route(
153             '/test/{baz}',
154             array(),
155             array(
156                 'baz' => 'te\\\\ st',
157             )
158         ));
159
160         $collection1 = new RouteCollection();
161
162         $route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
163         $collection1->add('route1', $route1);
164
165         $collection2 = new RouteCollection();
166
167         $route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
168         $collection2->add('route2', $route2);
169
170         $route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
171         $collection2->add('route3', $route3);
172
173         $collection2->addPrefix('/c2');
174         $collection1->addCollection($collection2);
175
176         $route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
177         $collection1->add('route4', $route4);
178
179         $route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
180         $collection1->add('route5', $route5);
181
182         $route6 = new Route('/route6', array(), array(), array(), null);
183         $collection1->add('route6', $route6);
184
185         $collection->addCollection($collection1);
186
187         // host and variables
188
189         $collection1 = new RouteCollection();
190
191         $route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
192         $collection1->add('route11', $route11);
193
194         $route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
195         $collection1->add('route12', $route12);
196
197         $route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
198         $collection1->add('route13', $route13);
199
200         $route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
201         $collection1->add('route14', $route14);
202
203         $route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
204         $collection1->add('route15', $route15);
205
206         $route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
207         $collection1->add('route16', $route16);
208
209         $route17 = new Route('/route17', array(), array(), array(), null);
210         $collection1->add('route17', $route17);
211
212         $collection->addCollection($collection1);
213
214         return $collection;
215     }
216 }