]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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 | ||
16 | class RouterTest extends \PHPUnit_Framework_TestCase | |
17 | { | |
18 | private $router = null; | |
19 | ||
20 | private $loader = null; | |
21 | ||
22 | protected function setUp() | |
23 | { | |
24 | $this->loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface'); | |
25 | $this->router = new Router($this->loader, 'routing.yml'); | |
26 | } | |
27 | ||
28 | public function testSetOptionsWithSupportedOptions() | |
29 | { | |
30 | $this->router->setOptions(array( | |
31 | 'cache_dir' => './cache', | |
32 | 'debug' => true, | |
33 | 'resource_type' => 'ResourceType' | |
34 | )); | |
35 | ||
36 | $this->assertSame('./cache', $this->router->getOption('cache_dir')); | |
37 | $this->assertTrue($this->router->getOption('debug')); | |
38 | $this->assertSame('ResourceType', $this->router->getOption('resource_type')); | |
39 | } | |
40 | ||
41 | /** | |
42 | * @expectedException \InvalidArgumentException | |
43 | * @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar" | |
44 | */ | |
45 | public function testSetOptionsWithUnsupportedOptions() | |
46 | { | |
47 | $this->router->setOptions(array( | |
48 | 'cache_dir' => './cache', | |
49 | 'option_foo' => true, | |
50 | 'option_bar' => 'baz', | |
51 | 'resource_type' => 'ResourceType' | |
52 | )); | |
53 | } | |
54 | ||
55 | public function testSetOptionWithSupportedOption() | |
56 | { | |
57 | $this->router->setOption('cache_dir', './cache'); | |
58 | ||
59 | $this->assertSame('./cache', $this->router->getOption('cache_dir')); | |
60 | } | |
61 | ||
62 | /** | |
63 | * @expectedException \InvalidArgumentException | |
64 | * @expectedExceptionMessage The Router does not support the "option_foo" option | |
65 | */ | |
66 | public function testSetOptionWithUnsupportedOption() | |
67 | { | |
68 | $this->router->setOption('option_foo', true); | |
69 | } | |
70 | ||
71 | /** | |
72 | * @expectedException \InvalidArgumentException | |
73 | * @expectedExceptionMessage The Router does not support the "option_foo" option | |
74 | */ | |
75 | public function testGetOptionWithUnsupportedOption() | |
76 | { | |
77 | $this->router->getOption('option_foo', true); | |
78 | } | |
79 | ||
80 | public function testThatRouteCollectionIsLoaded() | |
81 | { | |
82 | $this->router->setOption('resource_type', 'ResourceType'); | |
83 | ||
84 | $routeCollection = $this->getMock('Symfony\Component\Routing\RouteCollection'); | |
85 | ||
86 | $this->loader->expects($this->once()) | |
87 | ->method('load')->with('routing.yml', 'ResourceType') | |
88 | ->will($this->returnValue($routeCollection)); | |
89 | ||
90 | $this->assertSame($routeCollection, $this->router->getRouteCollection()); | |
91 | } | |
92 | ||
93 | /** | |
94 | * @dataProvider provideMatcherOptionsPreventingCaching | |
95 | */ | |
96 | public function testMatcherIsCreatedIfCacheIsNotConfigured($option) | |
97 | { | |
98 | $this->router->setOption($option, null); | |
99 | ||
100 | $this->loader->expects($this->once()) | |
101 | ->method('load')->with('routing.yml', null) | |
102 | ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection'))); | |
103 | ||
104 | $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher()); | |
105 | ||
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 | ||
131 | public function provideGeneratorOptionsPreventingCaching() | |
132 | { | |
133 | return array( | |
134 | array('cache_dir'), | |
135 | array('generator_cache_class') | |
136 | ); | |
137 | } | |
138 | } |