8000 [FrameworkBundle] Added unit tests for Controller::getUser() · symfony/symfony@982afbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 982afbd

Browse files
committed
[FrameworkBundle] Added unit tests for Controller::getUser()
1 parent 8c5bd20 commit 982afbd

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 94 additions & 1 deletion
< 8000 tr>
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\RequestStack;
1819
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
21+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
22+
use Symfony\Component\Security\Core\User\User;
1923

2024
class ControllerTest extends TestCase
2125
{
@@ -37,10 +41,99 @@ public function testForward()
3741
$container->expects($this->at(0))->method('get')->will($this->returnValue($requestStack));
3842
$container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));
3943

40-
$controller = new Controller();
44+
$controller = new TestController();
4145
$controller->setContainer($container);
4246

4347
$response = $controller->forward('a_controller');
4448
$this->assertEquals('xml--fr', $response->getContent());
4549
}
50+
51+
public function testGetUser()
52+
{
53+
$user = new User('user', 'pass');
54+
$token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER'));
55+
56+
$controller = new TestController();
57+
$controller->setContainer($this->getContainerWithTokenStorage($token));
58+
59+
$this->assertSame($controller->getUser(), $user);
60+
}
61+
62+
public function testGetUserAnonymousUserConvertedToNull()
63+
{
64+
$token = new AnonymousToken('default', 'anon.');
65+
66+
$controller = new TestController();
67+
$controller->setContainer($this->getContainerWithTokenStorage($token));
68+
69+
$this->assertNull($controller->getUser());
70+
}
71+
72+
public function testGetUserWithEmptyTokenStorage()
73+
{
74+
$controller = new TestController();
75+
$controller->setContainer($this->getContainerWithTokenStorage(null));
76+
77+
$this->assertNull($controller->getUser());
78+
}
79+
80+
/**
81+
* @expectedException \LogicException
82+
* @expectedExceptionMessage The SecurityBundle is not registered in your application.
83+
*/
84+
public function testGetUserWithEmptyContainer()
85+
{
86+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
87+
$container
88+
->expects($this->once())
89+
->method('has')
90+
->with('security.token_storage')
91+
->will($this->returnValue(false));
92+
93+
$controller = new TestController();
94+
$controller->setContainer($container);
95+
96+
$controller->getUser();
97+
}
98+
99+
/**
100+
* @param $token
101+
* @return ContainerInterface
102+
*/
103+
private function getContainerWithTokenStorage($token = null)
104+
{
105+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage');
106+
$tokenStorage
107+
->expects($this->once())
108+
->method('getToken')
109+
->will($this->returnValue($token));
110+
111+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
112+
$container
113+
->expects($this->once())
114+
->method('has')
115+
->with('security.token_storage')
116+
->will($this->returnValue(true));
117+
118+
$container
119+
->expects($this->once())
120+
->method('get')
121+
->with('security.token_storage')
122+
->will($this->returnValue($tokenStorage));
123+
124+
return $container;
125+
}
126+
}
127+
128+
class TestController extends Controller
129+
{
130+
public function forward($controller, array $path = array(), array $query = array())
131+
{
132+
return parent::forward($controller, $path, $query);
133+
}
134+
135+
public function getUser()
136+
{
137+
return parent::getUser();
138+
}
46139
}

0 commit comments

Comments
 (0)
0