diff --git a/src/AcsfApi/AcsfClient.php b/src/AcsfApi/AcsfClient.php index fba9cf4f7..afeccd501 100644 --- a/src/AcsfApi/AcsfClient.php +++ b/src/AcsfApi/AcsfClient.php @@ -12,7 +12,7 @@ class AcsfClient extends Client { public function processResponse(ResponseInterface $response): mixed { $bodyJson = $response->getBody(); - $body = json_decode($bodyJson, FALSE, 512, JSON_THROW_ON_ERROR); + $body = json_decode((string) $bodyJson, FALSE, 512, JSON_THROW_ON_ERROR); // ACSF sometimes returns an array rather than an object. if (is_array($body)) { @@ -27,7 +27,7 @@ public function processResponse(ResponseInterface $response): mixed { throw new ApiErrorException($body); } // Throw error for 4xx and 5xx responses. - if (property_exists($body, 'message') && in_array(substr($response->getStatusCode(), 0, 1), [4, 5], TRUE)) { + if (property_exists($body, 'message') && in_array(substr((string) $response->getStatusCode(), 0, 1), ['4', '5'], TRUE)) { $body->error = $response->getStatusCode(); throw new ApiErrorException($body); } diff --git a/tests/phpunit/src/CloudApi/AcsfClientServiceTest.php b/tests/phpunit/src/CloudApi/AcsfClientServiceTest.php index a7436905c..c02549be2 100644 --- a/tests/phpunit/src/CloudApi/AcsfClientServiceTest.php +++ b/tests/phpunit/src/CloudApi/AcsfClientServiceTest.php @@ -9,9 +9,14 @@ use Acquia\Cli\AcsfApi\AcsfCredentials; use Acquia\Cli\DataStore\CloudDataStore; use Acquia\Cli\Tests\TestBase; +use AcquiaCloudApi\Exception\ApiErrorException; +use GuzzleHttp\Psr7\Response; class AcsfClientServiceTest extends TestBase { + protected string $apiSpecFixtureFilePath = __DIR__ . '/../../../../assets/acsf-spec.yaml'; + protected string $apiCommandPrefix = 'acsf'; + /** * @return array */ @@ -41,4 +46,39 @@ public function testIsMachineAuthenticated(array $envVars, bool $isAuthenticated self::unsetEnvVars($envVars); } + public function testEmbeddedItems(): void { + putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1'); + $cloudDatastore = $this->prophet->prophesize(CloudDataStore::class); + $clientService = new AcsfClientService(new AcsfConnectorFactory(['key' => NULL, 'secret' => NULL, 'accessToken' => NULL]), $this->application, new AcsfCredentials($cloudDatastore->reveal())); + $client = $clientService->getClient(); + $mockBody = ['_embedded' => ['items' => 'foo']]; + $response = new Response(200, [], json_encode($mockBody)); + $body = $client->processResponse($response); + $this->assertEquals('foo', $body); + } + + public function testErrorMessage(): void { + putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1'); + $cloudDatastore = $this->prophet->prophesize(CloudDataStore::class); + $clientService = new AcsfClientService(new AcsfConnectorFactory(['key' => NULL, 'secret' => NULL, 'accessToken' => NULL]), $this->application, new AcsfCredentials($cloudDatastore->reveal())); + $client = $clientService->getClient(); + $mockBody = ['error' => 'foo', 'message' => 'bar']; + $response = new Response(200, [], json_encode($mockBody)); + $this->expectException(ApiErrorException::class); + $this->expectExceptionMessage('bar'); + $client->processResponse($response); + } + + public function testErrorCode(): void { + putenv('ACQUIA_CLI_USE_CLOUD_API_SPEC_CACHE=1'); + $cloudDatastore = $this->prophet->prophesize(CloudDataStore::class); + $clientService = new AcsfClientService(new AcsfConnectorFactory(['key' => NULL, 'secret' => NULL, 'accessToken' => NULL]), $this->application, new AcsfCredentials($cloudDatastore->reveal())); + $client = $clientService->getClient(); + $mockBody = ['message' => 'bar']; + $response = new Response(400, [], json_encode($mockBody)); + $this->expectException(ApiErrorException::class); + $this->expectExceptionMessage('bar'); + $client->processResponse($response); + } + }