Completed
Pull Request — master (#33)
by Joao
06:58
created

BaseTestCase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeRequest() 0 27 1
A assertRequest() 0 18 2
1
<?php
2
3
namespace ByJG\ApiTools\Base;
4
5
use ByJG\ApiTools\ApiRequester;
6
use ByJG\ApiTools\Base\Schema;
7
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
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\ApiTools\Exception\GenericSwaggerException;
14
use GuzzleHttp\GuzzleException;
15
use PHPUnit\Framework\TestCase;
16
17
abstract class BaseTestCase extends TestCase
18
{
19
    /**
20
     * @var Schema
21
     */
22
    protected $swaggerSchema;
23
24
    protected $filePath;
25
26
    /**
27
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
28
     * @param string $path The REST path call
29
     * @param int $statusExpected
30
     * @param array|null $query
31
     * @param array|null $requestBody
32
     * @param array $requestHeader
33
     * @return mixed
34
     * @throws DefinitionNotFoundException
35
     * @throws HttpMethodNotFoundException
36
     * @throws InvalidDefinitionException
37
     * @throws NotMatchedException
38
     * @throws PathNotFoundException
39
     * @throws StatusCodeNotMatchedException
40
     * @throws \GuzzleHttp\Exception\GuzzleException
41
     * @deprecated Use assertRequest instead
42
     */
43
    protected function makeRequest(
44
        $method,
45
        $path,
46
        $statusExpected = 200,
47
        $query = null,
48
        $requestBody = null,
49
        $requestHeader = []
50
    ) {
51
        $requester = new ApiRequester();
52
        $body = $requester
53
            ->withSwaggerSchema($this->swaggerSchema)
54
            ->withMethod($method)
55
            ->withPath($path)
56
            ->withQuery($query)
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 47 can also be of type null; however, ByJG\ApiTools\ApiRequester::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...
57
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody defined by parameter $requestBody on line 48 can also be of type array; however, ByJG\ApiTools\ApiRequester::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...
58
            ->withRequestHeader($requestHeader)
59
            ->assertResponseCode($statusExpected)
60
            ->send();
61
62
        // Note:
63
        // This code is only reached if the send is successful and
64
        // all matches are satisfied. Otherwise an error is throwed before
65
        // reach this
66
        $this->assertTrue(true);
67
68
        return $body;
69
    }
70
71
    /**
72
     * @param ApiRequester $request
73
     * @return mixed
74
     * @throws DefinitionNotFoundException
75
     * @throws HttpMethodNotFoundException
76
     * @throws InvalidDefinitionException
77
     * @throws NotMatchedException
78
     * @throws PathNotFoundException
79
     * @throws StatusCodeNotMatchedException
80
     * @throws \GuzzleHttp\Exception\GuzzleException
81
     */
82
    public function assertRequest(ApiRequester $request)
83
    {
84
        // Add own swagger if nothing is passed.
85
        if (!$request->hasSwaggerSchema()) {
86
            $request->withSwaggerSchema($this->swaggerSchema);
87
        }
88
89
        // Request based on the Swagger Request definitios
90
        $body = $request->send();
91
92
        // Note:
93
        // This code is only reached if the send is successful and
94
        // all matches are satisfied. Otherwise an error is throwed before
95
        // reach this
96
        $this->assertTrue(true);
97
98
        return $body;
99
    }
100
}
101