public function testAuthenticatePreviouslyAnonymous()
{
$username = 'foo_user';
$password = 'publish';
$anonymousUserId = 10;
$existingUser = $this->createUser($anonymousUserId);
$existingToken = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken')->disableOriginalConstructor()->getMock();
$existingToken->expects($this->once())->method('getUsername')->will($this->returnValue(__METHOD__));
$existingToken->expects($this->once())->method('getUser')->will($this->returnValue($existingUser));
$request = new Request();
$request->attributes->set('username', $username);
$request->attributes->set('password', $password);
$usernamePasswordToken = new UsernamePasswordToken($username, $password, self::PROVIDER_KEY);
$authenticatedToken = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken')->disableOriginalConstructor()->getMock();
$this->authenticationManager->expects($this->once())->method('authenticate')->with($this->equalTo($usernamePasswordToken))->will($this->returnValue($authenticatedToken));
$this->eventDispatcher->expects($this->once())->method('dispatch')->with(SecurityEvents::INTERACTIVE_LOGIN, $this->equalTo(new InteractiveLoginEvent($request, $authenticatedToken)));
$this->tokenStorage->expects($this->at(0))->method('getToken')->will($this->returnValue($existingToken));
$this->tokenStorage->expects($this->at(1))->method('setToken')->with($authenticatedToken);
$this->tokenStorage->expects($this->at(2))->method('getToken')->will($this->returnValue($authenticatedToken));
$authenticatedUser = $this->createUser(456);
$authenticatedToken->expects($this->once())->method('getUser')->will($this->returnValue($authenticatedUser));
$this->configResolver->expects($this->once())->method('getParameter')->with('anonymous_user_id')->will($this->returnValue($anonymousUserId));
$this->assertSame($authenticatedToken, $this->authenticator->authenticate($request));
}