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 Symfony\Component\Routing\Router
;
16 class RouterTest
extends \PHPUnit_Framework_TestCase
18 private $router = null;
20 private $loader = null;
22 protected function setUp()
24 $this->loader
= $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
25 $this->router
= new Router($this->loader
, 'routing.yml');
28 public function testSetOptionsWithSupportedOptions()
30 $this->router
->setOptions(array(
31 'cache_dir' => './cache',
33 'resource_type' => 'ResourceType'
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'));
42 * @expectedException \InvalidArgumentException
43 * @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
45 public function testSetOptionsWithUnsupportedOptions()
47 $this->router
->setOptions(array(
48 'cache_dir' => './cache',
50 'option_bar' => 'baz',
51 'resource_type' => 'ResourceType'
55 public function testSetOptionWithSupportedOption()
57 $this->router
->setOption('cache_dir', './cache');
59 $this->assertSame('./cache', $this->router
->getOption('cache_dir'));
63 * @expectedException \InvalidArgumentException
64 * @expectedExceptionMessage The Router does not support the "option_foo" option
66 public function testSetOptionWithUnsupportedOption()
68 $this->router
->setOption('option_foo', true);
72 * @expectedException \InvalidArgumentException
73 * @expectedExceptionMessage The Router does not support the "option_foo" option
75 public function testGetOptionWithUnsupportedOption()
77 $this->router
->getOption('option_foo', true);
80 public function testThatRouteCollectionIsLoaded()
82 $this->router
->setOption('resource_type', 'ResourceType');
84 $routeCollection = $this->getMock('Symfony\Component\Routing\RouteCollection');
86 $this->loader
->expects($this->once())
87 ->method('load')->with('routing.yml', 'ResourceType')
88 ->will($this->returnValue($routeCollection));
90 $this->assertSame($routeCollection, $this->router
->getRouteCollection());
94 * @dataProvider provideMatcherOptionsPreventingCaching
96 public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
98 $this->router
->setOption($option, null);
100 $this->loader
->expects($this->once())
101 ->method('load')->with('routing.yml', null)
102 ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
104 $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router
->getMatcher());
108 public function provideMatcherOptionsPreventingCaching()
112 array('matcher_cache_class')
117 * @dataProvider provideGeneratorOptionsPreventingCaching
119 public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
121 $this->router
->setOption($option, null);
123 $this->loader
->expects($this->once())
124 ->method('load')->with('routing.yml', null)
125 ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
127 $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router
->getGenerator());
131 public function provideGeneratorOptionsPreventingCaching()
135 array('generator_cache_class')