| 1 | <?php |
||
| 2 | |||
| 3 | namespace VGirol\JsonApiAssert\Laravel\Asserts\Content; |
||
| 4 | |||
| 5 | use Illuminate\Testing\TestResponse; |
||
| 6 | use VGirol\JsonApiConstant\Members; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * This trait adds the ability to test jsonapi object. |
||
| 10 | */ |
||
| 11 | trait AssertJsonapiObject |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Asserts that a jsonapi object equals an expected array. |
||
| 15 | * |
||
| 16 | * @param TestResponse $response |
||
| 17 | * @param array $expected |
||
| 18 | * |
||
| 19 | * @return void |
||
| 20 | * @throws \PHPUnit\Framework\AssertionFailedError |
||
| 21 | */ |
||
| 22 | 18 | public static function assertResponseJsonapiObjectEquals(TestResponse $response, $expected) |
|
| 23 | { |
||
| 24 | 18 | if (!\is_array($expected)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | 3 | static::invalidArgument( |
|
| 26 | 3 | 2, |
|
| 27 | 3 | 'array', |
|
| 28 | $expected |
||
| 29 | ); |
||
| 30 | } |
||
| 31 | |||
| 32 | // Decode JSON response |
||
| 33 | 15 | $json = $response->json(); |
|
| 34 | |||
| 35 | 15 | static::assertHasMember(Members::JSONAPI, $json); |
|
| 36 | |||
| 37 | 9 | $jsonapi = $json[Members::JSONAPI]; |
|
| 38 | |||
| 39 | 9 | static::assertJsonapiObjectEquals($expected, $jsonapi); |
|
| 40 | 6 | } |
|
| 41 | } |
||
| 42 |