Skip to content

Commit

Permalink
CLI-1086: ACSF API typeerror (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
danepowell authored Jun 26, 2023
1 parent 1f8bf21 commit d936cc3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/AcsfApi/AcsfClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/src/CloudApi/AcsfClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>
*/
Expand Down Expand Up @@ -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);
}

}

0 comments on commit d936cc3

Please sign in to comment.