1 | <?php |
||
2 | |||
3 | namespace Chadicus; |
||
4 | |||
5 | /** |
||
6 | * Trait for adding asserts for arrays |
||
7 | */ |
||
8 | trait ArrayAssertsTrait |
||
9 | { |
||
10 | /** |
||
11 | * Asserts the given $actual array is the same as the $expected array disregarding index order |
||
12 | * |
||
13 | * @param array $expected The expected array. |
||
14 | * @param mixed $actual The actual array. |
||
15 | * @param string|null $prefix Prefix to use with error messages. Useful for nested arrays. |
||
16 | * |
||
17 | * @return void |
||
18 | */ |
||
19 | public function assertSameArray(array $expected, $actual, $prefix = null) |
||
20 | { |
||
21 | //assert that the actual value is an array |
||
22 | $this->assertIsArray($actual, '$actual was not an array'); |
||
23 | |||
24 | $expectedKeys = array_keys($expected); |
||
25 | $actualKeys = array_keys($actual); |
||
26 | |||
27 | //find any keys in the expected array that are not present in the actual array |
||
28 | $missingExpectedKeys = array_diff($expectedKeys, $actualKeys); |
||
29 | $this->assertCount( |
||
30 | 0, |
||
31 | $missingExpectedKeys, |
||
32 | sprintf( |
||
33 | '$actual array is missing %d keys: %s', |
||
34 | count($missingExpectedKeys), |
||
35 | implode(', ', $missingExpectedKeys) |
||
36 | ) |
||
37 | ); |
||
38 | |||
39 | //find any keys in the actual array that are not expected in the expected array |
||
40 | $unexpectedKeys = array_diff($actualKeys, $expectedKeys); |
||
41 | $this->assertCount( |
||
42 | 0, |
||
43 | $unexpectedKeys, |
||
44 | sprintf( |
||
45 | '$actual array contains %d unexpected keys: %s', |
||
46 | count($unexpectedKeys), |
||
47 | implode(', ', $unexpectedKeys) |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | //Assert all values are the same value and type. |
||
52 | //Recursively call assertSameArray on array values |
||
53 | foreach ($expected as $key => $value) { |
||
54 | if (is_array($value)) { |
||
55 | $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}."); |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | $this->assertSame( |
||
60 | $value, |
||
61 | $actual[$key], |
||
62 | sprintf( |
||
63 | "{$prefix}{$key} value is not correct expected %s\nfound %s", |
||
64 | var_export($value, 1), |
||
0 ignored issues
–
show
|
|||
65 | var_export($actual[$key], 1) |
||
0 ignored issues
–
show
Are you sure the usage of
var_export($actual[$key], 1) is correct as it seems to always return null .
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
66 | ) |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Asserts the number of elements of an array, Countable or Traversable. |
||
73 | * |
||
74 | * Ensures this method must be provided by classes using this trait. |
||
75 | * |
||
76 | * @param integer $expectedCount The expected number of items in $haystack. |
||
77 | * @param mixed $haystack The array, countable or traversable object containing items. |
||
78 | * @param string $message Optional error message to give upon failure. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | abstract public static function assertCount(int $expectedCount, $haystack, string $message = ''); |
||
83 | |||
84 | /** |
||
85 | * Asserts that a variable is an Array. |
||
86 | * |
||
87 | * Ensures this method must be provided by classes using this trait. |
||
88 | * |
||
89 | * @param mixed $actual The variable to verify. |
||
90 | * @param string $message Optional error message to give upon failure. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | abstract public static function assertIsArray($actual, string $message = ''); |
||
95 | |||
96 | /** |
||
97 | * Asserts that two variables have the same type and value. Used on objects, it asserts that two variables reference |
||
98 | * the same object. |
||
99 | * |
||
100 | * Ensures this method must be provided by classes using this trait. |
||
101 | * |
||
102 | * @param string|array|object $expected The expected value. |
||
103 | * @param mixed $actual The actual value. |
||
104 | * @param string $message Optional error message to give upon failure. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | abstract public static function assertSame($expected, $actual, string $message = ''); |
||
109 | } |
||
110 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.