|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Cmf\Component\Routing\Tests\NestedMatcher; |
| 4 | + |
| 5 | +use Symfony\Component\HttpFoundation\Request; |
| 6 | + |
| 7 | +use Symfony\Component\Routing\RouteCollection; |
| 8 | +use Symfony\Component\Routing\Route; |
| 9 | +use Symfony\Cmf\Component\Routing\NestedMatcher\ConfigurableUrlMatcher; |
| 10 | + |
| 11 | +use Symfony\Cmf\Component\Routing\RouteObjectInterface; |
| 12 | + |
| 13 | +use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase; |
| 14 | + |
| 15 | +class ConfigurableUrlMatcherTest extends CmfUnitTestCase |
| 16 | +{ |
| 17 | + protected $routeDocument; |
| 18 | + protected $routeCompiled; |
| 19 | + protected $matcher; |
| 20 | + protected $context; |
| 21 | + protected $request; |
| 22 | + |
| 23 | + protected $url = '/foo/bar'; |
| 24 | + |
| 25 | + public function setUp() |
| 26 | + { |
| 27 | + $this->routeDocument = $this->buildMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RouteMock', array('getDefaults', 'getRouteKey', 'compile')); |
| 28 | + $this->routeCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute'); |
| 29 | + |
| 30 | + $this->context = $this->buildMock('Symfony\\Component\\Routing\\RequestContext'); |
| 31 | + $this->request = Request::create($this->url); |
| 32 | + |
| 33 | + $this->matcher = new ConfigurableUrlMatcher(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testMatch() |
| 37 | + { |
| 38 | + $this->routeCompiled->expects($this->atLeastOnce()) |
| 39 | + ->method('getStaticPrefix') |
| 40 | + ->will($this->returnValue($this->url)) |
| 41 | + ; |
| 42 | + $this->routeCompiled->expects($this->atLeastOnce()) |
| 43 | + ->method('getRegex') |
| 44 | + ->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#')) |
| 45 | + ; |
| 46 | + $this->routeDocument->expects($this->atLeastOnce()) |
| 47 | + ->method('compile') |
| 48 | + ->will($this->returnValue($this->routeCompiled)) |
| 49 | + ; |
| 50 | + $this->routeDocument->expects($this->atLeastOnce()) |
| 51 | + ->method('getDefaults') |
| 52 | + ->will($this->returnValue(array('foo' => 'bar'))) |
| 53 | + ; |
| 54 | + |
| 55 | + $mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute'); |
| 56 | + $mockCompiled->expects($this->any()) |
| 57 | + ->method('getStaticPrefix') |
| 58 | + ->will($this->returnValue('/no/match')) |
| 59 | + ; |
| 60 | + $mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); |
| 61 | + $mockRoute->expects($this->any()) |
| 62 | + ->method('compile') |
| 63 | + ->will($this->returnValue($mockCompiled)) |
| 64 | + ; |
| 65 | + $routeCollection = new RouteCollection(); |
| 66 | + $routeCollection->add('some', $mockRoute); |
| 67 | + $routeCollection->add('_company_more', $this->routeDocument); |
| 68 | + $routeCollection->add('other', $mockRoute); |
| 69 | + |
| 70 | + $results = $this->matcher->finalMatch($routeCollection, $this->request); |
| 71 | + |
| 72 | + $expected = array( |
| 73 | + '_route_name' => '_company_more', |
| 74 | + '_route' => $this->routeDocument, |
| 75 | + 'foo' => 'bar', |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertEquals($expected, $results); |
| 79 | + } |
| 80 | + |
| 81 | + public function testMatchNoRouteObject() |
| 82 | + { |
| 83 | + $this->routeCompiled->expects($this->atLeastOnce()) |
| 84 | + ->method('getStaticPrefix') |
| 85 | + ->will($this->returnValue($this->url)) |
| 86 | + ; |
| 87 | + $this->routeCompiled->expects($this->atLeastOnce()) |
| 88 | + ->method('getRegex') |
| 89 | + ->will($this->returnValue('#'.str_replace('/', '\\/', $this->url).'#')) |
| 90 | + ; |
| 91 | + $this->routeDocument = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); |
| 92 | + $this->routeDocument->expects($this->atLeastOnce()) |
| 93 | + ->method('compile') |
| 94 | + ->will($this->returnValue($this->routeCompiled)) |
| 95 | + ; |
| 96 | + $this->routeDocument->expects($this->never()) |
| 97 | + ->method('getRouteKey') |
| 98 | + ; |
| 99 | + $this->routeDocument->expects($this->atLeastOnce()) |
| 100 | + ->method('getDefaults') |
| 101 | + ->will($this->returnValue(array('foo' => 'bar'))) |
| 102 | + ; |
| 103 | + |
| 104 | + $mockCompiled = $this->buildMock('Symfony\\Component\\Routing\\CompiledRoute'); |
| 105 | + $mockCompiled->expects($this->any()) |
| 106 | + ->method('getStaticPrefix') |
| 107 | + ->will($this->returnValue('/no/match')) |
| 108 | + ; |
| 109 | + $mockRoute = $this->getMockBuilder('Symfony\\Component\\Routing\\Route')->disableOriginalConstructor()->getMock(); |
| 110 | + $mockRoute->expects($this->any()) |
| 111 | + ->method('compile') |
| 112 | + ->will($this->returnValue($mockCompiled)) |
| 113 | + ; |
| 114 | + $routeCollection = new RouteCollection(); |
| 115 | + $routeCollection->add('some', $mockRoute); |
| 116 | + $routeCollection->add('_company_more', $this->routeDocument); |
| 117 | + $routeCollection->add('other', $mockRoute); |
| 118 | + |
| 119 | + $results = $this->matcher->finalMatch($routeCollection, $this->request); |
| 120 | + |
| 121 | + $expected = array( |
| 122 | + '_route_name' => '_company_more', |
| 123 | + '_route' => $this->routeDocument, |
| 124 | + 'foo' => 'bar', |
| 125 | + ); |
| 126 | + |
| 127 | + $this->assertEquals($expected, $results); |
| 128 | + } |
| 129 | +} |
0 commit comments