Skip to content

Commit 3f055f7

Browse files
author
Iltar van der Berg
committed
Fixed a bug and added unit-tests for GlobalVariables
1 parent cc04ce1 commit 3f055f7

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ public function getSecurity()
5656
*/
5757
public function getUser()
5858
{
59-
if (!$tokenStorage = $this->container->get('security.token_storage')) {
59+
if (!$this->container->has('security.token_storage')) {
6060
return;
6161
}
6262

63+
$tokenStorage = $this->container->get('security.token_storage');
64+
6365
if (!$token = $tokenStorage->getToken()) {
6466
return;
6567
}

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php

+37-7
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,55 @@ public function testGetSecurity()
3535
$this->assertSame($securityContext, $this->globals->getSecurity());
3636
}
3737

38-
public function testGetUser()
38+
public function testGetUserNoTokenStorage()
3939
{
40-
// missing test cases to return null, only happy flow tested
41-
$securityContext = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
40+
$this->assertNull($this->globals->getUser());
41+
}
42+
43+
public function testGetUserNoToken()
44+
{
45+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
46+
$this->container->set('security.token_storage', $tokenStorage);
47+
$this->assertNull($this->globals->getUser());
48+
}
49+
50+
/**
51+
* @dataProvider getUserProvider
52+
*/
53+
public function testGetUser($user, $expectedUser)
54+
{
55+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
4256
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
43-
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
4457

45-
$this->container->set('security.token_storage', $securityContext);
58+
$this->container->set('security.token_storage', $tokenStorage);
4659

4760
$token
4861
->expects($this->once())
4962
->method('getUser')
5063
->will($this->returnValue($user));
5164

52-
$securityContext
65+
$tokenStorage
5366
->expects($this->once())
5467
->method('getToken')
5568
->will($this->returnValue($token));
5669

57-
$this->assertSame($user, $this->globals->getUser());
70+
$this->assertSame($expectedUser, $this->globals->getUser());
71+
}
72+
73+
public function getUserProvider()
74+
{
75+
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
76+
$std = new \stdClass();
77+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
78+
79+
return array(
80+
array($user, $user),
81+
array($std, $std),
82+
array($token, $token),
83+
array('Anon.', null),
84+
array(null, null),
85+
array(10, null),
86+
array(true, null),
87+
);
5888
}
5989
}

0 commit comments

Comments
 (0)