1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VGirol\JsonApiAssert\Laravel\Asserts\Response; |
4
|
|
|
|
5
|
|
|
use Illuminate\Testing\TestResponse; |
6
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
7
|
|
|
use VGirol\JsonApiAssert\Laravel\HttpHeader; |
8
|
|
|
use VGirol\JsonApiAssert\Laravel\Messages; |
9
|
|
|
use VGirol\JsonApiConstant\Members; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This trait adds the ability to test response returned after resource creation. |
13
|
|
|
*/ |
14
|
|
|
trait AssertCreated |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Asserts that a response object is a valid "201 Created" response following a creation request. |
18
|
|
|
* |
19
|
|
|
* @param TestResponse $response |
20
|
|
|
* @param array $expected The expected newly created resource object |
21
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @throws \PHPUnit\Framework\AssertionFailedError |
25
|
|
|
*/ |
26
|
30 |
|
public static function assertIsCreatedResponse( |
27
|
|
|
TestResponse $response, |
28
|
|
|
$expected, |
29
|
|
|
bool $strict |
30
|
|
|
) { |
31
|
30 |
|
$response->assertStatus(201); |
32
|
27 |
|
$response->assertHeader( |
33
|
27 |
|
HttpHeader::HEADER_NAME, |
34
|
27 |
|
HttpHeader::MEDIA_TYPE |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// Decode JSON response |
38
|
24 |
|
$json = $response->json(); |
39
|
|
|
|
40
|
|
|
// Checks response structure |
41
|
24 |
|
static::assertHasValidStructure( |
42
|
24 |
|
$json, |
43
|
|
|
$strict |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
// Checks data member |
47
|
21 |
|
static::assertHasData($json); |
48
|
15 |
|
$data = $json[Members::DATA]; |
49
|
|
|
|
50
|
15 |
|
static::assertIsNotArrayOfObjects($data); |
51
|
|
|
|
52
|
12 |
|
static::assertResourceObjectEquals( |
53
|
12 |
|
$expected, |
54
|
|
|
$data |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
// Checks Location header |
58
|
12 |
|
$header = $response->headers->get('Location'); |
59
|
12 |
|
if (($header !== null) && isset($data[Members::LINKS][Members::LINK_SELF])) { |
60
|
6 |
|
PHPUnit::assertEquals( |
61
|
6 |
|
$header, |
62
|
6 |
|
$data[Members::LINKS][Members::LINK_SELF], |
63
|
6 |
|
Messages::LOCATION_HEADER_IS_NOT_AS_EXPECTED |
64
|
|
|
); |
65
|
|
|
} |
66
|
9 |
|
} |
67
|
|
|
} |
68
|
|
|
|