147385c40a9db2350bdb2bfa0ad3c574ddb3d741
[yaffs-website] / vendor / symfony-cmf / routing / Tests / NestedMatcher / NestedMatcherTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing\Tests\NestedMatcher;
13
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;
20
21 class NestedMatcherTest extends CmfUnitTestCase
22 {
23     private $provider;
24     private $routeFilter1;
25     private $routeFilter2;
26     private $finalMatcher;
27
28     public function setUp()
29     {
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');
34     }
35
36     public function testNestedMatcher()
37     {
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);
42
43         $this->provider->expects($this->once())
44             ->method('getRouteCollectionForRequest')
45             ->with($request)
46             ->will($this->returnValue($routeCollection))
47         ;
48         $this->routeFilter1->expects($this->once())
49             ->method('filter')
50             ->with($routeCollection, $request)
51             ->will($this->returnValue($routeCollection))
52         ;
53         $this->routeFilter2->expects($this->once())
54             ->method('filter')
55             ->with($routeCollection, $request)
56             ->will($this->returnValue($routeCollection))
57         ;
58         $this->finalMatcher->expects($this->once())
59             ->method('finalMatch')
60             ->with($routeCollection, $request)
61             ->will($this->returnValue(array('foo' => 'bar')))
62         ;
63
64         $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
65         $matcher->addRouteFilter($this->routeFilter1);
66         $matcher->addRouteFilter($this->routeFilter2);
67
68         $attributes = $matcher->matchRequest($request);
69
70         $this->assertEquals(array('foo' => 'bar'), $attributes);
71     }
72
73     /**
74      * Test priorities and exception handling.
75      */
76     public function testNestedMatcherPriority()
77     {
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);
82
83         $wrongProvider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
84         $wrongProvider->expects($this->never())
85             ->method('getRouteCollectionForRequest')
86         ;
87         $this->provider->expects($this->once())
88             ->method('getRouteCollectionForRequest')
89             ->with($request)
90             ->will($this->returnValue($routeCollection))
91         ;
92         $this->routeFilter1->expects($this->once())
93             ->method('filter')
94             ->with($routeCollection, $request)
95             ->will($this->throwException(new ResourceNotFoundException()))
96         ;
97         $this->routeFilter2->expects($this->never())
98             ->method('filter')
99         ;
100         $this->finalMatcher->expects($this->never())
101             ->method('finalMatch')
102         ;
103
104         $matcher = new NestedMatcher($wrongProvider, $this->finalMatcher);
105         $matcher->setRouteProvider($this->provider);
106         $matcher->addRouteFilter($this->routeFilter2, 10);
107         $matcher->addRouteFilter($this->routeFilter1, 20);
108
109         try {
110             $matcher->matchRequest($request);
111             fail('nested matcher is eating exception');
112         } catch (ResourceNotFoundException $e) {
113             // expected
114         }
115     }
116
117     public function testProviderNoMatch()
118     {
119         $request = Request::create('/path/one');
120         $routeCollection = new RouteCollection();
121         $this->provider->expects($this->once())
122             ->method('getRouteCollectionForRequest')
123             ->with($request)
124             ->will($this->returnValue($routeCollection))
125         ;
126         $this->finalMatcher->expects($this->never())
127             ->method('finalMatch')
128         ;
129
130         $matcher = new NestedMatcher($this->provider, $this->finalMatcher);
131
132         $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
133         $matcher->matchRequest($request);
134     }
135 }