Completed
Push — master ( 01d609...f0430d )
by Mathieu
11s
created

AssertArrays   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 70
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B assertArrayStructureEquals() 0 30 6
B seeArrayTypedStructure() 0 20 8
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 3
    public function assertArrayStructureEquals(array $structure, array $array)
18
    {
19
        $structureFirstLevel = array_map(function ($value, $key) {
20 3
            return is_array($value) ? $key : $value;
21 3
        }, $structure, array_keys($structure));
22
23 3
        $responseFirstLevel = array_keys($array);
24
25 3
        if ($structureFirstLevel !== ['*']) {
26 3
            PHPUnit::assertEquals($structureFirstLevel, $responseFirstLevel, '', 0.0, 10, true);
27
        }
28
29 3
        $structureOtherLevels = array_filter($structure, function ($value) {
30 3
            return is_array($value);
31 3
        });
32
33 3
        foreach ($structureOtherLevels as $key => $childStructure) {
34 3
            if ($key === '*') {
35 3
                PHPUnit::assertInternalType('array', $array);
36
37 3
                foreach ($array as $responseDataItem) {
38 3
                    $this->assertArrayStructureEquals($childStructure, $responseDataItem);
39
                }
40
            } else {
41 3
                $this->assertArrayStructureEquals($childStructure, $array[$key]);
42
            }
43
        }
44
45 3
        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 3
    public function seeArrayTypedStructure(array $structure, array $array)
57
    {
58 3
        foreach ($structure as $key => $type) {
59 3
            if (is_array($type) && $key === '*') {
60 3
                PHPUnit::assertInternalType('array', $array);
61
62 3
                foreach ($array as $arrayItem) {
63 3
                    $this->seeArrayTypedStructure($structure['*'], $arrayItem);
64
                }
65 3
            } elseif (is_array($type) && isset($structure[$key])) {
66 3
                if (is_array($structure[$key])) {
67 3
                    $this->seeArrayTypedStructure($structure[$key], $array[$key]);
68
                }
69
            } else {
70 3
                PHPUnit::assertInternalType($type, $array[$key]);
71
            }
72
        }
73
74 3
        return $this;
75
    }
76
}
77