4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Tests\NestedMatcher;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\Routing\RouteCollection;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
18 use Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher;
19 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
21 class NestedMatcherTest extends CmfUnitTestCase
24 private $routeFilter1;
25 private $routeFilter2;
26 private $finalMatcher;
28 public function setUp()
30 $this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
31 $this->routeFilter1 = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface');
32 $this->routeFilter2 = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface');
33 $this->finalMatcher = $this->buildMock('Symfony\Cmf\Component\Routing\NestedMatcher\FinalMatcherInterface');
36 public function testNestedMatcher()
38 $request = Request::create('/path/one');
39 $routeCollection = new RouteCollection();
40 $route = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
41 $routeCollection->add('route', $route);
43 $this->provider->expects($this->once())
44 ->method('getRouteCollectionForRequest')
46 ->will($this->returnValue($routeCollection))
48 $this->routeFilter1->expects($this->once())
50 ->with($routeCollection, $request)
51 ->will($this->returnValue($routeCollection))
53 $this->routeFilter2->expects($this->once())
55 ->with($routeCollection, $request)
56 ->will($this->returnValue($routeCollection))
58 $this->finalMatcher->expects($this->once())
59 ->method('finalMatch')
60 ->with($routeCollection, $request)
61 ->will($this->returnValue(array('foo' => 'bar')))
64 $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
65 $matcher->addRouteFilter($this->routeFilter1);
66 $matcher->addRouteFilter($this->routeFilter2);
68 $attributes = $matcher->matchRequest($request);
70 $this->assertEquals(array('foo' => 'bar'), $attributes);
74 * Test priorities and exception handling.
76 public function testNestedMatcherPriority()
78 $request = Request::create('/path/one');
79 $routeCollection = new RouteCollection();
80 $route = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
81 $routeCollection->add('route', $route);
83 $wrongProvider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
84 $wrongProvider->expects($this->never())
85 ->method('getRouteCollectionForRequest')
87 $this->provider->expects($this->once())
88 ->method('getRouteCollectionForRequest')
90 ->will($this->returnValue($routeCollection))
92 $this->routeFilter1->expects($this->once())
94 ->with($routeCollection, $request)
95 ->will($this->throwException(new ResourceNotFoundException()))
97 $this->routeFilter2->expects($this->never())
100 $this->finalMatcher->expects($this->never())
101 ->method('finalMatch')
104 $matcher = new NestedMatcher($wrongProvider, $this->finalMatcher);
105 $matcher->setRouteProvider($this->provider);
106 $matcher->addRouteFilter($this->routeFilter2, 10);
107 $matcher->addRouteFilter($this->routeFilter1, 20);
110 $matcher->matchRequest($request);
111 fail('nested matcher is eating exception');
112 } catch (ResourceNotFoundException $e) {
117 public function testProviderNoMatch()
119 $request = Request::create('/path/one');
120 $routeCollection = new RouteCollection();
121 $this->provider->expects($this->once())
122 ->method('getRouteCollectionForRequest')
124 ->will($this->returnValue($routeCollection))
126 $this->finalMatcher->expects($this->never())
127 ->method('finalMatch')
130 $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
132 $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
133 $matcher->matchRequest($request);