GlobalVariablesTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
  11. use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\DependencyInjection\Container;
  14. class GlobalVariablesTest extends TestCase
  15. {
  16. private $container;
  17. private $globals;
  18. protected function setUp()
  19. {
  20. $this->container = new Container();
  21. $this->globals = new GlobalVariables($this->container);
  22. }
  23. /**
  24. * @group legacy
  25. */
  26. public function testLegacyGetSecurity()
  27. {
  28. $securityContext = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContextInterface')->getMock();
  29. $this->assertNull($this->globals->getSecurity());
  30. $this->container->set('security.context', $securityContext);
  31. $this->assertSame($securityContext, $this->globals->getSecurity());
  32. }
  33. public function testGetUserNoTokenStorage()
  34. {
  35. $this->assertNull($this->globals->getUser());
  36. }
  37. public function testGetUserNoToken()
  38. {
  39. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  40. $this->container->set('security.token_storage', $tokenStorage);
  41. $this->assertNull($this->globals->getUser());
  42. }
  43. /**
  44. * @dataProvider getUserProvider
  45. */
  46. public function testGetUser($user, $expectedUser)
  47. {
  48. $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
  49. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  50. $this->container->set('security.token_storage', $tokenStorage);
  51. $token
  52. ->expects($this->once())
  53. ->method('getUser')
  54. ->will($this->returnValue($user));
  55. $tokenStorage
  56. ->expects($this->once())
  57. ->method('getToken')
  58. ->will($this->returnValue($token));
  59. $this->assertSame($expectedUser, $this->globals->getUser());
  60. }
  61. public function getUserProvider()
  62. {
  63. $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
  64. $std = new \stdClass();
  65. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  66. return array(
  67. array($user, $user),
  68. array($std, $std),
  69. array($token, $token),
  70. array('Anon.', null),
  71. array(null, null),
  72. array(10, null),
  73. array(true, null),
  74. );
  75. }
  76. }