Skip to content

Commit 474601a

Browse files
committed
Fix some router bugs. Close #333.
1 parent 8a62339 commit 474601a

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

Routing/Router.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public function setContext(RequestContext $context)
4545
*/
4646
public function getContext()
4747
{
48-
$this->router->getContext();
48+
return $this->router->getContext();
4949
}
5050

5151
/**
5252
* {@inheritdoc}
5353
*/
5454
public function getRouteCollection()
5555
{
56-
$this->router->getRouteCollection();
56+
return $this->router->getRouteCollection();
5757
}
5858

5959
/*
@@ -67,6 +67,7 @@ public function match($pathInfo)
6767
$request = Request::create($pathInfo);
6868
$context = (new RequestContext())->fromRequest($request);
6969
$context->setPathInfo($pathInfo);
70+
$context->setScheme($baseContext->getScheme());
7071

7172
try {
7273
$this->router->setContext($context);

Tests/Routing/RouterTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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 Dunglas\ApiBundle\Tests\Routing;
13+
14+
use Dunglas\ApiBundle\Routing\Router;
15+
use Prophecy\Argument;
16+
use Symfony\Component\Routing\RequestContext;
17+
use Symfony\Component\Routing\RouteCollection;
18+
use Symfony\Component\Routing\Router as SymfonyRouter;
19+
20+
/**
21+
* @author Kévin Dunglas <[email protected]>
22+
*/
23+
class RouterTest extends \PHPUnit_Framework_TestCase
24+
{
25+
public function testContextAccessor()
26+
{
27+
$context = new RequestContext();
28+
29+
$mockedRouter = $this->prophesize('Symfony\Component\Routing\RouterInterface');
30+
$mockedRouter->setContext($context)->shouldBeCalled();
31+
$mockedRouter->getContext()->willReturn($context)->shouldBeCalled();
32+
33+
$router = new Router($mockedRouter->reveal());
34+
$router->setContext($context);
35+
$this->assertSame($context, $router->getContext());
36+
}
37+
38+
public function testGetRouteCollection()
39+
{
40+
$routeCollection = new RouteCollection();
41+
42+
$mockedRouter = $this->prophesize('Symfony\Component\Routing\RouterInterface');
43+
$mockedRouter->getRouteCollection()->willReturn($routeCollection)->shouldBeCalled();
44+
45+
$router = new Router($mockedRouter->reveal());
46+
$this->assertSame($routeCollection, $router->getRouteCollection());
47+
}
48+
49+
public function testGenerate()
50+
{
51+
$mockedRouter = $this->prophesize('Symfony\Component\Routing\RouterInterface');
52+
$mockedRouter->generate('foo', [], false)->willReturn('/bar')->shouldBeCalled();
53+
54+
$router = new Router($mockedRouter->reveal());
55+
$this->assertSame('/bar', $router->generate('foo'));
56+
}
57+
58+
public function testMatch()
59+
{
60+
$context = new RequestContext('/app_dev.php', 'GET', 'localhost', 'https');
61+
62+
$mockedRouter = $this->prophesize('Symfony\Component\Routing\RouterInterface');
63+
$mockedRouter->getContext()->willReturn($context)->shouldBeCalled();
64+
$mockedRouter->setContext(Argument::type('Symfony\Component\Routing\RequestContext'))->shouldBeCalled();
65+
$mockedRouter->setContext($context)->shouldBeCalled();
66+
$mockedRouter->match('/foo')->willReturn(['bar'])->shouldBeCalled();
67+
68+
$router = new Router($mockedRouter->reveal());
69+
70+
$this->assertEquals(['bar'], $router->match('/app_dev.php/foo'));
71+
}
72+
}

0 commit comments

Comments
 (0)