Completed
Push — master ( f21c98...d36dae )
by Joao Gilberto
11s
created

SwaggerTestCase::assertRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by Joao Gilberto Magalhaes
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace ByJG\Swagger;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\BadResponseException;
7
use GuzzleHttp\Psr7\Request;
8
use PHPUnit\Framework\TestCase;
9
10
// backward compatibility
11
if (!class_exists('\PHPUnit\Framework\TestCase')) {
12
    class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
13
}
14
15
abstract class SwaggerTestCase extends TestCase
16
{
17
    /**
18
     * @var \ByJG\Swagger\SwaggerSchema
19
     */
20
    protected $swaggerSchema;
21
22
    protected $filePath;
23
24
    /**
25
     * @throws \Exception
26
     */
27
    protected function setUp()
28
    {
29
        if (empty($this->filePath)) {
30
            throw new \Exception('You have to define the property $filePath');
31
        }
32
33
        $this->swaggerSchema = new SwaggerSchema(file_get_contents($this->filePath));
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
     * @deprecated Use assertRequest instead
45
     * @throws \ByJG\Swagger\Exception\HttpMethodNotFoundException
46
     * @throws \ByJG\Swagger\Exception\InvalidDefinitionException
47
     * @throws \ByJG\Swagger\Exception\NotMatchedException
48
     * @throws \ByJG\Swagger\Exception\PathNotFoundException
49
     * @throws \ByJG\Swagger\Exception\RequiredArgumentNotFound
50
     * @throws \Exception
51
     * @throws \GuzzleHttp\Exception\GuzzleException
52
     */
53
    protected function makeRequest(
54
        $method,
55
        $path,
56
        $statusExpected = 200,
57
        $query = null,
58
        $requestBody = null,
59
        $requestHeader = []
60
    ) {
61
        $requester = new SwaggerRequester();
62
        $body = $requester
63
            ->withSwaggerSchema($this->swaggerSchema)
64
            ->withMethod($method)
65
            ->withPath($path)
66
            ->withQuery($query)
0 ignored issues
show
Bug introduced by Joao Gilberto Magalhães
It seems like $query defined by parameter $query on line 57 can also be of type null; however, ByJG\Swagger\SwaggerRequester::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...
67
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by Joao Gilberto Magalhães
It seems like $requestBody defined by parameter $requestBody on line 58 can also be of type array; however, ByJG\Swagger\SwaggerRequester::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...
68
            ->withRequestHeader($requestHeader)
69
            ->assertResponseCode($statusExpected)
70
            ->send();
71
72
        // Note:
73
        // This code is only reached if the send is successful and
74
        // all matches are satisfied. Otherwise an error is throwed before
75
        // reach this;
76
        $this->assertTrue(true);
77
78
        return $body;
79
    }
80
81
    /**
82
     * @param \ByJG\Swagger\SwaggerRequester $request
83
     * @return mixed
84
     * @throws \ByJG\Swagger\Exception\HttpMethodNotFoundException
85
     * @throws \ByJG\Swagger\Exception\InvalidDefinitionException
86
     * @throws \ByJG\Swagger\Exception\NotMatchedException
87
     * @throws \ByJG\Swagger\Exception\PathNotFoundException
88
     * @throws \ByJG\Swagger\Exception\RequiredArgumentNotFound
89
     * @throws \Exception
90
     */
91
    public function assertRequest(SwaggerRequester $request)
92
    {
93
        // Request based on the Swagger Request definitios
94
        $body = $request
95
            ->withSwaggerSchema($this->swaggerSchema)
96
            ->send();
97
98
        // Note:
99
        // This code is only reached if the send is successful and
100
        // all matches are satisfied. Otherwise an error is throwed before
101
        // reach this;
102
        $this->assertTrue(true);
103
104
        return $body;
105
    }
106
}
107