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

BaseTestCase::assertRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 $swaggerSchema;
24
25
    protected $filePath;
26
27
    /**
28
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
29
     * @param string $path The REST path call
30
     * @param int $statusExpected
31
     * @param array|null $query
32
     * @param array|null $requestBody
33
     * @param array $requestHeader
34
     * @return mixed
35
     * @throws DefinitionNotFoundException
36
     * @throws HttpMethodNotFoundException
37
     * @throws InvalidDefinitionException
38
     * @throws NotMatchedException
39
     * @throws PathNotFoundException
40
     * @throws StatusCodeNotMatchedException
41
     * @throws \GuzzleHttp\Exception\GuzzleException
42
     * @deprecated Use assertRequest instead
43
     */
44
    protected function makeRequest(
45
        $method,
46
        $path,
47
        $statusExpected = 200,
48
        $query = null,
49
        $requestBody = null,
50
        $requestHeader = []
51
    ) {
52
        $requester = new ApiRequester();
53
        $body = $requester
54
            ->withSwaggerSchema($this->swaggerSchema)
55
            ->withMethod($method)
56
            ->withPath($path)
57
            ->withQuery($query)
0 ignored issues
show
Bug introduced by
It seems like $query defined by parameter $query on line 48 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...
58
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody defined by parameter $requestBody on line 49 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...
59
            ->withRequestHeader($requestHeader)
60
            ->assertResponseCode($statusExpected)
61
            ->send();
62
63
        // Note:
64
        // This code is only reached if the send is successful and
65
        // all matches are satisfied. Otherwise an error is throwed before
66
        // reach this
67
        $this->assertTrue(true);
68
69
        return $body;
70
    }
71
72
    /**
73
     * @param AbstractRequester $request
74
     * @return mixed
75
     * @throws DefinitionNotFoundException
76
     * @throws HttpMethodNotFoundException
77
     * @throws InvalidDefinitionException
78
     * @throws NotMatchedException
79
     * @throws PathNotFoundException
80
     * @throws StatusCodeNotMatchedException
81
     * @throws \GuzzleHttp\Exception\GuzzleException
82
     */
83
    public function assertRequest(AbstractRequester $request)
84
    {
85
        // Add own swagger if nothing is passed.
86
        if (!$request->hasSwaggerSchema()) {
87
            $request->withSwaggerSchema($this->swaggerSchema);
88
        }
89
90
        // Request based on the Swagger Request definitios
91
        $body = $request->send();
92
93
        // Note:
94
        // This code is only reached if the send is successful and
95
        // all matches are satisfied. Otherwise an error is throwed before
96
        // reach this
97
        $this->assertTrue(true);
98
99
        return $body;
100
    }
101
}
102