6279b7de026e21346887f40bcc8854d0a6f5c30f
[yaffs-website] / vendor / symfony-cmf / routing / Tests / NestedMatcher / UrlMatcherTest.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\Cmf\Component\Routing\NestedMatcher\UrlMatcher;
17 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
18 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
19
20 class UrlMatcherTest extends CmfUnitTestCase
21 {
22     protected $routeDocument;
23     protected $routeCompiled;
24     protected $matcher;
25     protected $context;
26     protected $request;
27
28     protected $url = '/foo/bar';
29
30     public function setUp()
31     {
32         $this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'getRouteKey', 'compile'));
33         $this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
34
35         $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
36         $this->request = Request::create($this->url);
37
38         $this->matcher = new UrlMatcher(new RouteCollection(), $this->context);
39     }
40
41     public function testMatchRouteKey()
42     {
43         $this->doTestMatchRouteKey($this->url);
44     }
45
46     public function testMatchNoKey()
47     {
48         $this->doTestMatchRouteKey(null);
49     }
50
51     public function doTestMatchRouteKey($routeKey)
52     {
53         $this->routeCompiled->expects($this->atLeastOnce())
54             ->method('getStaticPrefix')
55             ->will($this->returnValue($this->url))
56         ;
57         $this->routeCompiled->expects($this->atLeastOnce())
58             ->method('getRegex')
59             ->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
60         ;
61         $this->routeDocument->expects($this->atLeastOnce())
62             ->method('compile')
63             ->will($this->returnValue($this->routeCompiled))
64         ;
65         $this->routeDocument->expects($this->atLeastOnce())
66             ->method('getRouteKey')
67             ->will($this->returnValue($routeKey))
68         ;
69         $this->routeDocument->expects($this->atLeastOnce())
70             ->method('getDefaults')
71             ->will($this->returnValue(array('foo' => 'bar')))
72         ;
73
74         $mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
75         $mockCompiled->expects($this->any())
76             ->method('getStaticPrefix')
77             ->will($this->returnValue('/no/match'))
78         ;
79         $mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
80         $mockRoute->expects($this->any())
81             ->method('compile')
82             ->will($this->returnValue($mockCompiled))
83         ;
84         $routeCollection = new RouteCollection();
85         $routeCollection->add('some', $mockRoute);
86         $routeCollection->add('_company_more', $this->routeDocument);
87         $routeCollection->add('other', $mockRoute);
88
89         $results = $this->matcher->finalMatch($routeCollection, $this->request);
90
91         $expected = array(
92             RouteObjectInterface::ROUTE_NAME => ($routeKey) ? $routeKey : '_company_more',
93             RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
94             'foo' => 'bar',
95         );
96
97         $this->assertEquals($expected, $results);
98     }
99
100     public function testMatchNoRouteObject()
101     {
102         $this->routeCompiled->expects($this->atLeastOnce())
103             ->method('getStaticPrefix')
104             ->will($this->returnValue($this->url))
105         ;
106         $this->routeCompiled->expects($this->atLeastOnce())
107             ->method('getRegex')
108             ->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
109         ;
110         $this->routeDocument = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
111         $this->routeDocument->expects($this->atLeastOnce())
112             ->method('compile')
113             ->will($this->returnValue($this->routeCompiled))
114         ;
115         $this->routeDocument->expects($this->never())
116             ->method('getRouteKey')
117         ;
118         $this->routeDocument->expects($this->atLeastOnce())
119             ->method('getDefaults')
120             ->will($this->returnValue(array('foo' => 'bar')))
121         ;
122
123         $mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
124         $mockCompiled->expects($this->any())
125             ->method('getStaticPrefix')
126             ->will($this->returnValue('/no/match'))
127         ;
128         $mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
129         $mockRoute->expects($this->any())
130             ->method('compile')
131             ->will($this->returnValue($mockCompiled))
132         ;
133         $routeCollection = new RouteCollection();
134         $routeCollection->add('some', $mockRoute);
135         $routeCollection->add('_company_more', $this->routeDocument);
136         $routeCollection->add('other', $mockRoute);
137
138         $results = $this->matcher->finalMatch($routeCollection, $this->request);
139
140         $expected = array(
141             RouteObjectInterface::ROUTE_NAME => '_company_more',
142             RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
143             'foo' => 'bar',
144         );
145
146         $this->assertEquals($expected, $results);
147     }
148 }