assertValidationErrorContain()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Apiato\Core\Traits\TestsTraits\PhpUnit;
4
5
use Illuminate\Support\Arr as LaravelArr;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Str as LaravelStr;
8
9
/**
10
 * Class TestsResponseHelperTrait
11
 *
12
 * Tests helper for making formatting and asserting http responses.
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16
trait TestsResponseHelperTrait
17
{
18
19
    /**
20
     * @param $keys
21
     */
22 View Code Duplication
    public function assertResponseContainKeys($keys)
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        if (!is_array($keys)) {
25
            $keys = (array)$keys;
26
        }
27
28
        $arrayResponse = $this->removeDataKeyFromResponse($this->getResponseContentArray());
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like getResponseContentArray() 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...
29
30
        foreach ($keys as $key) {
31
            $this->assertTrue(array_key_exists($key, $arrayResponse));
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like assertTrue() 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...
32
        }
33
    }
34
35
    /**
36
     * @param $values
37
     */
38 View Code Duplication
    public function assertResponseContainValues($values)
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        if (!is_array($values)) {
41
            $values = (array)$values;
42
        }
43
44
        $arrayResponse = $this->removeDataKeyFromResponse($this->getResponseContentArray());
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like getResponseContentArray() 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...
45
46
        foreach ($values as $value) {
47
            $this->assertTrue(in_array($value, $arrayResponse));
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like assertTrue() 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...
48
        }
49
    }
50
51
    /**
52
     * @param $data
53
     */
54
    public function assertResponseContainKeyValue($data)
55
    {
56
        // `responseContentToArray` will remove the `data` node
57
        $httpResponse = json_encode(LaravelArr::sortRecursive((array)$this->getResponseContentArray()));
58
59
        foreach (LaravelArr::sortRecursive($data) as $key => $value) {
60
            $expected = $this->formatToExpectedJson($key, $value);
61
            $this->assertTrue(LaravelStr::contains($httpResponse, $expected),
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like assertTrue() 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...
62
                "The JSON fragment [ {$expected} ] does not exist in the response [ {$httpResponse} ].");
63
        }
64
    }
65
66
    /**
67
     * @param array $messages
68
     */
69
    public function assertValidationErrorContain(array $messages)
70
    {
71
        $responseContent = $this->getResponseContentObject();
0 ignored issues
show
Bug introduced by Mahmoud Zalt
It seems like getResponseContentObject() 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...
72
73
        foreach ($messages as $key => $value) {
74
            $this->assertEquals($responseContent->errors->{$key}[0], $value);
0 ignored issues
show
Bug introduced by Mahmoud Zalt
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...
75
        }
76
    }
77
78
    /**
79
     * @param $key
80
     * @param $value
81
     *
82
     * @return  string
83
     */
84
    private function formatToExpectedJson($key, $value)
85
    {
86
        $expected = json_encode([$key => $value]);
87
88
        if (Str::startsWith($expected, '{')) {
89
            $expected = substr($expected, 1);
90
        }
91
92
        if (Str::endsWith($expected, '}')) {
93
            $expected = substr($expected, 0, -1);
94
        }
95
96
        return trim($expected);
97
    }
98
99
    /**
100
     * @param array $responseContent
101
     *
102
     * @return  array|mixed
103
     */
104
    private function removeDataKeyFromResponse(array $responseContent)
105
    {
106
        if (array_key_exists('data', $responseContent)) {
107
            return $responseContent['data'];
108
        }
109
110
        return $responseContent;
111
    }
112
113
}
114