8000 [Refactor] Output JSON API errors if status code is not expected · Naxon/laravel-json-api@ac3bd82 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac3bd82

Browse files
committed
[Refactor] Output JSON API errors if status code is not expected
This returns the behaviour of response status checking that was in prior versions of this library, but which was removed by accident in v0.11.0.
1 parent 5aab3dd commit ac3bd82

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Testing/TestResponse.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Collection;
1313
use Neomerx\JsonApi\Contracts\Document\DocumentInterface as Keys;
1414
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
15+
use PHPUnit\Framework\Assert as PHPUnit;
1516
use RuntimeException;
1617

1718
class TestResponse extends BaseTestResponse
@@ -56,6 +57,26 @@ public function assertJsonApiResponse(
5657
return $this;
5758
}
5859

60+
/**
61+
* Assert that the response has the given status code.
62+
*
63+
* @param int $status
64+
* @return $this
65+
*/
66+
public function assertStatus($status)
67+
{
68+
$actual = $this->getStatusCode();
69+
$message = "Expected status code {$status} but received {$actual}";
70+
$content = (array) json_decode((string) $this->getContent(), true);
71+
if (isset($content[Keys::KEYWORD_ERRORS])) {
72+
$message .= " with errors:\n" . json_encode($content, JSON_PRETTY_PRINT);
73+
}
74+
75+
PHPUnit::assertSame($status, $actual, $message);
76+
77+
return $this;
78+
}
79+
5980
/**
6081
* Assert a response with a singular resource in the `data` member.
6182
*

0 commit comments

Comments
 (0)
0