* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Core\Tests; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\User\User; class SecurityTest extends TestCase { public function testGetToken() { $token = new UsernamePasswordToken('foo', 'bar', 'provider'); $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); $tokenStorage->expects($this->once()) ->method('getToken') ->will($this->returnValue($token)); $container = $this->createContainer('security.token_storage', $tokenStorage); $security = new Security($container); $this->assertSame($token, $security->getToken()); } /** * @dataProvider getUserTests */ public function testGetUser($userInToken, $expectedUser) { $token = $this->getMockBuilder(TokenInterface::class)->getMock(); $token->expects($this->any()) ->method('getUser') ->will($this->returnValue($userInToken)); $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); $tokenStorage->expects($this->once()) ->method('getToken') ->will($this->returnValue($token)); $container = $this->createContainer('security.token_storage', $tokenStorage); $security = new Security($container); $this->assertSame($expectedUser, $security->getUser()); } public function getUserTests() { yield array(null, null); yield array('string_username', null); $user = new User('nice_user', 'foo'); yield array($user, $user); } public function testIsGranted() { $authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock(); $authorizationChecker->expects($this->once()) ->method('isGranted') ->with('SOME_ATTRIBUTE', 'SOME_SUBJECT') ->will($this->returnValue(true)); $container = $this->createContainer('security.authorization_checker', $authorizationChecker); $security = new Security($container); $this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT')); } private function createContainer($serviceId, $serviceObject) { $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); $container->expects($this->atLeastOnce()) ->method('get') ->with($serviceId) ->will($this->returnValue($serviceObject)); return $container; } } __halt_compiler();----SIGNATURE:----WbNKMrgTVoN8NbIfS6WwNPqzYo4eFginZPUz6pn9Dle+vfQYhGYX/Y20RDnkCcv2RkJwT/i0KbcjMkyq0UvxfvXHPlzP29xuMfiQZt8f9HnpqtQqvKADGGcD6LpFj3luzdxUTboxJPtmu3enikW5N+oKpnhy+xluLbmOUbek12f5+ATbSN3AnJOu13h0Rak+u3mkTjrwvq+pgMIq3K+uNxXQI60mAqYUHq/PRY3Ebq8zZLO4E00DiVFhb9xb6qVFLQFp9LuTkW7uIBJ/gI26ApBshEPB0LE/GLd7kOasjhoAtGQ+ZQlfBPAk+uHxgk5j6BYWn6W7ppVPzjsSHYqvDSHgKi1PKHxf2EMYxjA893CKL1Z6DGvQOhFDFSA5PUGPoAz2sqpiEe+3od/RJHgZ3jfl9ln+2w/7tSb2dVD0WYcIGD14ub+VYTnT4xkoarnGJjegvVoD4tFvhufR4sZWYfZVDWPZz7IX+kkn8e89hfFeTs1JNxvoXkV12BMOKzExz4wvstfCD5Jt/w8pD+dZRHmvrWSjC+f4usRtTfuSHxv0QLwbU3KKNTMl32qDkpCpazJtnHuVFmLo4t7Oexr9OWFYbm3G2wPiDARWC8U2gJ6hVPBCm5jQMV6wEhdJBZHRx4rXF0keE/DI+XQcwYkeshJpmV0AmFYxVSU7kI1+Qec=----ATTACHMENT:----NzM1Nzg4OTM3NjE4MDc5IDYzMDc5Mzc3NjYwMDA3MjcgNTA4MjE5NDY4NTIzNTMyNA==