Completed
Push — master ( dcbb11...fd8391 )
by Joao
14s
created

SwaggerTestCase::getCustomHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
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 20 and the first side effect is on line 17.

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
 * User: jg
4
 * Date: 22/05/17
5
 * Time: 15:32
6
 */
7
8
namespace ByJG\Swagger;
9
10
use GuzzleHttp\Client;
11
use GuzzleHttp\Exception\BadResponseException;
12
use GuzzleHttp\Psr7\Request;
13
use PHPUnit\Framework\TestCase;
14
15
// backward compatibility
16
if (!class_exists('\PHPUnit\Framework\TestCase')) {
17
    class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
18
}
19
20
abstract class SwaggerTestCase extends TestCase
21
{
22
    /**
23
     * @var \ByJG\Swagger\SwaggerSchema
24
     */
25
    protected $swaggerSchema;
26
27
    /**
28
     * @var \GuzzleHttp\ClientInterface
29
     */
30
    protected $guzzleHttpClient;
31
32
    protected $filePath;
33
34
    protected function setUp()
35
    {
36
        if (empty($this->filePath)) {
37
            throw new \Exception('You have to define the property $filePath');
38
        }
39
40
        $this->swaggerSchema = new SwaggerSchema(file_get_contents($this->filePath));
41
42
        $this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'Swagger Test']]);
43
    }
44
45
    /**
46
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
47
     * @param string $path The REST path call
48
     * @param int $statusExpected
49
     * @param array|null $query
50
     * @param array|null $requestBody
51
     * @param array $requestHeader
52
     * @return mixed
53
     * @throws \ByJG\Swagger\Exception\InvalidDefinitionException
54
     * @throws \ByJG\Swagger\Exception\NotMatchedException
55
     * @throws \ByJG\Swagger\Exception\RequiredArgumentNotFound
56
     * @throws \GuzzleHttp\Exception\GuzzleException
57
     */
58
    protected function makeRequest(
59
        $method,
60
        $path,
61
        $statusExpected = 200,
62
        $query = null,
63
        $requestBody = null,
64
        $requestHeader = []
65
    ) {
66
        // Preparing Parameters
67
        $paramInQuery = null;
68
        if (!empty($query)) {
69
            $paramInQuery = '?' . http_build_query($query);
70
        }
71
72
        // Preparing Header
73
        if (empty($requestHeader)) {
74
            $requestHeader = [];
75
        }
76
        $header = array_merge(
77
            [
78
                'Accept' => 'application/json'
79
            ],
80
            $requestHeader
81
        );
82
83
        // Defining Variables
84
        $httpSchema = $this->swaggerSchema->getHttpSchema();
85
        $host = $this->swaggerSchema->getHost();
86
        $basePath = $this->swaggerSchema->getBasePath();
87
88
        // Check if the body is the expected before request
89
        $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$path", $method);
90
        $bodyRequestDef->match($requestBody);
91
92
        // Make the request
93
        $request = new Request(
94
            $method,
95
            "$httpSchema://$host$basePath$path$paramInQuery",
96
            $header,
97
            json_encode($requestBody)
98
        );
99
100
        $statusReturned = null;
101
        try {
102
            $response = $this->guzzleHttpClient->send($request);
103
            $responseBody = json_decode((string) $response->getBody(), true);
104
            $statusReturned = $response->getStatusCode();
105
        } catch (BadResponseException $ex) {
106
            $responseBody = json_decode((string) $ex->getResponse()->getBody(), true);
107
            $statusReturned = $ex->getResponse()->getStatusCode();
108
        }
109
110
        // Assert results
111
        $this->assertEquals($statusExpected, $statusReturned, json_encode($responseBody, JSON_PRETTY_PRINT));
112
113
        $bodyResponseDef = $this->swaggerSchema->getResponseParameters("$basePath$path", $method, $statusExpected);
114
        $bodyResponseDef->match($responseBody);
115
116
        return $responseBody;
117
    }
118
}
119