|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[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 Symfony\Bundle\FrameworkBundle\Tests\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Application; |
| 15 | +use Symfony\Component\Console\Tester\CommandTester; |
| 16 | +use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand; |
| 18 | +use Symfony\Component\Routing\Route; |
| 19 | +use Symfony\Component\Routing\RouteCollection; |
| 20 | +use Symfony\Component\Routing\RequestContext; |
| 21 | + |
| 22 | +class RouterMatchCommandTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + public function testWithMatchPath() |
| 25 | + { |
| 26 | + $tester = $this->createCommandTester(); |
| 27 | + $ret = $tester->execute(array('path_info' => '/foo', 'foo')); |
| 28 | + |
| 29 | + $this->assertEquals(0, $ret, 'Returns 0 in case of success'); |
| 30 | + $this->assertContains('[router] Route "foo"', $tester->getDisplay()); |
| 31 | + } |
| 32 | + |
| 33 | + public function testWithNotMatchPath() |
| 34 | + { |
| 35 | + $tester = $this->createCommandTester(); |
| 36 | + $ret = $tester->execute(array('path_info' => '/test', 'foo')); |
| 37 | + |
| 38 | + $this->assertEquals(1, $ret, 'Returns 1 in case of failure'); |
| 39 | + $this->assertContains('None of the routes match the path "/test"', $tester->getDisplay()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return CommandTester |
| 44 | + */ |
| 45 | + private function createCommandTester() |
| 46 | + { |
| 47 | + $application = new Application(); |
| 48 | + |
| 49 | + $command = new RouterMatchCommand(); |
| 50 | + $command->setContainer($this->getContainer()); |
| 51 | + $application->add($command); |
| 52 | + |
| 53 | + $command = new RouterDebugCommand(); |
| 54 | + $command->setContainer($this->getContainer()); |
| 55 | + $application->add($command); |
| 56 | + |
| 57 | + return new CommandTester($application->find('router:match')); |
| 58 | + } |
| 59 | + |
| 60 | + private function getContainer() |
| 61 | + { |
| 62 | + $routeCollection = new RouteCollection(); |
| 63 | + $routeCollection->add('foo', new Route('foo')); |
| 64 | + $requestContext = new RequestContext(); |
| 65 | + $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); |
| 66 | + $router |
| 67 | + ->expects($this->any()) |
| 68 | + ->method('getRouteCollection') |
| 69 | + ->will($this->returnValue($routeCollection)) |
| 70 | + ; |
| 71 | + $router |
| 72 | + ->expects($this->any()) |
| 73 | + ->method('getContext') |
| 74 | + ->will($this->returnValue($requestContext)) |
| 75 | + ; |
| 76 | + |
| 77 | + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
| 78 | + $container |
| 79 | + ->expects($this->once()) |
| 80 | + ->method('has') |
| 81 | + ->with('router') |
| 82 | + ->will($this->returnValue(true)) |
| 83 | + ; |
| 84 | + $container |
| 85 | + ->expects($this->atLeastOnce()) |
| 86 | + ->method('get') |
| 87 | + ->with('router') |
| 88 | + ->will($this->returnValue($router)) |
| 89 | + ; |
| 90 | + |
| 91 | + return $container; |
| 92 | + } |
| 93 | +} |
0 commit comments