* * 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:----tM+LW4UeAb6KV+TPGB2A5VyEb8qE8TBrs8fPxrngG8hlaE4mYjZovRaD7UWqdLfofdgJqM/gv+i5bRV4hkwqYKv7wWnRwwgeK51fiBV9rxGCNqPQt3340SHv4nU1PjqbitE6NdllHUJ8C1le/5wtMsVVucI4kdHJZ0Jj4XhT4MO3UexOH7WsJJejClmmby0PBse1PTy/gcK7PGHsFXR03F5mtNhl6G5eEaZSZSvW0rzQqykrfOJdcqy//SAImsPp077Qs2cYF5SBHpF10Ra5WNaYds1cWMnm+nZaYwKRpWspvDEkiopISpIUTaKmOK3IfcmhcmMYZY2ikRSEw2TX/1OsKA/u7QTFqBKW8fxhYGFlqLRFSMHlIpuyxlXNgvq5WZQOROyls9Rc/zO9uvA6weOTKEXGN5NEA0cYLjwscOOeX/nky2xyilAnZidSiZO9eqFJRzQJ3pT7hCeGMiHcXrwEXp2AEWAZ2sFljxcxhsP23t1AZ87QkR7kMEjxwPwnUpBm1Tct3ZNgTJW8j30uwLF7ahNPRRsiH0IUy6ZlMmTpSgiTO3uoXtfSbGWjyekgB4qg4xbNLYUjvptDy8WBpNBFQzjeHOb5cd849K1Uwsnrp7k0wWcgLtfbdlUy1iTMI9BMU5jmdEE2kGpOYShpwHn5qsEiQ+7NGAJ3fFiXFXM=----ATTACHMENT:----MzExNjc4NTI4MzU0Nzg5OCA5NzkxNjU3MDgyNTY2ODEzIDY1MDQ4MTk2NTIzNzg4MDg=