Fixed a bug and added unit-tests for GlobalVariables

This commit is contained in:
Iltar van der Berg 2014-09-24 11:23:26 +02:00
parent cc04ce15c0
commit 3f055f706c
2 changed files with 41 additions and 9 deletions

View File

@ -56,10 +56,12 @@ class GlobalVariables
*/ */
public function getUser() public function getUser()
{ {
if (!$tokenStorage = $this->container->get('security.token_storage')) { if (!$this->container->has('security.token_storage')) {
return; return;
} }
$tokenStorage = $this->container->get('security.token_storage');
if (!$token = $tokenStorage->getToken()) { if (!$token = $tokenStorage->getToken()) {
return; return;
} }

View File

@ -35,25 +35,55 @@ class GlobalVariablesTest extends TestCase
$this->assertSame($securityContext, $this->globals->getSecurity()); $this->assertSame($securityContext, $this->globals->getSecurity());
} }
public function testGetUser() public function testGetUserNoTokenStorage()
{ {
// missing test cases to return null, only happy flow tested $this->assertNull($this->globals->getUser());
$securityContext = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); }
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$this->container->set('security.token_storage', $securityContext); public function testGetUserNoToken()
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$this->container->set('security.token_storage', $tokenStorage);
$this->assertNull($this->globals->getUser());
}
/**
* @dataProvider getUserProvider
*/
public function testGetUser($user, $expectedUser)
{
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$this->container->set('security.token_storage', $tokenStorage);
$token $token
->expects($this->once()) ->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)); ->will($this->returnValue($user));
$securityContext $tokenStorage
->expects($this->once()) ->expects($this->once())
->method('getToken') ->method('getToken')
->will($this->returnValue($token)); ->will($this->returnValue($token));
$this->assertSame($user, $this->globals->getUser()); $this->assertSame($expectedUser, $this->globals->getUser());
}
public function getUserProvider()
{
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$std = new \stdClass();
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
return array(
array($user, $user),
array($std, $std),
array($token, $token),
array('Anon.', null),
array(null, null),
array(10, null),
array(true, null),
);
} }
} }