public function tokenFromAnAuthenticationManagerIsReplacedIfThereIsOneOfTheSameTypeInTheSession()
{
$mockAuthenticationManager = $this->createMock(AuthenticationManagerInterface::class);
$token1 = $this->createMock(TokenInterface::class);
$token1->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
$token1Clone = $this->createMock(TokenInterface::class);
$token1Clone->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token1Provider'));
$token1Clone->expects($this->any())->method('getAuthenticationStatus')->will($this->returnValue(TokenInterface::AUTHENTICATION_NEEDED));
$token2 = $this->createMock(TokenInterface::class);
$token2->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
$token2Clone = $this->createMock(TokenInterface::class);
$token2Clone->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token2Provider'));
$token2Clone->expects($this->any())->method('getAuthenticationStatus')->will($this->returnValue(TokenInterface::AUTHENTICATION_NEEDED));
$token3 = $this->createMock(TokenInterface::class);
$token3->expects($this->any())->method('getAuthenticationProviderName')->will($this->returnValue('token3Provider'));
$tokensFromTheManager = [$token1, $token2, $token3];
$tokensFromTheSession = [$token1Clone, $token2Clone];
$mockAuthenticationManager->expects($this->once())->method('getTokens')->will($this->returnValue($tokensFromTheManager));
$mockSession = $this->createMock(SessionInterface::class);
$mockSessionManager = $this->createMock(SessionManagerInterface::class);
$mockSessionManager->expects($this->any())->method('getCurrentSession')->will($this->returnValue($mockSession));
$mockSecurityLogger = $this->createMock(SecurityLoggerInterface::class);
$securityContext = $this->getAccessibleMock(Context::class, ['dummy']);
$securityContext->injectAuthenticationManager($mockAuthenticationManager);
$securityContext->setRequest($this->mockActionRequest);
$securityContext->_set('tokens', $tokensFromTheSession);
$securityContext->_set('sessionManager', $mockSessionManager);
$securityContext->_set('securityLogger', $mockSecurityLogger);
$securityContext->_call('initialize');
$expectedMergedTokens = [$token1Clone, $token2Clone, $token3];
$this->assertEquals(array_values($securityContext->_get('tokens')), $expectedMergedTokens);
}