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