Passed
Pull Request — master (#33)
by Joao
01:37
created

BaseTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

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