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