-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssertJsonResponse.php
87 lines (73 loc) · 2.26 KB
/
AssertJsonResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace LaravelFr\ApiTesting;
use Illuminate\Foundation\Testing\TestResponse;
trait AssertJsonResponse
{
use AssertArrays;
public function __construct()
{
if (class_exists(TestResponse::class)) {
$this->addJsonResponseMacros();
}
}
/**
* Add the functions to the TestResponse objects.
*/
public function addJsonResponseMacros()
{
$trait = $this;
TestResponse::macro('assertJsonStructureEquals', function ($structure) use ($trait) {
return $trait->assertJsonStructureEquals($structure, $this->decodeResponseJson());
});
TestResponse::macro('seeJsonTypedStructure', function ($structure) use ($trait) {
return $trait->seeJsonTypedStructure($structure, $this->decodeResponseJson());
});
TestResponse::macro('jsonResponse', function ($key = null) use ($trait) {
return $trait->jsonResponse($key, $this->decodeResponseJson());
});
}
/**
* Assert that the JSON response has exactly the given structure.
*
* @param array $structure
* @param array|null $responseData
*
* @return $this
*/
public function assertJsonStructureEquals($structure, $responseData = null)
{
if (!$responseData) {
$responseData = $this->decodeResponseJson();
}
return $this->assertArrayStructureEquals($structure, $responseData);
}
/**
* Assert that the JSON response has a given typed structure.
*
* @param array $structure
* @param array|null $responseData
* @return $this
*/
public function seeJsonTypedStructure($structure, $responseData = null)
{
if (!$responseData) {
$responseData = $this->decodeResponseJson();
}
return $this->seeArrayTypedStructure($structure, $responseData);
}
/**
* Return the json response or a part of it as an array.
*
* @param string $key
* @param array|null $responseData
*
* @return mixed
*/
public function jsonResponse($key = null, $responseData = null)
{
if (!$responseData) {
$responseData = $this->decodeResponseJson();
}
return array_get($responseData, $key);
}
}