1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFr\ApiTesting; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests; |
6
|
|
|
|
7
|
|
|
trait AssertJsonResponse |
8
|
|
|
{ |
9
|
|
|
use AssertArrays, MakesHttpRequests; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Assert that the JSON response has exactly the given structure. |
13
|
|
|
* |
14
|
|
|
* @param array $structure |
15
|
|
|
* @param array|null $responseData |
16
|
|
|
* |
17
|
|
|
* @return $this |
18
|
|
|
*/ |
19
|
1 |
|
public function seeJsonStructureEquals(array $structure, $responseData = null) |
20
|
|
|
{ |
21
|
1 |
|
if (!$responseData) { |
22
|
1 |
|
$responseData = $this->decodeResponseJson(); |
23
|
1 |
|
} |
24
|
|
|
|
25
|
1 |
|
return $this->assertArrayStructureEquals($structure, $responseData); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Assert that the JSON response has a given typed structure. |
30
|
|
|
* |
31
|
|
|
* @param array|null $structure |
32
|
|
|
* @param array|null $responseData |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
1 |
|
public function seeJsonTypedStructure(array $structure = null, $responseData = null) |
36
|
|
|
{ |
37
|
1 |
|
if (is_null($structure)) { |
38
|
|
|
return $this->seeJsonStructure($structure); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
if (!$responseData) { |
42
|
1 |
|
$responseData = json_decode($this->response->getContent(), true); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
foreach ($structure as $key => $type) { |
46
|
1 |
|
if (is_array($type) && $key === '*') { |
47
|
|
|
$this->assertInternalType('array', $responseData); |
48
|
|
|
|
49
|
|
|
foreach ($responseData as $responseDataItem) { |
50
|
|
|
$this->seeJsonTypedStructure($structure['*'], $responseDataItem); |
51
|
|
|
} |
52
|
1 |
|
} elseif (is_array($type) && array_key_exists($key, $structure)) { |
53
|
1 |
|
if (is_array($structure[$key])) { |
54
|
1 |
|
$this->seeJsonTypedStructure($structure[$key], $responseData[$key]); |
55
|
1 |
|
} |
56
|
1 |
|
} else { |
57
|
1 |
|
$this->assertInternalType($type, $responseData[$key]); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return the json response or a part of it as an array. |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
1 |
|
public function jsonResponse($key = null) |
72
|
|
|
{ |
73
|
1 |
|
$response = $this->decodeResponseJson(); |
74
|
|
|
|
75
|
1 |
|
return $key ? array_get($response, $key) : $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function decodeResponseJson() |
79
|
|
|
{ |
80
|
2 |
|
$decodedResponse = json_decode($this->response->getContent(), true); |
81
|
|
|
|
82
|
2 |
|
if (is_null($decodedResponse) || $decodedResponse === false) { |
83
|
|
|
$this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?'); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return $decodedResponse; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.