Passed
Pull Request — master (#33)
by Joao
08:26
created

ApiTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

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