Skip to content

Commit

Permalink
Merge pull request #63 from keboola/COM-898-ondra-get-empty-profiles-…
Browse files Browse the repository at this point in the history
…properties

Fix empty profiles/properties
  • Loading branch information
ondrajodas authored Jun 21, 2021
2 parents 656a3a7 + 9f67587 commit 2e14deb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/GoogleAnalytics/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,45 @@ public function getSegments(): array
{
$response = $this->api->request(self::SEGMENTS_URL);
$body = json_decode($response->getBody()->getContents(), true);
return $body['items'];
return $body['items'] ?? [];
}

public function getAccountProperties(): array
{
$response = $this->api->request(self::ACCOUNT_PROPERTIES_URL);
$body = json_decode($response->getBody()->getContents(), true);
return array_filter($body['accountSummaries'], fn(array $v) => isset($v['propertySummaries']));
if (isset($body['accountSummaries'])) {
return array_filter($body['accountSummaries'], fn(array $v) => isset($v['propertySummaries']));
}
return [];
}

public function getAccounts(): array
{
$response = $this->api->request(self::ACCOUNTS_URL);
$body = json_decode($response->getBody()->getContents(), true);
return $body['items'];
return $body['items'] ?? [];
}

public function getWebProperties(): array
{
$response = $this->api->request(self::ACCOUNT_WEB_PROPERTIES_URL);
$body = json_decode($response->getBody()->getContents(), true);
return $body['items'];
return $body['items'] ?? [];
}

public function getAccountProfiles(): array
{
$response = $this->api->request(self::ACCOUNT_PROFILES_URL);
$body = json_decode($response->getBody()->getContents(), true);
return $body['items'];
return $body['items'] ?? [];
}

public function getCustomMetrics(int $accountId, string $webPropertyId): array
{
$response = $this->api->request(sprintf(self::CUSTOM_METRICS_URL, $accountId, $webPropertyId));
$body = json_decode($response->getBody()->getContents(), true);
return $body['items'];
return $body['items'] ?? [];
}

public function request(string $method, string $url, ?array $body = null, ?array $query = null): array
Expand Down
36 changes: 36 additions & 0 deletions tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,37 @@ public function testGetProfilesProperties(): void
);
}

public function testGetEmptyProfilesProperties(): void
{
$restApi = $this->createMock(RestApi::class);
$restApi
->method('request')
->with($this->logicalOr(
Client::ACCOUNT_PROPERTIES_URL,
Client::ACCOUNT_WEB_PROPERTIES_URL,
Client::ACCOUNT_PROFILES_URL,
Client::ACCOUNTS_URL
))
->will($this->returnCallback(array($this, 'returnMockServerRequestEmptyResponse')))
;

$logger = new NullLogger();
$client = new Client(
$restApi,
$logger
);
$output = new Output($this->dataDir);
$extractor = new Extractor($client, $output, $logger);

$this->assertEquals(
[
'profiles' => [],
'properties' => [],
],
$extractor->getProfilesPropertiesAction()
);
}

public function returnMockServerRequest(string $url): Response
{
/** @phpcs:disable */
Expand Down Expand Up @@ -273,6 +304,11 @@ public function returnMockServerRequest(string $url): Response
return new Response(200, [], '');
}

public function returnMockServerRequestEmptyResponse(string $url): Response
{
return new Response(200, [], '');
}

private function getOutputFiles(string $queryName): Finder
{
$finder = new Finder();
Expand Down

0 comments on commit 2e14deb

Please sign in to comment.