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\Routing\Router;
16 use Symfony\Component\HttpFoundation\Request;
18 class RouterTest extends TestCase
20 private $router = null;
22 private $loader = null;
24 protected function setUp()
26 $this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
27 $this->router = new Router($this->loader, 'routing.yml');
30 public function testSetOptionsWithSupportedOptions()
32 $this->router->setOptions(array(
33 'cache_dir' => './cache',
35 'resource_type' => 'ResourceType',
38 $this->assertSame('./cache', $this->router->getOption('cache_dir'));
39 $this->assertTrue($this->router->getOption('debug'));
40 $this->assertSame('ResourceType', $this->router->getOption('resource_type'));
44 * @expectedException \InvalidArgumentException
45 * @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
47 public function testSetOptionsWithUnsupportedOptions()
49 $this->router->setOptions(array(
50 'cache_dir' => './cache',
52 'option_bar' => 'baz',
53 'resource_type' => 'ResourceType',
57 public function testSetOptionWithSupportedOption()
59 $this->router->setOption('cache_dir', './cache');
61 $this->assertSame('./cache', $this->router->getOption('cache_dir'));
65 * @expectedException \InvalidArgumentException
66 * @expectedExceptionMessage The Router does not support the "option_foo" option
68 public function testSetOptionWithUnsupportedOption()
70 $this->router->setOption('option_foo', true);
74 * @expectedException \InvalidArgumentException
75 * @expectedExceptionMessage The Router does not support the "option_foo" option
77 public function testGetOptionWithUnsupportedOption()
79 $this->router->getOption('option_foo', true);
82 public function testThatRouteCollectionIsLoaded()
84 $this->router->setOption('resource_type', 'ResourceType');
86 $routeCollection = $this->getMockBuilder('Symfony\Component\Routing\RouteCollection')->getMock();
88 $this->loader->expects($this->once())
89 ->method('load')->with('routing.yml', 'ResourceType')
90 ->will($this->returnValue($routeCollection));
92 $this->assertSame($routeCollection, $this->router->getRouteCollection());
96 * @dataProvider provideMatcherOptionsPreventingCaching
98 public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
100 $this->router->setOption($option, null);
102 $this->loader->expects($this->once())
103 ->method('load')->with('routing.yml', null)
104 ->will($this->returnValue($this->getMockBuilder('Symfony\Component\Routing\RouteCollection')->getMock()));
106 $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
109 public function provideMatcherOptionsPreventingCaching()
113 array('matcher_cache_class'),
118 * @dataProvider provideGeneratorOptionsPreventingCaching
120 public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
122 $this->router->setOption($option, null);
124 $this->loader->expects($this->once())
125 ->method('load')->with('routing.yml', null)
126 ->will($this->returnValue($this->getMockBuilder('Symfony\Component\Routing\RouteCollection')->getMock()));
128 $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
131 public function provideGeneratorOptionsPreventingCaching()
135 array('generator_cache_class'),
139 public function testMatchRequestWithUrlMatcherInterface()
141 $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
142 $matcher->expects($this->once())->method('match');
144 $p = new \ReflectionProperty($this->router, 'matcher');
145 $p->setAccessible(true);
146 $p->setValue($this->router, $matcher);
148 $this->router->matchRequest(Request::create('/'));
151 public function testMatchRequestWithRequestMatcherInterface()
153 $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
154 $matcher->expects($this->once())->method('matchRequest');
156 $p = new \ReflectionProperty($this->router, 'matcher');
157 $p->setAccessible(true);
158 $p->setValue($this->router, $matcher);
160 $this->router->matchRequest(Request::create('/'));