| 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\Router;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
16 |
|
|
|
17 |
class RouterTest extends \PHPUnit_Framework_TestCase
|
|
|
18 |
{
|
|
|
19 |
private $router = null;
|
|
|
20 |
|
|
|
21 |
private $loader = null;
|
|
|
22 |
|
|
|
23 |
protected function setUp()
|
|
|
24 |
{
|
|
|
25 |
$this->loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
|
|
|
26 |
$this->router = new Router($this->loader, 'routing.yml');
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function testSetOptionsWithSupportedOptions()
|
|
|
30 |
{
|
|
|
31 |
$this->router->setOptions(array(
|
|
|
32 |
'cache_dir' => './cache',
|
|
|
33 |
'debug' => true,
|
|
|
34 |
'resource_type' => 'ResourceType',
|
|
|
35 |
));
|
|
|
36 |
|
|
|
37 |
$this->assertSame('./cache', $this->router->getOption('cache_dir'));
|
|
|
38 |
$this->assertTrue($this->router->getOption('debug'));
|
|
|
39 |
$this->assertSame('ResourceType', $this->router->getOption('resource_type'));
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @expectedException \InvalidArgumentException
|
|
|
44 |
* @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
|
|
|
45 |
*/
|
|
|
46 |
public function testSetOptionsWithUnsupportedOptions()
|
|
|
47 |
{
|
|
|
48 |
$this->router->setOptions(array(
|
|
|
49 |
'cache_dir' => './cache',
|
|
|
50 |
'option_foo' => true,
|
|
|
51 |
'option_bar' => 'baz',
|
|
|
52 |
'resource_type' => 'ResourceType',
|
|
|
53 |
));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function testSetOptionWithSupportedOption()
|
|
|
57 |
{
|
|
|
58 |
$this->router->setOption('cache_dir', './cache');
|
|
|
59 |
|
|
|
60 |
$this->assertSame('./cache', $this->router->getOption('cache_dir'));
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* @expectedException \InvalidArgumentException
|
|
|
65 |
* @expectedExceptionMessage The Router does not support the "option_foo" option
|
|
|
66 |
*/
|
|
|
67 |
public function testSetOptionWithUnsupportedOption()
|
|
|
68 |
{
|
|
|
69 |
$this->router->setOption('option_foo', true);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @expectedException \InvalidArgumentException
|
|
|
74 |
* @expectedExceptionMessage The Router does not support the "option_foo" option
|
|
|
75 |
*/
|
|
|
76 |
public function testGetOptionWithUnsupportedOption()
|
|
|
77 |
{
|
|
|
78 |
$this->router->getOption('option_foo', true);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function testThatRouteCollectionIsLoaded()
|
|
|
82 |
{
|
|
|
83 |
$this->router->setOption('resource_type', 'ResourceType');
|
|
|
84 |
|
|
|
85 |
$routeCollection = $this->getMock('Symfony\Component\Routing\RouteCollection');
|
|
|
86 |
|
|
|
87 |
$this->loader->expects($this->once())
|
|
|
88 |
->method('load')->with('routing.yml', 'ResourceType')
|
|
|
89 |
->will($this->returnValue($routeCollection));
|
|
|
90 |
|
|
|
91 |
$this->assertSame($routeCollection, $this->router->getRouteCollection());
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @dataProvider provideMatcherOptionsPreventingCaching
|
|
|
96 |
*/
|
|
|
97 |
public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
|
|
|
98 |
{
|
|
|
99 |
$this->router->setOption($option, null);
|
|
|
100 |
|
|
|
101 |
$this->loader->expects($this->once())
|
|
|
102 |
->method('load')->with('routing.yml', null)
|
|
|
103 |
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
|
|
|
104 |
|
|
|
105 |
$this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public function provideMatcherOptionsPreventingCaching()
|
|
|
109 |
{
|
|
|
110 |
return array(
|
|
|
111 |
array('cache_dir'),
|
|
|
112 |
array('matcher_cache_class'),
|
|
|
113 |
);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* @dataProvider provideGeneratorOptionsPreventingCaching
|
|
|
118 |
*/
|
|
|
119 |
public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
|
|
|
120 |
{
|
|
|
121 |
$this->router->setOption($option, null);
|
|
|
122 |
|
|
|
123 |
$this->loader->expects($this->once())
|
|
|
124 |
->method('load')->with('routing.yml', null)
|
|
|
125 |
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
|
|
|
126 |
|
|
|
127 |
$this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public function provideGeneratorOptionsPreventingCaching()
|
|
|
131 |
{
|
|
|
132 |
return array(
|
|
|
133 |
array('cache_dir'),
|
|
|
134 |
array('generator_cache_class'),
|
|
|
135 |
);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function testMatchRequestWithUrlMatcherInterface()
|
|
|
139 |
{
|
|
|
140 |
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
|
|
|
141 |
$matcher->expects($this->once())->method('match');
|
|
|
142 |
|
|
|
143 |
$p = new \ReflectionProperty($this->router, 'matcher');
|
|
|
144 |
$p->setAccessible(true);
|
|
|
145 |
$p->setValue($this->router, $matcher);
|
|
|
146 |
|
|
|
147 |
$this->router->matchRequest(Request::create('/'));
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public function testMatchRequestWithRequestMatcherInterface()
|
|
|
151 |
{
|
|
|
152 |
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
|
|
|
153 |
$matcher->expects($this->once())->method('matchRequest');
|
|
|
154 |
|
|
|
155 |
$p = new \ReflectionProperty($this->router, 'matcher');
|
|
|
156 |
$p->setAccessible(true);
|
|
|
157 |
$p->setValue($this->router, $matcher);
|
|
|
158 |
|
|
|
159 |
$this->router->matchRequest(Request::create('/'));
|
|
|
160 |
}
|
|
|
161 |
}
|