Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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;
13
 
14
use Symfony\Component\Routing\Route;
15
 
16
class RouteCompilerTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @dataProvider provideCompileData
20
     */
21
    public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens)
22
    {
23
        $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
24
        $route = $r->newInstanceArgs($arguments);
25
 
26
        $compiled = $route->compile();
27
        $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
28
        $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
29
        $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
30
        $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
31
    }
32
 
33
    public function provideCompileData()
34
    {
35
        return array(
36
            array(
37
                'Static route',
38
                array('/foo'),
39
                '/foo', '#^/foo$#s', array(), array(
40
                    array('text', '/foo'),
41
                ),
42
            ),
43
 
44
            array(
45
                'Route with a variable',
46
                array('/foo/{bar}'),
47
                '/foo', '#^/foo/(?P<bar>[^/]++)$#s', array('bar'), array(
48
                    array('variable', '/', '[^/]++', 'bar'),
49
                    array('text', '/foo'),
50
                ),
51
            ),
52
 
53
            array(
54
                'Route with a variable that has a default value',
55
                array('/foo/{bar}', array('bar' => 'bar')),
56
                '/foo', '#^/foo(?:/(?P<bar>[^/]++))?$#s', array('bar'), array(
57
                    array('variable', '/', '[^/]++', 'bar'),
58
                    array('text', '/foo'),
59
                ),
60
            ),
61
 
62
            array(
63
                'Route with several variables',
64
                array('/foo/{bar}/{foobar}'),
65
                '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
66
                    array('variable', '/', '[^/]++', 'foobar'),
67
                    array('variable', '/', '[^/]++', 'bar'),
68
                    array('text', '/foo'),
69
                ),
70
            ),
71
 
72
            array(
73
                'Route with several variables that have default values',
74
                array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
75
                '/foo', '#^/foo(?:/(?P<bar>[^/]++)(?:/(?P<foobar>[^/]++))?)?$#s', array('bar', 'foobar'), array(
76
                    array('variable', '/', '[^/]++', 'foobar'),
77
                    array('variable', '/', '[^/]++', 'bar'),
78
                    array('text', '/foo'),
79
                ),
80
            ),
81
 
82
            array(
83
                'Route with several variables but some of them have no default values',
84
                array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
85
                '/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
86
                    array('variable', '/', '[^/]++', 'foobar'),
87
                    array('variable', '/', '[^/]++', 'bar'),
88
                    array('text', '/foo'),
89
                ),
90
            ),
91
 
92
            array(
93
                'Route with an optional variable as the first segment',
94
                array('/{bar}', array('bar' => 'bar')),
95
                '', '#^/(?P<bar>[^/]++)?$#s', array('bar'), array(
96
                    array('variable', '/', '[^/]++', 'bar'),
97
                ),
98
            ),
99
 
100
            array(
101
                'Route with a requirement of 0',
102
                array('/{bar}', array('bar' => null), array('bar' => '0')),
103
                '', '#^/(?P<bar>0)?$#s', array('bar'), array(
104
                    array('variable', '/', '0', 'bar'),
105
                ),
106
            ),
107
 
108
            array(
109
                'Route with an optional variable as the first segment with requirements',
110
                array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
111
                '', '#^/(?P<bar>(foo|bar))?$#s', array('bar'), array(
112
                    array('variable', '/', '(foo|bar)', 'bar'),
113
                ),
114
            ),
115
 
116
            array(
117
                'Route with only optional variables',
118
                array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')),
119
                '', '#^/(?P<foo>[^/]++)?(?:/(?P<bar>[^/]++))?$#s', array('foo', 'bar'), array(
120
                    array('variable', '/', '[^/]++', 'bar'),
121
                    array('variable', '/', '[^/]++', 'foo'),
122
                ),
123
            ),
124
 
125
            array(
126
                'Route with a variable in last position',
127
                array('/foo-{bar}'),
128
                '/foo', '#^/foo\-(?P<bar>[^/]++)$#s', array('bar'), array(
129
                    array('variable', '-', '[^/]++', 'bar'),
130
                    array('text', '/foo'),
131
                ),
132
            ),
133
 
134
            array(
135
                'Route with nested placeholders',
136
                array('/{static{var}static}'),
137
                '/{static', '#^/\{static(?P<var>[^/]+)static\}$#s', array('var'), array(
138
                    array('text', 'static}'),
139
                    array('variable', '', '[^/]+', 'var'),
140
                    array('text', '/{static'),
141
                ),
142
            ),
143
 
144
            array(
145
                'Route without separator between variables',
146
                array('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '(y|Y)')),
147
                '', '#^/(?P<w>[^/\.]+)(?P<x>[^/\.]+)(?P<y>(y|Y))(?:(?P<z>[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#s', array('w', 'x', 'y', 'z', '_format'), array(
148
                    array('variable', '.', '[^/]++', '_format'),
149
                    array('variable', '', '[^/\.]++', 'z'),
150
                    array('variable', '', '(y|Y)', 'y'),
151
                    array('variable', '', '[^/\.]+', 'x'),
152
                    array('variable', '/', '[^/\.]+', 'w'),
153
                ),
154
            ),
155
 
156
            array(
157
                'Route with a format',
158
                array('/foo/{bar}.{_format}'),
159
                '/foo', '#^/foo/(?P<bar>[^/\.]++)\.(?P<_format>[^/]++)$#s', array('bar', '_format'), array(
160
                    array('variable', '.', '[^/]++', '_format'),
161
                    array('variable', '/', '[^/\.]++', 'bar'),
162
                    array('text', '/foo'),
163
                ),
164
            ),
165
        );
166
    }
167
 
168
    /**
169
     * @expectedException \LogicException
170
     */
171
    public function testRouteWithSameVariableTwice()
172
    {
173
        $route = new Route('/{name}/{name}');
174
 
175
        $compiled = $route->compile();
176
    }
177
 
178
    /**
179
     * @dataProvider getNumericVariableNames
180
     * @expectedException \DomainException
181
     */
182
    public function testRouteWithNumericVariableName($name)
183
    {
184
        $route = new Route('/{'.$name.'}');
185
        $route->compile();
186
    }
187
 
188
    public function getNumericVariableNames()
189
    {
190
        return array(
191
           array('09'),
192
           array('123'),
193
           array('1e2'),
194
        );
195
    }
196
 
197
    /**
198
     * @dataProvider provideCompileWithHostData
199
     */
200
    public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
201
    {
202
        $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
203
        $route = $r->newInstanceArgs($arguments);
204
 
205
        $compiled = $route->compile();
206
        $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
207
        $this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)');
208
        $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
209
        $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
210
        $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
211
        $this->assertEquals($hostRegex, str_replace(array("\n", ' '), '', $compiled->getHostRegex()), $name.' (host regex)');
212
        $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
213
        $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
214
    }
215
 
216
    public function provideCompileWithHostData()
217
    {
218
        return array(
219
            array(
220
                'Route with host pattern',
221
                array('/hello', array(), array(), array(), 'www.example.com'),
222
                '/hello', '#^/hello$#s', array(), array(), array(
223
                    array('text', '/hello'),
224
                ),
225
                '#^www\.example\.com$#si', array(), array(
226
                    array('text', 'www.example.com'),
227
                ),
228
            ),
229
            array(
230
                'Route with host pattern and some variables',
231
                array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'),
232
                '/hello', '#^/hello/(?P<name>[^/]++)$#s', array('tld', 'name'), array('name'), array(
233
                    array('variable', '/', '[^/]++', 'name'),
234
                    array('text', '/hello'),
235
                ),
236
                '#^www\.example\.(?P<tld>[^\.]++)$#si', array('tld'), array(
237
                    array('variable', '.', '[^\.]++', 'tld'),
238
                    array('text', 'www.example'),
239
                ),
240
            ),
241
            array(
242
                'Route with variable at beginning of host',
243
                array('/hello', array(), array(), array(), '{locale}.example.{tld}'),
244
                '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
245
                    array('text', '/hello'),
246
                ),
247
                '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
248
                    array('variable', '.', '[^\.]++', 'tld'),
249
                    array('text', '.example'),
250
                    array('variable', '', '[^\.]++', 'locale'),
251
                ),
252
            ),
253
            array(
254
                'Route with host variables that has a default value',
255
                array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'),
256
                '/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
257
                    array('text', '/hello'),
258
                ),
259
                '#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
260
                    array('variable', '.', '[^\.]++', 'tld'),
261
                    array('text', '.example'),
262
                    array('variable', '', '[^\.]++', 'locale'),
263
                ),
264
            ),
265
        );
266
    }
267
}