This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/GlobalVariablesTest.php

107 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.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\Templating;
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\Container;
class GlobalVariablesTest extends TestCase
{
private $container;
private $globals;
protected function setUp()
{
$this->container = new Container();
$this->globals = new GlobalVariables($this->container);
}
2016-12-05 22:50:48 +00:00
public function testGetTokenNoTokenStorage()
{
$this->assertNull($this->globals->getToken());
}
public function testGetTokenNoToken()
{
2016-12-19 16:14:19 +00:00
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
2016-12-05 22:50:48 +00:00
$this->container->set('security.token_storage', $tokenStorage);
$this->assertNull($this->globals->getToken());
}
public function testGetToken()
{
2016-12-19 16:14:19 +00:00
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
2016-12-05 22:50:48 +00:00
$this->container->set('security.token_storage', $tokenStorage);
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue('token'));
$this->assertSame('token', $this->globals->getToken());
}
public function testGetUserNoTokenStorage()
{
$this->assertNull($this->globals->getUser());
}
public function testGetUserNoToken()
{
2016-12-19 16:00:11 +00:00
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
$this->container->set('security.token_storage', $tokenStorage);
$this->assertNull($this->globals->getUser());
}
/**
* @dataProvider getUserProvider
*/
public function testGetUser($user, $expectedUser)
{
2016-12-19 16:00:11 +00:00
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
$this->container->set('security.token_storage', $tokenStorage);
$token
->expects($this->once())
->method('getUser')
->will($this->returnValue($user));
$tokenStorage
->expects($this->once())
->method('getToken')
->will($this->returnValue($token));
$this->assertSame($expectedUser, $this->globals->getUser());
}
public function getUserProvider()
{
2016-12-19 16:00:11 +00:00
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
$std = new \stdClass();
2016-12-19 16:00:11 +00:00
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
return array(
array($user, $user),
array($std, $std),
array($token, $token),
array('Anon.', null),
array(null, null),
array(10, null),
array(true, null),
);
}
}