1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFr\ApiTesting; |
4
|
|
|
|
5
|
|
|
trait AssertArrays |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Assert that the given array has exactly the given structure. |
9
|
|
|
* |
10
|
|
|
* @param array $structure |
11
|
|
|
* @param array $array |
12
|
|
|
* |
13
|
|
|
* @return $this |
14
|
|
|
*/ |
15
|
2 |
|
public function assertArrayStructureEquals(array $structure, array $array) |
16
|
|
|
{ |
17
|
|
|
$structureFirstLevel = array_map(function ($value, $key) { |
18
|
2 |
|
return is_array($value) ? $key : $value; |
19
|
2 |
|
}, $structure, array_keys($structure)); |
20
|
|
|
|
21
|
2 |
|
$responseFirstLevel = array_keys($array); |
22
|
|
|
|
23
|
2 |
|
if ($structureFirstLevel !== ['*']) { |
24
|
2 |
|
$this->assertEquals($structureFirstLevel, $responseFirstLevel, '', 0.0, 10, true); |
|
|
|
|
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
$structureOtherLevels = array_filter($structure, function ($value) { |
28
|
2 |
|
return is_array($value); |
29
|
2 |
|
}); |
30
|
|
|
|
31
|
2 |
|
foreach ($structureOtherLevels as $key => $childStructure) { |
32
|
2 |
|
if ($key === '*') { |
33
|
2 |
|
$this->assertInternalType('array', $array); |
|
|
|
|
34
|
|
|
|
35
|
2 |
|
foreach ($array as $responseDataItem) { |
36
|
2 |
|
$this->assertArrayStructureEquals($childStructure, $responseDataItem); |
37
|
2 |
|
} |
38
|
2 |
|
} else { |
39
|
2 |
|
$this->assertArrayStructureEquals($childStructure, $array[$key]); |
40
|
|
|
} |
41
|
2 |
|
} |
42
|
|
|
|
43
|
2 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Assert that the JSON response has a given typed structure. |
48
|
|
|
* |
49
|
|
|
* @param array|null $structure |
50
|
|
|
* @param array|null $responseData |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
1 |
|
public function seeJsonTypedStructure(array $structure = null, $responseData = null) |
54
|
|
|
{ |
55
|
1 |
|
if (is_null($structure)) { |
56
|
|
|
return $this->seeJsonStructure($structure); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if (! $responseData) { |
60
|
1 |
|
$responseData = json_decode($this->response->getContent(), true); |
|
|
|
|
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
foreach ($structure as $key => $type) { |
64
|
1 |
|
if (is_array($type) && $key === '*') { |
65
|
|
|
$this->assertInternalType('array', $responseData); |
|
|
|
|
66
|
|
|
|
67
|
|
|
foreach ($responseData as $responseDataItem) { |
68
|
|
|
$this->seeJsonTypedStructure($structure['*'], $responseDataItem); |
69
|
|
|
} |
70
|
1 |
|
} elseif (is_array($type) && array_key_exists($key, $structure)) { |
71
|
1 |
|
if (is_array($structure[$key])) { |
72
|
1 |
|
$this->seeJsonTypedStructure($structure[$key], $responseData[$key]); |
73
|
1 |
|
} |
74
|
1 |
|
} else { |
75
|
1 |
|
$this->assertInternalType($type, $responseData[$key]); |
|
|
|
|
76
|
|
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.