Completed
Pull Request — master (#40)
by Ulrich
01:53
created

SwaggerTestCase::makeRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
3
namespace ByJG\Swagger;
4
5
use ByJG\Swagger\Exception\GenericSwaggerException;
6
use GuzzleHttp\Exception\GuzzleException;
7
use ByJG\Swagger\AbstractRequester;
8
use ByJG\Swagger\ApiRequester;
9
use ByJG\Swagger\Base\Schema;
10
use ByJG\Swagger\Exception\DefinitionNotFoundException;
11
use ByJG\Swagger\Exception\HttpMethodNotFoundException;
12
use ByJG\Swagger\Exception\InvalidDefinitionException;
13
use ByJG\Swagger\Exception\NotMatchedException;
14
use ByJG\Swagger\Exception\PathNotFoundException;
15
use ByJG\Swagger\Exception\StatusCodeNotMatchedException;
16
use ByJG\Swagger\Exception\GenericSwaggerException;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use ByJG\Swagger\Exception\GenericSwaggerException as GenericSwaggerException because the name is already in use
Loading history...
17
use GuzzleHttp\GuzzleException;
18
use PHPUnit\Framework\TestCase;
19
20
abstract class SwaggerTestCase extends TestCase
21
{
22
    /**
23
     * @var SwaggerSchema
24
     */
25
    protected $swaggerSchema;
26
27
    protected $filePath;
28
29
    /**
30
     * @throws GenericSwaggerException
31
     */
32
    protected function setUp()
33
    {
34
        if (empty($this->filePath)) {
35
            throw new GenericSwaggerException('You have to define the property $filePath');
36
        }
37
38
        $this->swaggerSchema = new SwaggerSchema(file_get_contents($this->filePath));
39
    }
40
41
    /**
42
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
43
     * @param string $path The REST path call
44
     * @param int $statusExpected
45
     * @param array|null $query
46
     * @param array|null $requestBody
47
     * @param array $requestHeader
48
     * @return mixed
49
     * @throws Exception\DefinitionNotFoundException
50
     * @throws Exception\HttpMethodNotFoundException
51
     * @throws Exception\InvalidDefinitionException
52
     * @throws Exception\InvalidRequestException
53
     * @throws Exception\NotMatchedException
54
     * @throws Exception\PathNotFoundException
55
     * @throws Exception\RequiredArgumentNotFound
56
     * @throws Exception\StatusCodeNotMatchedException
57
     * @throws GenericSwaggerException
58
     * @throws GuzzleException
59
     * @deprecated Use assertRequest instead
60
     */
61
    protected function makeRequest(
62
        $method,
63
        $path,
64
        $statusExpected = 200,
65
        $query = null,
66
        $requestBody = null,
67
        $requestHeader = []
68
    ) {
69
        $requester = new SwaggerRequester();
70
        $body = $requester
71
            ->withSwaggerSchema($this->swaggerSchema)
72
            ->withMethod($method)
73
            ->withPath($path)
74
            ->withQuery($query)
75
            ->withRequestBody($requestBody)
76
            ->withRequestHeader($requestHeader)
77
            ->assertResponseCode($statusExpected)
78
            ->send();
79
80
        // Note:
81
        // This code is only reached if the send is successful and
82
        // all matches are satisfied. Otherwise an error is throwed before
83
        // reach this
84
        $this->assertTrue(true);
85
86
        return $body;
87
    }
88
89
    /**
90
     * @param AbstractRequester $request
91
     * @return mixed
92
     * @throws Exception\DefinitionNotFoundException
93
     * @throws Exception\HttpMethodNotFoundException
94
     * @throws Exception\InvalidDefinitionException
95
     * @throws Exception\InvalidRequestException
96
     * @throws Exception\NotMatchedException
97
     * @throws Exception\PathNotFoundException
98
     * @throws Exception\RequiredArgumentNotFound
99
     * @throws Exception\StatusCodeNotMatchedException
100
     * @throws GenericSwaggerException
101
     * @throws GuzzleException
102
     */
103
    public function assertRequest(AbstractRequester $request)
104
    {
105
        // Add own swagger if nothing is passed.
106
        if (!$request->hasSwaggerSchema()) {
107
            $request->withSwaggerSchema($this->swaggerSchema);
108
        }
109
110
        // Request based on the Swagger Request definitios
111
        $body = $request->send();
112
113
        // Note:
114
        // This code is only reached if the send is successful and
115
        // all matches are satisfied. Otherwise an error is throwed before
116
        // reach this
117
        $this->assertTrue(true);
118
119
        return $body;
120
    }
121
}
122