Completed
Push — master ( f45263...475295 )
by Chad
04:30
created

ArrayAssertsTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 62
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertSameArray() 0 50 3
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->assertInternalType('array', $actual, '$actual was not an array');
0 ignored issues
show
Bug introduced by
It seems like assertInternalType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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(
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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(
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
42
            0,
43
            $unexpectedKeys,
44
            sprintf(
45
                '$actual 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 ($expectedArray as $key => $value) {
0 ignored issues
show
Bug introduced by
The variable $expectedArray does not exist. Did you mean $expected?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
54
            if (!is_array($value)) {
55
                $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}.");
56
            }
57
58
            $this->assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Chadicus\ArrayAssertsTrait. Did you maybe mean assertSameArray()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
                $value,
60
                $actual[$key],
61
                sprintf(
62
                    "{$prefix}{$key} value is not correct expected %s\nfound %s",
63
                    var_export($value, 1),
64
                    var_export($actual[$key], 1)
65
                )
66
            );
67
        }
68
    }
69
}
70