Skip to content

Commit 2ed119a

Browse files
committed
minor #14432 [Framework] added test for router commands. (aitboudad)
This PR was merged into the 2.3 branch. Discussion ---------- [Framework] added test for router commands. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Fixed tickets | ~ | Tests pass? | yes | License | MIT - [x] router:debug - [x] router:match Commits ------- 6d403a7 [Framework] added test for Router commands.
2 parents 94ae514 + c533ecb commit 2ed119a

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\RouterDebugCommand;
17+
use Symfony\Component\Routing\Route;
18+
use Symfony\Component\Routing\RouteCollection;
19+
20+
class RouterDebugCommandTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testDebugAllRoutes()
23+
{
24+
$tester = $this->createCommandTester();
25+
$ret = $tester->execute(array('name' => null));
26+
27+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
28+
$this->assertContains('[router] Current routes', $tester->getDisplay());
29+
}
30+
31+
public function testDebugSingleRoute()
32+
{
33+
$tester = $this->createCommandTester();
34+
$ret = $tester->execute(array('name' => 'foo'));
35+
36+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
37+
$this->assertContains('[router] Route "foo"', $tester->getDisplay());
38+
}
39+
40+
/**
41+
* @expectedException \InvalidArgumentException
42+
*/
43+
public function testDebugInvalidRoute()
44+
{
45+
$this->createCommandTester()->execute(array('name' => 'test'));
46+
}
47+
48+
/**
49+
* @return CommandTester
50+
*/
51+
private function createCommandTester()
52+
{
53+
$application = new Application();
54+
55+
$command = new RouterDebugCommand();
56+
$command->setContainer($this->getContainer());
57+
$application->add($command);
58+
59+
return new CommandTester($application->find('router:debug'));
60+
}
61+
62+
private function getContainer()
63+
{
64+
$routeCollection = new RouteCollection();
65+
$routeCollection->add('foo', new Route('foo'));
66+
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
67+
$router
68+
->expects($this->atLeastOnce())
69+
->method('getRouteCollection')
70+
->will($this->returnValue($routeCollection))
71+
;
72+
73+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
74+
$container
75+
->expects($this->once())
76+
->method('has')
77+
->with('router')
78+
->will($this->returnValue(true))
79+
;
80+
$container
81+
->expects($this->atLeastOnce())
82+
->method('get')
83+
->with('router')
84+
->will($this->returnValue($router))
85+
;
86+
87+
return $container;
88+
}
89+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)