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