/**
  * @test
  */
 public function shouldWrapAnyThrownExceptionsAsAuthenticatedServiceException()
 {
     $samlSpInfoHelper = new SamlSpInfoHelper();
     $providerKey = 'main';
     $expectedSamlSpInfo = $samlSpInfoHelper->getSamlSpInfo();
     $expectedPreviousException = new \Exception($expectedMessage = 'Something goes wrong', $expectedCode = 21);
     $userProviderMock = $this->createUserManagerMock();
     $userProviderMock->expects($this->once())->method('loadUserBySamlInfo')->will($this->throwException($expectedPreviousException));
     $authProvider = new SamlSpAuthenticationProvider($providerKey, $userProviderMock, $this->createUserCheckerMock());
     $token = new SamlSpToken($providerKey);
     $token->setUser('');
     $token->setSamlSpInfo($expectedSamlSpInfo);
     try {
         $authProvider->authenticate($token);
     } catch (AuthenticationServiceException $e) {
         $this->assertSame($expectedPreviousException, $e->getPrevious(), $e->getPrevious());
         $this->assertEquals($expectedMessage, $e->getMessage());
         $this->assertEquals($expectedCode, $e->getCode());
         $this->assertNull($e->getToken());
         return;
     }
     $this->fail('Expected exception: AuthenticationServiceException was not thrown');
 }
 /**
  * @param \AerialShip\SamlSPBundle\Bridge\SamlSpInfo $samlInfo
  * @param array $attributes
  * @param array $roles
  * @param mixed $user
  * @return SamlSpToken
  */
 protected function createAuthenticatedToken(SamlSpInfo $samlInfo, array $attributes, array $roles, $user)
 {
     if ($user instanceof UserInterface && $this->userChecker) {
         $this->userChecker->checkPostAuth($user);
     }
     $newToken = new SamlSpToken($this->providerKey, $roles);
     $newToken->setUser($user);
     $newToken->setAttributes($attributes);
     $newToken->setSamlSpInfo($samlInfo);
     $newToken->setAuthenticated(true);
     if (!in_array('ROLE_USER', $roles)) {
         $roles[] = 'ROLE_USER';
     }
     return $newToken;
 }