/
RedirectControllerTest.php
96 lines (75 loc) · 3 KB
/
RedirectControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Bundle\FrameworkBundle\Tests\Logger;
use Symfony\Bundle\FrameworkBundle\Tests\Kernel;
/**
* @author Marcin Sikon<marcin.sikon@gmail.com>
*/
class RedirectControllerTest extends TestCase
{
public function testEmptyRoute()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$controller = new RedirectController();
$controller->setContainer($container);
$returnResponse = $controller->redirectAction('');
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertEquals(410, $returnResponse->getStatusCode());
}
/**
* @dataProvider provider
*/
public function testRoute($permanent, $expectedCode)
{
$request = new Request();
$route = 'new-route';
$url = '/redirect-url';
$params = array('additional-parameter' => 'value');
$request->attributes = new ParameterBag(array('route' => $route, '_route' => 'current-route', 'permanent' => $permanent) + $params);
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$router
->expects($this->once())
->method('generate')
->with($this->equalTo($route),$this->equalTo($params))
->will($this->returnValue($url));
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->at(0))
->method('get')
->with($this->equalTo('request'))
->will($this->returnValue($request));
$container
->expects($this->at(1))
->method('get')
->with($this->equalTo('router'))
->will($this->returnValue($router));
$controller = new RedirectController();
$controller->setContainer($container);
$returnResponse = $controller->redirectAction($route, $permanent);
$this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $returnResponse);
$this->assertTrue($returnResponse->isRedirect());
$this->assertTrue($returnResponse->isRedirected($url));
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
}
public function provider()
{
return array(
array(true, 301),
array(false, 302),
);
}
}