/**
  * Marks users successfully authenticated to the firewall as online.
  *
  * @param OnFirewallAuthenticationEvent $event
  */
 public function onFirewallLogin(OnFirewallAuthenticationEvent $event)
 {
     /** @var \AppBundle\Model\User\User $user */
     $user = $event->getToken()->getUser();
     $this->userIdProvider->addUserId($user->getId());
     $user->updateLastAction();
     $this->entityManager->persist($user);
     $this->entityManager->flush();
 }
 /**
  * Marks users successfully authenticated to the firewall as online.
  *
  * @param OnFirewallAuthenticationEvent $event
  */
 public function onFirewallLogin(OnFirewallAuthenticationEvent $event)
 {
     /** @var \AppBundle\Model\User\User $user */
     $user = $event->getToken()->getUser();
     $this->userIdProvider->addUserId($user->getId());
     $user->setLastAction(new DateTime(sprintf('@%s', $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'))));
     $this->entityManager->persist($user);
     $this->entityManager->flush($user);
 }
 public function testUpdateOnlineUsers()
 {
     $provider = $this->getMock(OnlineUserIdWriteProviderInterface::class);
     $provider->expects($this->once())->method('addUserId');
     $user = $this->getMockWithoutInvokingTheOriginalConstructor(User::class);
     $entityManager = $this->getMock(EntityManagerInterface::class);
     $entityManager->expects($this->once())->method('persist')->with($user);
     $user->expects($this->once())->method('updateLastAction');
     $user->expects($this->once())->method('getId')->willReturn('1');
     $entityManager->expects($this->once())->method('flush');
     $token = $this->getMockWithoutInvokingTheOriginalConstructor(PreAuthenticatedToken::class);
     $token->expects($this->once())->method('getUser')->willReturn($user);
     $listener = new OnlineUsersUpdateListener($provider, $entityManager);
     $event = new OnFirewallAuthenticationEvent();
     $event->setToken($token);
     $listener->onFirewallLogin($event);
 }
 public function testUpdateOnlineUsers()
 {
     $provider = $this->getMock(OnlineUserIdDataProviderInterface::class);
     $provider->expects($this->once())->method('addUserId');
     $now = time();
     $request = Request::create('/');
     $request->server->set('REQUEST_TIME', $now);
     $requestStack = $this->getMock(RequestStack::class, ['getMasterRequest']);
     $requestStack->expects($this->once())->method('getMasterRequest')->willReturn($request);
     $user = User::create('Ma27', '123456', '*****@*****.**');
     $entityManager = $this->getMock(EntityManagerInterface::class);
     $entityManager->expects($this->once())->method('persist')->with($user);
     $entityManager->expects($this->once())->method('flush')->with($user);
     $token = $this->getMockWithoutInvokingTheOriginalConstructor(PreAuthenticatedToken::class);
     $token->expects($this->once())->method('getUser')->willReturn($user);
     $listener = new OnlineUsersUpdateListener($provider, $requestStack, $entityManager);
     $event = new OnFirewallAuthenticationEvent();
     $event->setToken($token);
     $listener->onFirewallLogin($event);
     $this->assertSame($now, $user->getLastAction()->getTimestamp());
 }