diff --git a/src/GoogleAnalytics/Client.php b/src/GoogleAnalytics/Client.php index 52045ea..f35c568 100644 --- a/src/GoogleAnalytics/Client.php +++ b/src/GoogleAnalytics/Client.php @@ -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 diff --git a/tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php b/tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php index 4f3e4e5..2394396 100644 --- a/tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php +++ b/tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php @@ -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 */ @@ -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();