Skip to content

Commit

Permalink
properly instantiate SeatsioException when no requestId is present (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bverbeken committed Feb 4, 2022
1 parent d92d4f2 commit e1f9c3e
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/SeatsioException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Seatsio;

use GuzzleHttp\Utils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
Expand Down Expand Up @@ -52,10 +53,10 @@ private static function extractInfo($response)
{
$contentType = $response->getHeaderLine("content-type");
if (strpos($contentType, 'application/json') !== false) {
$json = \GuzzleHttp\json_decode($response->getBody());
$json = Utils::jsonDecode($response->getBody());
$mapper = SeatsioJsonMapper::create();
$errors = $mapper->mapArray($json->errors, array(), 'Seatsio\ApiError');
return ["messages" => $json->messages, "errors" => $errors, "requestId" => $json->requestId];
return ["messages" => $json->messages, "errors" => $errors, "requestId" => $json->requestId ?? null];
}
return ["messages" => [], "errors" => [], "requestId" => null];
}
Expand Down
175 changes: 175 additions & 0 deletions tests/SeatsioExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Seatsio;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

class SeatsioExceptionTest extends SeatsioClientTest
{

public function testCanInstantiateSeatsioExceptionWithoutRequestId()
{
$request = new DummyRequest();
$response = new DummyResponse(
"application/json",
"{\"errors\": [], \"messages\":[]}"
);
$exception = new SeatsioException($request, $response);
self::assertNull($exception->requestId);
}
}


class DummyRequest implements RequestInterface
{

public function __construct()
{
}

public function getProtocolVersion()
{
}

public function withProtocolVersion($version)
{
}

public function getHeaders()
{
}

public function hasHeader($name)
{
}

public function getHeader($name)
{
}

public function getHeaderLine($name)
{
}

public function withHeader($name, $value)
{
}

public function withAddedHeader($name, $value)
{
}

public function withoutHeader($name)
{
}

public function getBody()
{
}

public function withBody(StreamInterface $body)
{
}

public function getRequestTarget()
{
}

public function withRequestTarget($requestTarget)
{
}

public function getMethod()
{
}

public function withMethod($method)
{
}

public function getUri()
{
}

public function withUri(UriInterface $uri, $preserveHost = false)
{
}
}

class DummyResponse implements ResponseInterface
{


private $contentType;
private $body;

public function __construct(string $contentType, string $body)
{
$this->contentType = $contentType;
$this->body = $body;
}

public function getProtocolVersion()
{
}

public function withProtocolVersion($version)
{
}

public function getHeaders()
{
}

public function hasHeader($name)
{
}

public function getHeader($name)
{
}

public function getHeaderLine($name)
{
if ($name === "content-type") {
return $this->contentType;
}
return null;
}

public function withHeader($name, $value)
{
}

public function withAddedHeader($name, $value)
{
}

public function withoutHeader($name)
{
}

public function getBody()
{
return $this->body;
}

public function withBody(StreamInterface $body)
{
}

public function getStatusCode()
{
}

public function withStatus($code, $reasonPhrase = '')
{
}

public function getReasonPhrase()
{
}
}

0 comments on commit e1f9c3e

Please sign in to comment.