|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SecurityBundle\Tests\Security; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Container\ContainerInterface; |
| 16 | +use Symfony\Bundle\SecurityBundle\Security\Security; |
| 17 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 18 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 19 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 20 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 21 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 22 | +use Symfony\Component\Security\Core\User\InMemoryUser; |
| 23 | + |
| 24 | +class SecurityTest extends TestCase |
| 25 | +{ |
| 26 | + public function testGetToken() |
| 27 | + { |
| 28 | + $token = new UsernamePasswordToken(new InMemoryUser('foo', 'bar'), 'provider'); |
| 29 | + $tokenStorage = $this->createMock(TokenStorageInterface::class); |
| 30 | + |
| 31 | + $tokenStorage->expects($this->once()) |
| 32 | + ->method('getToken') |
| 33 | + ->willReturn($token); |
| 34 | + |
| 35 | + $container = $this->createContainer('security.token_storage', $tokenStorage); |
| 36 | + |
| 37 | + $security = new Security($container); |
| 38 | + $this->assertSame($token, $security->getToken()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider getUserTests |
| 43 | + */ |
| 44 | + public function testGetUser($userInToken, $expectedUser) |
| 45 | + { |
| 46 | + $token = $this->createMock(TokenInterface::class); |
| 47 | + $token->expects($this->any()) |
| 48 | + ->method('getUser') |
| 49 | + ->willReturn($userInToken); |
| 50 | + $tokenStorage = $this->createMock(TokenStorageInterface::class); |
| 51 | + |
| 52 | + $tokenStorage->expects($this->once()) |
| 53 | + ->method('getToken') |
| 54 | + ->willReturn($token); |
| 55 | + |
| 56 | + $container = $this->createContainer('security.token_storage', $tokenStorage); |
| 57 | + |
| 58 | + $security = new Security($container); |
| 59 | + $this->assertSame($expectedUser, $security->getUser()); |
| 60 | + } |
| 61 | + |
| 62 | + public function getUserTests() |
| 63 | + { |
| 64 | + yield [null, null]; |
| 65 | + |
| 66 | + $user = new InMemoryUser('nice_user', 'foo'); |
| 67 | + yield [$user, $user]; |
| 68 | + } |
| 69 | + |
| 70 | + public function testIsGranted() |
| 71 | + { |
| 72 | + $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); |
| 73 | + |
| 74 | + $authorizationChecker->expects($this->once()) |
| 75 | + ->method('isGranted') |
| 76 | + ->with('SOME_ATTRIBUTE', 'SOME_SUBJECT') |
| 77 | + ->willReturn(true); |
| 78 | + |
| 79 | + $container = $this->createContainer('security.authorization_checker', $authorizationChecker); |
| 80 | + |
| 81 | + $security = new Security($container); |
| 82 | + $this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT')); |
| 83 | + } |
| 84 | + |
| 85 | + private function createContainer(string $serviceId, object $serviceObject): ContainerInterface |
| 86 | + { |
| 87 | + return new ServiceLocator([$serviceId => fn () => $serviceObject]); |
| 88 | + } |
| 89 | +} |
0 commit comments