Completed
Pull Request — master (#56)
by
unknown
08:38
created

AssertRequestAgainstSchema::assertRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ByJG\ApiTools;
4
5
use ByJG\ApiTools\Base\Schema;
6
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
7
use ByJG\ApiTools\Exception\GenericSwaggerException;
8
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
9
use ByJG\ApiTools\Exception\InvalidDefinitionException;
10
use ByJG\ApiTools\Exception\NotMatchedException;
11
use ByJG\ApiTools\Exception\PathNotFoundException;
12
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException;
13
14
trait AssertRequestAgainstSchema
15
{
16
    /**
17
     * @var Schema
18
     */
19
    protected $schema;
20
21
    /**
22
     * configure the schema to use for requests
23
     *
24
     * When set, all requests without an own schema use this one instead.
25
     *
26
     * @param Schema|null $schema
27
     */
28
    public function setSchema($schema)
29
    {
30
        $this->schema = $schema;
31
    }
32
33
    /**
34
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
35
     * @param string $path The REST path call
36
     * @param int $statusExpected
37
     * @param array|null $query
38
     * @param array|null $requestBody
39
     * @param array $requestHeader
40
     * @return mixed
41
     * @throws DefinitionNotFoundException
42
     * @throws GenericSwaggerException
43
     * @throws HttpMethodNotFoundException
44
     * @throws InvalidDefinitionException
45
     * @throws NotMatchedException
46
     * @throws PathNotFoundException
47
     * @throws StatusCodeNotMatchedException
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     * @deprecated Use assertRequest instead
50
     */
51
    protected function makeRequest(
52
        $method,
53
        $path,
54
        $statusExpected = 200,
55
        $query = null,
56
        $requestBody = null,
57
        $requestHeader = []
58
    ) {
59
        $this->checkSchema();
60
        $requester = new ApiRequester();
61
        $body = $requester
62
            ->withSchema($this->schema)
63
            ->withMethod($method)
64
            ->withPath($path)
65
            ->withQuery($query)
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 55 can also be of type null; however, ByJG\ApiTools\AbstractRequester::withQuery() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody defined by parameter $requestBody on line 56 can also be of type array; however, ByJG\ApiTools\AbstractRequester::withRequestBody() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
67
            ->withRequestHeader($requestHeader)
68
            ->assertResponseCode($statusExpected)
69
            ->send();
70
71
        // Note:
72
        // This code is only reached if the send is successful and
73
        // all matches are satisfied. Otherwise an error is throwed before
74
        // reach this
75
        $this->assertTrue(true);
0 ignored issues
show
Bug introduced by
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...
76
77
        return $body;
78
    }
79
80
    /**
81
     * @param AbstractRequester $request
82
     * @return mixed
83
     * @throws DefinitionNotFoundException
84
     * @throws GenericSwaggerException
85
     * @throws HttpMethodNotFoundException
86
     * @throws InvalidDefinitionException
87
     * @throws NotMatchedException
88
     * @throws PathNotFoundException
89
     * @throws StatusCodeNotMatchedException
90
     * @throws \GuzzleHttp\Exception\GuzzleException
91
     */
92
    public function assertRequest(AbstractRequester $request)
93
    {
94
        // Add own schema if nothing is passed.
95
        if (!$request->hasSchema()) {
96
            $this->checkSchema();
97
            $request->withSchema($this->schema);
98
        }
99
100
        // Request based on the Swagger Request definitios
101
        $body = $request->send();
102
103
        // Note:
104
        // This code is only reached if the send is successful and
105
        // all matches are satisfied. Otherwise an error is throwed before
106
        // reach this
107
        $this->assertTrue(true);
0 ignored issues
show
Bug introduced by
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...
108
109
        return $body;
110
    }
111
112
    /**
113
     * @throws GenericSwaggerException
114
     */
115
    protected function checkSchema()
116
    {
117
        if (!$this->schema) {
118
            throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase');
119
        }
120
    }
121
}
122