This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace VGirol\JsonApiAssert\Laravel; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Testing\Assert; | ||
use Illuminate\Testing\TestResponse as IlluminateTestResponse; | ||
|
||
class TestResponse extends IlluminateTestResponse | ||
{ | ||
public const ERROR_STATUS = 'Expected status code %d but received %d.'; | ||
|
||
public function assertStatus($status) | ||
{ | ||
$actual = $this->getStatusCode(); | ||
|
||
$message = sprintf(self::ERROR_STATUS, $status, $actual); | ||
if ($actual >= 300) { | ||
$data = $this->baseResponse->getData(true); | ||
if (Arr::exists($data, 'errors')) { | ||
$errors = Arr::get($data, 'errors'); | ||
if (count($errors)) { | ||
$error = Arr::first($errors); | ||
if (Arr::exists($error, 'details')) { | ||
$message .= "\n" . Arr::get($error, 'details'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Assert::assertSame( | ||
$actual, | ||
$status, | ||
$message | ||
); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace VGirol\JsonApiAssert\Laravel; | ||
|
||
trait UseJsonapiTestResponse | ||
{ | ||
protected function createTestResponse($response) | ||
{ | ||
return TestResponse::fromBaseResponse($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace VGirol\JsonApiAssert\Laravel\Tests\Macros\Response; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use PHPUnit\Framework\Assert as PHPUnit; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use VGirol\JsonApiAssert\Laravel\HttpHeader; | ||
use VGirol\JsonApiAssert\Laravel\TestResponse; | ||
use VGirol\JsonApiAssert\Laravel\Tests\TestCase; | ||
use VGirol\JsonApiAssert\Laravel\UseJsonapiTestResponse; | ||
use VGirol\JsonApiConstant\Members; | ||
use VGirol\JsonApiFaker\Laravel\Generator; | ||
|
||
class TestResponseTest extends TestCase | ||
{ | ||
use UseJsonapiTestResponse; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function assertStatusSucceed() | ||
{ | ||
$headers = [ | ||
HttpHeader::HEADER_NAME => [HttpHeader::MEDIA_TYPE] | ||
]; | ||
|
||
$actual = 204; | ||
$expected = 204; | ||
$doc = (new Generator)->document(); | ||
|
||
$response = $this->createTestResponse( | ||
JsonResponse::create($doc->toArray(), $actual, $headers) | ||
); | ||
|
||
$obj = $response->assertStatus($expected); | ||
|
||
PHPUnit::assertSame($response, $obj); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function assertStatusFailed() | ||
{ | ||
$headers = [ | ||
HttpHeader::HEADER_NAME => [HttpHeader::MEDIA_TYPE] | ||
]; | ||
|
||
$actual = 204; | ||
$expected = 200; | ||
|
||
$doc = (new Generator)->document(); | ||
|
||
$message = sprintf(TestResponse::ERROR_STATUS, $expected, $actual); | ||
|
||
$response = $this->createTestResponse( | ||
JsonResponse::create($doc->toArray(), $actual, $headers) | ||
); | ||
|
||
$this->setFailure(ExpectationFailedException::class, $message); | ||
|
||
$response->assertStatus($expected); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider assertStatusFailedWithErrorMessageProvider | ||
*/ | ||
public function assertStatusFailedWithErrorMessage($actual) | ||
{ | ||
$headers = [ | ||
HttpHeader::HEADER_NAME => [HttpHeader::MEDIA_TYPE] | ||
]; | ||
|
||
$expected = 204; | ||
|
||
$errorFactory = (new Generator)->error() | ||
->fake() | ||
->set(Members::ERROR_STATUS, strval($actual)) | ||
->set(Members::ERROR_TITLE, JsonResponse::$statusTexts[$actual]) | ||
->set(Members::ERROR_DETAILS, 'test'); | ||
$doc = (new Generator)->document()->AddError($errorFactory); | ||
|
||
$message = sprintf(TestResponse::ERROR_STATUS, $expected, $actual) . "\ntest"; | ||
|
||
$response = $this->createTestResponse( | ||
JsonResponse::create($doc->toArray(), $actual, $headers) | ||
); | ||
|
||
$this->setFailure(ExpectationFailedException::class, $message); | ||
|
||
$response->assertStatus($expected); | ||
} | ||
|
||
public function assertStatusFailedWithErrorMessageProvider() | ||
{ | ||
return [ | ||
[300], | ||
[400] | ||
]; | ||
} | ||
} |