Completed
Pull Request — master (#2)
by Mathieu
08:19
created

AssertArrays   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 77
ccs 35
cts 40
cp 0.875
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B assertArrayStructureEquals() 0 30 6
D seeJsonTypedStructure() 0 28 10
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);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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...
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);
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...
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);
0 ignored issues
show
Bug introduced by
It seems like seeJsonStructure() 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...
57
        }
58
59 1
        if (! $responseData) {
60 1
            $responseData = json_decode($this->response->getContent(), true);
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61 1
        }
62
63 1
        foreach ($structure as $key => $type) {
64 1
            if (is_array($type) && $key === '*') {
65
                $this->assertInternalType('array', $responseData);
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...
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]);
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...
76
            }
77 1
        }
78
79 1
        return $this;
80
    }
81
}
82