Skip to content

Commit

Permalink
Support status response as array
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed May 7, 2022
1 parent b5c0185 commit 2651aba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Response/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

abstract class ApiResponse implements ArrayAccess
{
private ?ResponseInterface $response;
private ?ResponseInterface $response = null;

/**
* @return array<string,callable>
Expand Down
17 changes: 15 additions & 2 deletions src/Response/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ImboClient\Utils;
use Psr\Http\Message\ResponseInterface;

class Status
class Status extends ApiResponse
{
private DateTime $date;
private bool $databaseStatus;
Expand All @@ -22,7 +22,8 @@ public static function fromHttpResponse(ResponseInterface $response): self
{
/** @var array{date:string,database:bool,storage:bool} */
$body = Utils::convertResponseToArray($response);
return new self(new DateTime($body['date']), $body['database'], $body['storage']);
$status = new self(new DateTime($body['date']), $body['database'], $body['storage']);
return $status->withResponse($response);
}

public function getDate(): DateTime
Expand All @@ -44,4 +45,16 @@ public function isStorageHealthy(): bool
{
return $this->storageStatus;
}

protected function getArrayOffsets(): array
{
$response = $this->getResponse();
return [
'date' => fn (): DateTime => $this->getDate(),
'database' => fn (): bool => $this->isDatabaseHealthy(),
'storage' => fn (): bool => $this->isStorageHealthy(),
'status' => fn (): ?int => $response ? $response->getStatusCode() : null,
'message' => fn (): ?string => $response ? $response->getReasonPhrase() : null,
];
}
}
35 changes: 35 additions & 0 deletions tests/Response/StatusTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
namespace ImboClient\Response;

use DateTime;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -30,5 +31,39 @@ public function testCanCreateFromResponse(): void
$this->assertTrue($status->isDatabaseHealthy());
$this->assertFalse($status->isStorageHealthy());
$this->assertFalse($status->isHealthy());
$this->assertSame($response, $status->getResponse());
}

/**
* @covers ::offsetExists
* @covers ::offsetGet
* @covers ::getArrayOffsets
*/
public function testArrayAccess(): void
{
$response = $this->createConfiguredMock(ResponseInterface::class, [
'getBody' => $this->createConfiguredMock(StreamInterface::class, [
'getContents' => '{"date":"Mon, 20 Sep 2021 20:33:57 GMT","database":true,"storage":false}',
]),
'getStatusCode' => 503,
'getReasonPhrase' => 'storage error',
]);
$status = Status::fromHttpResponse($response);

$this->assertArrayHasKey('date', $status);
$this->assertArrayHasKey('database', $status);
$this->assertArrayHasKey('storage', $status);
$this->assertArrayHasKey('status', $status);
$this->assertArrayHasKey('message', $status);
$this->assertArrayNotHasKey('foobar', $status);

/** @var DateTime */
$date = $status['date'];

$this->assertSame(1632170037, $date->getTimestamp());
$this->assertTrue($status['database']);
$this->assertFalse($status['storage']);
$this->assertSame(503, $status['status']);
$this->assertSame('storage error', $status['message']);
}
}

0 comments on commit 2651aba

Please sign in to comment.