public function testAccessTokenError() { if (!function_exists('openssl_x509_read')) { $this->markTestSkipped('The "openssl_x509_read" function is not available.'); } else { $this->setExpectedException('Widop\\GoogleAnalytics\\GoogleAnalyticsException'); } $this->httpAdapterMock->expects($this->once())->method('postContent')->will($this->returnValue(json_encode(array('error' => 'error')))); $this->client->getAccessToken(); }
/** * @expectedException \RequestLab\XitiAnalytics\XitiException */ public function testQueryWithJsonError() { $this->clientMock->expects($this->once())->method('getHeaders')->will($this->returnValue(array('Content-Type' => 'application/json', 'Authorization' => 'Basic ' . base64_encode($this->login . ':' . $this->password)))); $this->queryMock->expects($this->once())->method('build')->will($this->returnValue('uri')); $this->httpAdapterMock->expects($this->once())->method('getContent')->with($this->equalTo('uri'))->will($this->returnValue(json_encode(array('ErrorCode' => '2017', 'ErrorMessage' => 'Le message')))); $this->service->query($this->queryMock); }
/** * @expectedException \Widop\GoogleAnalytics\GoogleAnalyticsException */ public function testQueryWithHtmlError() { $this->clientMock->expects($this->once())->method('getAccessToken')->will($this->returnValue('token')); $this->queryMock->expects($this->once())->method('build')->with($this->equalTo('token'))->will($this->returnValue('uri')); $this->httpAdapterMock->expects($this->once())->method('getContent')->with($this->equalTo('uri'))->will($this->returnValue('<html></html>')); $this->service->query($this->queryMock); }
/** * Gets the google OAuth access token. * * @throws \Widop\GoogleAnalytics\Exception\GoogleAnalyticsException If the access token can not be retrieved. * * @return string The access token. */ public function getAccessToken() { if ($this->accessToken === null) { $headers = array('Content-Type' => 'application/x-www-form-urlencoded'); $content = array('grant_type' => 'assertion', 'assertion_type' => 'http://oauth.net/grant_type/jwt/1.0/bearer', 'assertion' => $this->generateJsonWebToken()); $response = json_decode($this->httpAdapter->postContent($this->url, $headers, $content)); if (isset($response->error)) { throw GoogleAnalyticsException::invalidAccessToken($response->error); } $this->accessToken = $response->access_token; } return $this->accessToken; }