/**
  * @param Request $request
  * @return RedirectResponse
  */
 public function authorize(Request $request)
 {
     $query = $request->query;
     $token = $this->authService->getStoredRequestToken();
     if ($query->get('oauth_token') == $token->getToken() && $query->get('oauth_verifier')) {
         $user = $this->authService->getAccessToken($token, $query->get('oauth_verifier'));
         $this->authService->removeStoredRequestToken();
         $this->userSessionService->setMinimalUserInfo($user);
     }
     if ($query->get('destination')) {
         return new RedirectResponse($query->get('destination'));
     } else {
         return new RedirectResponse($this->urlGenerator->generate($this->defaultDestination));
     }
 }
 /**
  * @test
  */
 public function it_redirects_to_a_destination_after_authorisation()
 {
     $oauthVerifier = 'verification';
     // The authorisation method should get the stored request token.
     $this->authService->expects($this->any())->method('getStoredRequestToken')->willReturn($this->requestToken);
     // Based on the stored request token and the oauth verifier it should
     // get the user from the authentication service.
     $userId = 1;
     $tokenCredentials = new TokenCredentials('token2', 'secret2');
     $user = new User($userId, $tokenCredentials);
     $this->authService->expects($this->any())->method('getAccessToken')->with($this->requestToken, $oauthVerifier)->willReturn($user);
     // Afterwards it should remove the stored request token.
     $this->authService->expects($this->any())->method('removeStoredRequestToken');
     // Perform a fake request to the route with the query parameters.
     $query = ['oauth_token' => $this->requestToken->getToken(), 'oauth_verifier' => $oauthVerifier, 'destination' => $this->destination];
     $request = new Request($query);
     $response = $this->controller->authorize($request);
     // Make sure the response is a redirect to the destination that
     // was set in the query parameters.
     $this->assertEquals(new RedirectResponse($this->destination), $response);
     // Make sure that the minimal user info has been stored in the session.
     $this->assertEquals($this->userSessionService->getMinimalUserInfo(), $user);
     // Perform the fake request again, but this time without destination
     // parameter in the query.
     $this->urlGenerator->expects($this->once())->method('generate')->with($this->defaultDestination)->willReturn($this->defaultDestinationUrl);
     $query = ['oauth_token' => $this->requestToken->getToken(), 'oauth_verifier' => $oauthVerifier];
     $request = new Request($query);
     $response = $this->controller->authorize($request);
     // Make sure that the response now redirects to the default
     // destination.
     $this->assertEquals(new RedirectResponse($this->defaultDestinationUrl), $response);
 }