Passed
Push — master ( 5911e9...52488d )
by Joao
31s
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
    protected function getCustomHeader()
46
    {
47
        return [];
48
    }
49
50
    /**
51
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
52
     * @param string $path The REST path call
53
     * @param int $statusExpected
54
     * @param array|null $query
55
     * @param array|null $requestBody
56
     * @return mixed
57
     */
58
    protected function makeRequest($method, $path, $statusExpected = 200, $query = null, $requestBody = null)
59
    {
60
        // Preparing Parameters
61
        $paramInQuery = null;
62
        if (!empty($query)) {
63
            $paramInQuery = '?' . http_build_query($query);
64
        }
65
66
        // Preparing Header
67
        $header = array_merge([
68
                'Accept' => 'application/json'
69
            ],
70
            $this->getCustomHeader()
71
        );
72
73
        // Defining Variables
74
        $httpSchema = $this->swaggerSchema->getHttpSchema();
75
        $host = $this->swaggerSchema->getHost();
76
        $basePath = $this->swaggerSchema->getBasePath();
77
78
        // Check if the body is the expected before request
79
        $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$path", $method);
80
        if (!empty($requestBody)) {
81
            $bodyRequestDef->match($requestBody);
82
        }
83
84
        // Make the request
85
        $request = new Request(
86
            $method,
87
            "$httpSchema://$host$basePath$path$paramInQuery",
88
            $header,
89
            json_encode($requestBody)
90
        );
91
92
        $statusReturned = null;
93
        try {
94
            $response = $this->guzzleHttpClient->send($request);
95
            $responseBody = json_decode((string) $response->getBody(), true);
96
            $statusReturned = $response->getStatusCode();
97
        } catch (BadResponseException $ex) {
98
            $responseBody = json_decode((string) $ex->getResponse()->getBody(), true);
99
            $statusReturned = $ex->getResponse()->getStatusCode();
100
        }
101
102
        // Assert results
103
        $this->assertEquals($statusExpected, $statusReturned, json_encode($responseBody, JSON_PRETTY_PRINT));
104
105
        $bodyResponseDef = $this->swaggerSchema->getResponseParameters("$basePath$path", $method, $statusExpected);
106
        $bodyResponseDef->match($responseBody);
107
108
        return $responseBody;
109
    }
110
}
111