Passed
Push — master ( f9279d...87874e )
by Joao
01:35 queued 11s
created

ApiTestCase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSchema() 0 4 1
A setRequester() 0 4 1
A getRequester() 0 7 2
A makeRequest() 0 27 1
A assertRequest() 0 19 2
A checkSchema() 0 6 2
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
use ByJG\Util\Psr7\MessageException;
14
use ByJG\Util\Psr7\Response;
15
use PHPUnit\Framework\TestCase;
16
17
abstract class ApiTestCase extends TestCase
18
{
19
    /**
20
     * @var Schema
21
     */
22
    protected $schema;
23
24
    /**
25
     * @var AbstractRequester
26
     */
27
    protected $requester = null;
28
29
    /**
30
     * configure the schema to use for requests
31
     *
32
     * When set, all requests without an own schema use this one instead.
33
     *
34
     * @param Schema|null $schema
35
     */
36
    public function setSchema($schema)
37
    {
38
        $this->schema = $schema;
39
    }
40
41
    public function setRequester(AbstractRequester $requester)
42
    {
43
        $this->requester = $requester;
44
    }
45
46
    /**
47
     * @return AbstractRequester
48
     */
49
    protected function getRequester()
50
    {
51
        if (is_null($this->requester)) {
52
            $this->requester = new ApiRequester();
53
        }
54
        return $this->requester;
55
    }
56
57
    /**
58
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
59
     * @param string $path The REST path call
60
     * @param int $statusExpected
61
     * @param array|null $query
62
     * @param array|null $requestBody
63
     * @param array $requestHeader
64
     * @return mixed
65
     * @throws DefinitionNotFoundException
66
     * @throws GenericSwaggerException
67
     * @throws HttpMethodNotFoundException
68
     * @throws InvalidDefinitionException
69
     * @throws NotMatchedException
70
     * @throws PathNotFoundException
71
     * @throws StatusCodeNotMatchedException
72
     * @throws MessageException
73
     * @deprecated Use assertRequest instead
74
     */
75
    protected function makeRequest(
76
        $method,
77
        $path,
78
        $statusExpected = 200,
79
        $query = null,
80
        $requestBody = null,
81
        $requestHeader = []
82
    ) {
83
        $this->checkSchema();
84
        $body = $this->requester
85
            ->withSchema($this->schema)
86
            ->withMethod($method)
87
            ->withPath($path)
88
            ->withQuery($query)
89
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody defined by parameter $requestBody on line 80 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...
90
            ->withRequestHeader($requestHeader)
91
            ->assertResponseCode($statusExpected)
92
            ->send();
93
94
        // Note:
95
        // This code is only reached if the send is successful and
96
        // all matches are satisfied. Otherwise an error is throwed before
97
        // reach this
98
        $this->assertTrue(true);
99
100
        return $body;
101
    }
102
103
    /**
104
     * @param AbstractRequester $request
105
     * @return Response
106
     * @throws DefinitionNotFoundException
107
     * @throws GenericSwaggerException
108
     * @throws HttpMethodNotFoundException
109
     * @throws InvalidDefinitionException
110
     * @throws NotMatchedException
111
     * @throws PathNotFoundException
112
     * @throws StatusCodeNotMatchedException
113
     * @throws MessageException
114
     */
115
    public function assertRequest(AbstractRequester $request)
116
    {
117
        // Add own schema if nothing is passed.
118
        if (!$request->hasSchema()) {
119
            $this->checkSchema();
120
            $request = $request->withSchema($this->schema);
121
        }
122
123
        // Request based on the Swagger Request definitios
124
        $body = $request->send();
125
126
        // Note:
127
        // This code is only reached if the send is successful and
128
        // all matches are satisfied. Otherwise an error is throwed before
129
        // reach this
130
        $this->assertTrue(true);
131
132
        return $body;
133
    }
134
135
    /**
136
     * @throws GenericSwaggerException
137
     */
138
    protected function checkSchema()
139
    {
140
        if (!$this->schema) {
141
            throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase');
142
        }
143
    }
144
}
145