public function onBootstrap(EventInterface $e)
 {
     /** @var EventManager $em */
     $em = $e->getApplication()->getEventManager();
     $request = $e->getApplication()->getRequest();
     $response = $e->getApplication()->getResponse();
     // Write token to cookie after valid authentication
     $placeCookie = new WriteTokenToCookie();
     $placeCookie->setRequest($request);
     $placeCookie->setResponse($response);
     $placeCookie->setServiceLocator($e->getApplication()->getServiceManager());
     $em->getSharedManager()->attachAggregate($placeCookie);
     // Try to login from Cookie if applicable
     $service = new CookieAuthenticationService();
     $service->setServiceLocator($e->getApplication()->getServiceManager());
     $service->loginFrom($request, $response);
 }
 public function testValidLogin()
 {
     $request = new Request();
     $response = new Response();
     $serieTokenInCookie = new SerieToken(1, 'abc', 'def');
     $newSerie = new SerieToken(1, 'abc', 'ghi');
     // Assume valid user
     $this->userMapper->expects($this->once())->method('findById')->will($this->returnValue($this->getMock('ZfcUser\\Entity\\UserInterface')));
     // Request contains cookie
     $this->cookieService->expects($this->once())->method('read')->with($request, $response)->will($this->returnValue($serieTokenInCookie));
     // Response contains updated cookie
     $this->cookieService->expects($this->once())->method('writeSerie')->with($response, $newSerie);
     $newSerie->setExpiresAt(new \DateTime('+3 days'));
     $this->rememberMeService->expects($this->once())->method('getNextInSerie')->with($serieTokenInCookie)->will($this->returnValue($newSerie));
     $this->authService->expects($this->once())->method('authenticate');
     $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface');
     $this->service->setEventManager($eventManager);
     $eventManager->expects($this->once())->method('trigger')->with('login', $this->service, ['token' => $newSerie]);
     $this->service->loginFrom($request, $response);
 }