Repository: symfony/symfony
File: src/Symfony/Bundle/SecurityBundle/Tests/SecurityTest.php (PHP)

Code before:
            ->expects($this->atLeastOnce())
            ->method('get')
            ->willReturnMap([
                ['request_stack', $requestStack],
                ['security.firewall.map', $firewallMap],
                ['security.user_checker', $userChecker],
            ])
        ;

        $requestStack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
        $firewallMap->expects($this->once())->method('getFirewallConfig')->willReturn($firewall);

        $security = new Security($container, ['main' => null]);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('No authenticators found for firewall "main".');

        $security->login($user);
    }

    public function testLoginWithoutRequestContext()
    {
        $requestStack = $this->createMock(RequestStack::class);
        $user = $this->createMock(UserInterface::class);

        $container = $this->createMock(ContainerInterface::class);
        $container
            ->expects($this->atLeastOnce())
            ->method('get')
            ->willReturnMap([
                ['request_stack', $requestStack],
            ])
        ;

        $requestStack->expects($this->once())->method('getCurrentRequest')->willReturn(null);

        $security = new Security($container, ['main' => null]);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Unable to login without a request context.');

        $security->login($user);
    }

    public function testLogout()
    {
        $request = new Request();
        $requestStack = $this->createMock(RequestStack::class);
        $requestStack->expects($this->once())->method('getMainRequest')->willReturn($request);

        $token = $this->createMock(TokenInterface::class);

Review comment (suggestion):
I would also not mock the `ContainerInterface` but use the `ContainerBuilder` class instead

Code after:
            ->expects($this->atLeastOnce())
            ->method('get')
            ->willReturnMap([
                ['request_stack', $requestStack],
                ['security.firewall.map', $firewallMap],
                ['security.user_checker', $userChecker],
            ])
        ;

        $requestStack->expects($this->once())->method('getCurrentRequest')->willReturn($request);
        $firewallMap->expects($this->once())->method('getFirewallConfig')->willReturn($firewall);

        $security = new Security($container, ['main' => null]);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('No authenticators found for firewall "main".');

        $security->login($user);
    }

    public function testLoginWithoutRequestContext()
    {
        $requestStack = new RequestStack();
        $user = $this->createMock(UserInterface::class);

        $container = $this->createMock(ContainerInterface::class);
        $container
            ->expects($this->atLeastOnce())
            ->method('get')
            ->willReturnMap([
                ['request_stack', $requestStack],
            ])
        ;

        $security = new Security($container, ['main' => null]);

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Unable to login without a request context.');

        $security->login($user);
    }

    public function testLogout()
    {
        $request = new Request();
        $requestStack = $this->createMock(RequestStack::class);
        $requestStack->expects($this->once())->method('getMainRequest')->willReturn($request);

        $token = $this->createMock(TokenInterface::class);
        $token->method('getUser')->willReturn(new InMemoryUser('foo', 'bar'));
        $tokenStorage = $this->createMock(TokenStorageInterface::class);