* * 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:----SgjPnHAaMp3tAWhAbLm9PK+tJhFy/NHEgnkkdMLFmGEntpBuIFVudYIlg8VZ/AQEcV6XD6ncbnnWYf8l/R1O4GPe31q8MSfJB0di/uT+0ij19uZ615QC0pkAAENURXCHqcDqgnQFB9BOgdB/5dtUkXrS5PtYNTznI5JO6On6VfzU+d8XceySZ/NV3IIClUABJGRyHbpoJgdSPenROc7U8RwBwwqi4kx/38yiqyrYsDt6yB8+4IDCBYYIi8Jf6GJ1yJKRGAJX5z3hPbS87AIrkoVQ/sUB6oIzRqU2P++xHHtiAJB/yZcMJ+y8gyX09WDbai4cm8hIeU94BMhnDBZrVzwOFtT54MvoeKmVVk+DbpFkabGYMIq9njcGckC7ZacrQ2qJHTlrpG8KO3bmQP3FzZZ9kllW1yBBH+F+DLqh2HcQpWhOwloCDZepR7AcwcrMULkbZYw7UeHCRzLB9rfIe7ZZGNVHyc8mhRAC5bl8ggnro9YlTFhON58gHYN+eR+aum715uQ0E36uoBphV7be3YAteXX9Ym3C0OwsQx11Ue6q1/LSRsLrXtf/+9rDBw35Pioh/5anbxsWy+DY8+jlhEVsJ5XtqOw2q+D98jUR8kZK7LshmhK2tp2/FJYnim+Q3jwL5bCUnnQRMb5Xb4iWX3uLuOFmNRWSv/EkFLCN98o=----ATTACHMENT:----OTkwNDU3NTc5ODI1MDQ1NSAzMjc0NjA5MTg4ODY1MTAyIDk4NjcwNTc2MjYzMDA0NzI=