Skip to content

Commit

Permalink
Handle malformed JSON (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fefas authored Jun 16, 2020
1 parent 7368559 commit 01b5c53
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/Exceptions/MalformedJsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bauhaus\AssertPsrResponse\Exceptions;

use InvalidArgumentException;
use Throwable;

class MalformedJsonException extends InvalidArgumentException
{
public function __construct(string $malformedJson, Throwable $previousEx)
{
$message = "Malformed JSON provided:\n$malformedJson";
$code = 0;

parent::__construct($message, $code, $previousEx);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Bauhaus\AssertPsrResponse;
namespace Bauhaus\AssertPsrResponse\Exceptions;

use RuntimeException;

Expand Down
24 changes: 17 additions & 7 deletions src/Matchers/JsonBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

namespace Bauhaus\AssertPsrResponse\Matchers;

use Bauhaus\AssertPsrResponse\Exceptions\MalformedJsonException;
use Psr\Http\Message\ResponseInterface as PsrResponse;
use Fefas\Jsoncoder\Json;
use InvalidArgumentException;

class JsonBody implements Matcher
final class JsonBody implements Matcher
{
private const MISMATCH_MESSAGE =
'Actual response json body \'%s\' is not equal to the expected \'%s\'';

private string $expected;
private Json $expected;

public function __construct(string $expected)
private function __construct(string $expected)
{
$this->expected = $expected;
$this->expected = $this->handleJson($expected);
}

public function match(PsrResponse $psrResponse): bool
{
$expected = Json::createFromString($this->expected);
$actual = Json::createFromString($this->extractBody($psrResponse));
$actual = $this->handleJson($this->extractBody($psrResponse));

return $expected->isEqualTo($actual);
return $this->expected->isEqualTo($actual);
}

public function mismatchMessage(PsrResponse $psrResponse): string
Expand All @@ -39,4 +40,13 @@ private function extractBody(PsrResponse $psrResponse): string
{
return (string) $psrResponse->getBody();
}

private function handleJson(string $jsonString): Json
{
try {
return Json::createFromString($jsonString);
} catch (InvalidArgumentException $ex) {
throw new MalformedJsonException($jsonString, $ex);
}
}
}
1 change: 1 addition & 0 deletions src/PsrResponseAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bauhaus\AssertPsrResponse;

use Bauhaus\AssertPsrResponse\Exceptions\PsrResponseAssertionException;
use Bauhaus\AssertPsrResponse\Matchers\Matcher;
use Psr\Http\Message\ResponseInterface as PsrResponse;

Expand Down
24 changes: 24 additions & 0 deletions tests/Matchers/JsonBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bauhaus\AssertPsrResponse\Matchers;

use Bauhaus\AssertPsrResponse\Exceptions\MalformedJsonException;
use Bauhaus\AssertPsrResponse\TestCase;

class JsonBodyTest extends TestCase
Expand Down Expand Up @@ -45,4 +46,27 @@ public function returnMismatchMessageAccordingToExpectedAndActualValues(): void
$expected = 'Actual response json body \'[1,2,3]\' is not equal to the expected \'[1,2]\'';
$this->assertEquals($expected, $mismatchMessage);
}

/**
* @test
*/
public function throwExceptionIfAMalformedJsonIsProvidedAsExpectedValue(): void
{
$this->expectException(MalformedJsonException::class);

JsonBody::equalTo('[1,2');
}

/**
* @test
*/
public function throwExceptionIfResponseToAssertHasAMalformedJson(): void
{
$jsonBodyMatcher = JsonBody::equalTo('[1,2]');
$responseToAssert = $this->responseWithJsonBody('[1,2,3');

$this->expectException(MalformedJsonException::class);

$jsonBodyMatcher->match($responseToAssert);
}
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bauhaus\AssertPsrResponse;

use Bauhaus\AssertPsrResponse\Exceptions\PsrResponseAssertionException;
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\StreamInterface as Stream;
Expand Down

0 comments on commit 01b5c53

Please sign in to comment.