public function testGetOAuthCredentials()
 {
     $accessCode = 'demo access code';
     $accessToken = 'this is a demo access token';
     $redirectUri = 'http://github.coms';
     $oAuth2Info = ['demo' => 'info'];
     /** @var AdWordsUser $adWordsUser */
     $adWordsUser = $this->getMockBuilder(AdWordsUser::class)->disableOriginalConstructor()->getMock();
     /** @var OAuth2Handler $oAuth2Handler */
     $oAuth2Handler = $this->getMockBuilder(OAuth2Handler::class)->disableOriginalConstructor()->setMethods(['GetAuthorizationUrl', 'GetAccessToken', 'RefreshAccessToken'])->getMock();
     $adWordsUser->expects($this->at(0))->method('getOAuth2Handler')->willReturn($oAuth2Handler);
     $adWordsUser->expects($this->at(1))->method('getOauth2Info')->willReturn($oAuth2Info);
     $adWordsUser->expects($this->at(2))->method('setOauth2Info')->with($accessToken);
     $adWordsUser->expects($this->at(3))->method('getOauth2Info')->willReturn($oAuth2Info);
     $oAuth2Handler->expects($this->at(0))->method('GetAccessToken')->with($oAuth2Info, $accessCode, $redirectUri)->willReturn($accessToken);
     $result = $this->service->getOAuthCredentials($adWordsUser, $accessCode, $redirectUri);
     $this->assertSame($oAuth2Info, $result);
 }
 /**
  * Generate refresh token
  *
  * @param AdWordsService $adWordsService
  */
 public function fire(AdWordsService $adWordsService)
 {
     $user = new AdWordsUser();
     $authorizationUrl = $adWordsService->getOAuthAuthorizationUrl($user);
     // Post authorization URL
     $this->line(sprintf("Please sign in to your AdWords account, and open following url:\n%s", $authorizationUrl));
     // Retrieve token
     $accessToken = $this->ask('Insert your access token:');
     try {
         $oAuth2Info = $adWordsService->getOAuthCredentials($user, $accessToken);
     } catch (OAuth2Exception $e) {
         // OAuth2 error
         return $this->error('[OAUTH2]: ' . $e->getMessage());
     } catch (Exception $e) {
         return $this->error($e->getMessage());
     }
     if (!isset($oAuth2Info['refresh_token'])) {
         return $this->error('Error fetching the refresh token');
     }
     $this->comment('Insert the refresh token in your googleads configuration file (config/google-ads.php)');
     // Print refresh token
     $this->line(sprintf('Refresh token: "%s"', $oAuth2Info['refresh_token']));
 }