Completed
Push — master ( da6615...13704a )
by Joao
03:04
created

SwaggerTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A getCustomRequest() 0 4 1
B makeRequest() 0 48 4
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\ClientException;
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 function setUp()
33
    {
34
        $filePath = __DIR__ . '/../../web/docs/swagger.json';
35
        $this->swaggerSchema = new SwaggerSchema(file_get_contents($filePath));
36
37
        $this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'Swagger Test']]);
38
    }
39
40
    protected function getCustomRequest()
41
    {
42
        return [];
43
    }
44
45
    /**
46
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
47
     * @param $path The REST path call
48
     * @param int $statusExpected
49
     * @param null $query
50
     * @param null $body
51
     * @return mixed
52
     */
53
    protected function makeRequest($method, $path, $statusExpected = 200, $query = null, $body = null)
54
    {
55
        $paramInQuery = null;
56
        if (!empty($query)) {
57
            $paramInQuery = '?' . http_build_query($query);
58
        }
59
60
        $header = array_merge([
61
                'Accept' => 'application/json'
62
            ],
63
            $this->getCustomRequest()
64
        );
65
66
        $httpSchema = $this->swaggerSchema->getHttpSchema();
67
        $host = $this->swaggerSchema->getHost();
68
        $basePath = $this->swaggerSchema->getBasePath();
69
70
        $request = new Request(
71
            $method,
72
            "$httpSchema://$host/$basePath$path$paramInQuery",
73
            $header,
74
            json_encode($body)
75
        );
76
77
        $statusReturned = null;
78
        try {
79
            $response = $this->guzzleHttpClient->send($request);
80
            $responseBody = json_decode((string) $response->getBody(), true);
81
            $statusReturned = $response->getStatusCode();
82
        } catch (ClientException $ex) {
83
            $responseBody = json_decode((string) $ex->getResponse()->getBody(), true);
84
            $statusReturned = $ex->getResponse()->getStatusCode();
85
        }
86
87
        $this->assertEquals($statusExpected, $statusReturned);
88
89
        $method = strtolower($method);
90
91
        $bodyRequestDef = $this->swaggerSchema->getRequestParameters("$basePath$path", $method);
92
        $bodyResponseDef = $this->swaggerSchema->getResponseParameters("$basePath$path", $method, $statusExpected);
93
94
        if (!empty($body)) {
95
            $bodyRequestDef->match($body);
96
        }
97
        $bodyResponseDef->match($responseBody);
98
99
        return $responseBody;
100
    }
101
}
102