2014-09-24 08:31:12 +01:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->container = new Container();
|
|
|
|
$this->globals = new GlobalVariables($this->container);
|
|
|
|
}
|
|
|
|
|
2015-01-04 20:27:00 +00:00
|
|
|
public function testLegacyGetSecurity()
|
2014-09-24 08:31:12 +01:00
|
|
|
{
|
2015-01-04 20:27:00 +00:00
|
|
|
$this->iniSet('error_reporting', -1 & E_USER_DEPRECATED);
|
|
|
|
|
2014-09-24 08:31:12 +01:00
|
|
|
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
|
|
|
|
|
$this->assertNull($this->globals->getSecurity());
|
|
|
|
$this->container->set('security.context', $securityContext);
|
|
|
|
$this->assertSame($securityContext, $this->globals->getSecurity());
|
|
|
|
}
|
|
|
|
|
2014-09-24 10:23:26 +01:00
|
|
|
public function testGetUserNoTokenStorage()
|
2014-09-24 08:31:12 +01:00
|
|
|
{
|
2014-09-24 10:23:26 +01:00
|
|
|
$this->assertNull($this->globals->getUser());
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2014-09-24 08:31:12 +01:00
|
|
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
|
|
|
|
2014-09-24 10:23:26 +01:00
|
|
|
$this->container->set('security.token_storage', $tokenStorage);
|
2014-09-24 08:31:12 +01:00
|
|
|
|
|
|
|
$token
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
2014-09-24 10:23:26 +01:00
|
|
|
$tokenStorage
|
2014-09-24 08:31:12 +01:00
|
|
|
->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->will($this->returnValue($token));
|
|
|
|
|
2014-09-24 10:23:26 +01:00
|
|
|
$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),
|
|
|
|
);
|
2014-09-24 08:31:12 +01:00
|
|
|
}
|
|
|
|
}
|