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\Cmf\Component\Routing\NestedMatcher\UrlMatcher;
17 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
18 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
20 class UrlMatcherTest extends CmfUnitTestCase
22 protected $routeDocument;
23 protected $routeCompiled;
28 protected $url = '/foo/bar';
30 public function setUp()
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');
35 $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
36 $this->request = Request::create($this->url);
38 $this->matcher = new UrlMatcher(new RouteCollection(), $this->context);
41 public function testMatchRouteKey()
43 $this->doTestMatchRouteKey($this->url);
46 public function testMatchNoKey()
48 $this->doTestMatchRouteKey(null);
51 public function doTestMatchRouteKey($routeKey)
53 $this->routeCompiled->expects($this->atLeastOnce())
54 ->method('getStaticPrefix')
55 ->will($this->returnValue($this->url))
57 $this->routeCompiled->expects($this->atLeastOnce())
59 ->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
61 $this->routeDocument->expects($this->atLeastOnce())
63 ->will($this->returnValue($this->routeCompiled))
65 $this->routeDocument->expects($this->atLeastOnce())
66 ->method('getRouteKey')
67 ->will($this->returnValue($routeKey))
69 $this->routeDocument->expects($this->atLeastOnce())
70 ->method('getDefaults')
71 ->will($this->returnValue(array('foo' => 'bar')))
74 $mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
75 $mockCompiled->expects($this->any())
76 ->method('getStaticPrefix')
77 ->will($this->returnValue('/no/match'))
79 $mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
80 $mockRoute->expects($this->any())
82 ->will($this->returnValue($mockCompiled))
84 $routeCollection = new RouteCollection();
85 $routeCollection->add('some', $mockRoute);
86 $routeCollection->add('_company_more', $this->routeDocument);
87 $routeCollection->add('other', $mockRoute);
89 $results = $this->matcher->finalMatch($routeCollection, $this->request);
92 RouteObjectInterface::ROUTE_NAME => ($routeKey) ? $routeKey : '_company_more',
93 RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
97 $this->assertEquals($expected, $results);
100 public function testMatchNoRouteObject()
102 $this->routeCompiled->expects($this->atLeastOnce())
103 ->method('getStaticPrefix')
104 ->will($this->returnValue($this->url))
106 $this->routeCompiled->expects($this->atLeastOnce())
108 ->will($this->returnValue('#'.str_replace('/', '\/', $this->url).'#'))
110 $this->routeDocument = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
111 $this->routeDocument->expects($this->atLeastOnce())
113 ->will($this->returnValue($this->routeCompiled))
115 $this->routeDocument->expects($this->never())
116 ->method('getRouteKey')
118 $this->routeDocument->expects($this->atLeastOnce())
119 ->method('getDefaults')
120 ->will($this->returnValue(array('foo' => 'bar')))
123 $mockCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
124 $mockCompiled->expects($this->any())
125 ->method('getStaticPrefix')
126 ->will($this->returnValue('/no/match'))
128 $mockRoute = $this->getMockBuilder('Symfony\Component\Routing\Route')->disableOriginalConstructor()->getMock();
129 $mockRoute->expects($this->any())
131 ->will($this->returnValue($mockCompiled))
133 $routeCollection = new RouteCollection();
134 $routeCollection->add('some', $mockRoute);
135 $routeCollection->add('_company_more', $this->routeDocument);
136 $routeCollection->add('other', $mockRoute);
138 $results = $this->matcher->finalMatch($routeCollection, $this->request);
141 RouteObjectInterface::ROUTE_NAME => '_company_more',
142 RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
146 $this->assertEquals($expected, $results);