4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\Routing\RouteCollection;
17 use Symfony\Component\Routing\Router;
19 class RouterTest extends TestCase
21 private $router = null;
23 private $loader = null;
25 protected function setUp()
27 $this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
28 $this->router = new Router($this->loader, 'routing.yml');
31 public function testSetOptionsWithSupportedOptions()
33 $this->router->setOptions(array(
34 'cache_dir' => './cache',
36 'resource_type' => 'ResourceType',
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'));
45 * @expectedException \InvalidArgumentException
46 * @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
48 public function testSetOptionsWithUnsupportedOptions()
50 $this->router->setOptions(array(
51 'cache_dir' => './cache',
53 'option_bar' => 'baz',
54 'resource_type' => 'ResourceType',
58 public function testSetOptionWithSupportedOption()
60 $this->router->setOption('cache_dir', './cache');
62 $this->assertSame('./cache', $this->router->getOption('cache_dir'));
66 * @expectedException \InvalidArgumentException
67 * @expectedExceptionMessage The Router does not support the "option_foo" option
69 public function testSetOptionWithUnsupportedOption()
71 $this->router->setOption('option_foo', true);
75 * @expectedException \InvalidArgumentException
76 * @expectedExceptionMessage The Router does not support the "option_foo" option
78 public function testGetOptionWithUnsupportedOption()
80 $this->router->getOption('option_foo', true);
83 public function testThatRouteCollectionIsLoaded()
85 $this->router->setOption('resource_type', 'ResourceType');
87 $routeCollection = new RouteCollection();
89 $this->loader->expects($this->once())
90 ->method('load')->with('routing.yml', 'ResourceType')
91 ->will($this->returnValue($routeCollection));
93 $this->assertSame($routeCollection, $this->router->getRouteCollection());
97 * @dataProvider provideMatcherOptionsPreventingCaching
99 public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
101 $this->router->setOption($option, null);
103 $this->loader->expects($this->once())
104 ->method('load')->with('routing.yml', null)
105 ->will($this->returnValue(new RouteCollection()));
107 $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
110 public function provideMatcherOptionsPreventingCaching()
114 array('matcher_cache_class'),
119 * @dataProvider provideGeneratorOptionsPreventingCaching
121 public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
123 $this->router->setOption($option, null);
125 $this->loader->expects($this->once())
126 ->method('load')->with('routing.yml', null)
127 ->will($this->returnValue(new RouteCollection()));
129 $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
132 public function provideGeneratorOptionsPreventingCaching()
136 array('generator_cache_class'),
140 public function testMatchRequestWithUrlMatcherInterface()
142 $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
143 $matcher->expects($this->once())->method('match');
145 $p = new \ReflectionProperty($this->router, 'matcher');
146 $p->setAccessible(true);
147 $p->setValue($this->router, $matcher);
149 $this->router->matchRequest(Request::create('/'));
152 public function testMatchRequestWithRequestMatcherInterface()
154 $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
155 $matcher->expects($this->once())->method('matchRequest');
157 $p = new \ReflectionProperty($this->router, 'matcher');
158 $p->setAccessible(true);
159 $p->setValue($this->router, $matcher);
161 $this->router->matchRequest(Request::create('/'));