|
| 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