Completed
Push — master ( ad3b69...02c092 )
by John
09:08 queued 05:48
created

ApiTestCase::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Test;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
13
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestMeta;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 *
20
 * @property string env
21
 * @property array  defaultServerVars
22
 */
23
trait ApiTestCase
24
{
25
    /**
26
     * @var ApiTestClient
27
     */
28
    protected $client;
29
30
    /**
31
     * @var SchemaValidator
32
     */
33
    private $validator;
34
35
    /**
36
     */
37
    protected function setUp()
38
    {
39
        $this->createApiTestClient();
40
    }
41
42
    /**
43
     * Create a client, booting the kernel using SYMFONY_ENV = $this->env
44
     */
45
    protected function createApiTestClient()
46
    {
47
        return $this->client = new ApiTestClient(
48
            static::createClient([
49
                'environment' => $this->getEnv(),
50
                'debug'       => true
51
            ])
52
        );
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    protected function getDefaultServerVars(): array
59
    {
60
        return isset($this->defaultServerVars) ? $this->defaultServerVars : [];
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    protected function getEnv(): string
67
    {
68
        return isset($this->env) ? $this->env : 'test';
69
    }
70
71
    /**
72
     * @param string $path
73
     * @param array  $params
74
     *
75
     * @return mixed
76
     * @throws ApiResponseErrorException
77
     */
78
    protected function get(string $path, array $params = [])
79
    {
80
        return $this->request($path, 'GET', $params);
81
    }
82
83
    /**
84
     * @param string $path
85
     * @param array  $params
86
     *
87
     * @return mixed
88
     * @throws ApiResponseErrorException
89
     */
90
    protected function delete(string $path, array $params = [])
91
    {
92
        return $this->request($path, 'DELETE', $params);
93
    }
94
95
    /**
96
     * @param string $path
97
     * @param array  $content
98
     * @param array  $params
99
     *
100
     * @return mixed
101
     * @throws ApiResponseErrorException
102
     */
103
    protected function patch(string $path, array $content, array $params = [])
104
    {
105
        return $this->request($path, 'PATCH', $params, $content);
106
    }
107
108
    /**
109
     * @param string $path
110
     * @param array  $content
111
     * @param array  $params
112
     *
113
     * @return mixed
114
     * @throws ApiResponseErrorException
115
     */
116
    protected function post(string $path, array $content, array $params = [])
117
    {
118
        return $this->request($path, 'POST', $params, $content);
119
    }
120
121
    /**
122
     * @param string $path
123
     * @param array  $content
124
     * @param array  $params
125
     *
126
     * @return mixed
127
     * @throws ApiResponseErrorException
128
     */
129
    protected function put(string $path, array $content, array $params = [])
130
    {
131
        return $this->request($path, 'PUT', $params, $content);
132
    }
133
134
    /**
135
     * @param string     $path
136
     * @param string     $method
137
     * @param array      $params
138
     * @param array|null $content
139
     *
140
     * @return mixed
141
     * @throws ApiResponseErrorException
142
     */
143
    protected function request(string $path, string $method, array $params = [], array $content = null)
144
    {
145
        $apiRequest = new ApiRequest($this->assembleUri($path, $params), $method);
146
        $apiRequest->setServer(array_merge(['CONTENT_TYPE' => 'application/json'], $this->getDefaultServerVars()));
147
148
        if ($content !== null) {
149
            $apiRequest->setContent(json_encode($content));
150
        }
151
152
        $this->client->requestFromRequest($apiRequest);
153
154
        /** @var Response $response */
155
        $response = $this->client->getResponse();
156
        /** @var Request $request */
157
        $request = $this->client->getRequest();
158
159
        $body    = null;
160
        $content = null;
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
162
        if (($content = $response->getContent()) && $response->getStatusCode() !== Response::HTTP_NO_CONTENT) {
163
            $body = json_decode($content);
164
            $this->assertSame(
165
                JSON_ERROR_NONE,
166
                json_last_error(),
167
                "Not valid JSON: " . json_last_error_msg() . "(" . var_export($content, true) . ")"
168
            );
169
        }
170
171
        if (substr((string)$response->getStatusCode(), 0, 1) != '2') {
172
            // This throws an exception so that tests can catch it when it is expected
173
            throw new ApiResponseErrorException($content, $body, $response->getStatusCode());
174
        }
175
176
        /** @var Operation $operation */
177
        $operation = $request->attributes->get(RequestMeta::ATTRIBUTE)->getOperation();
178
        $schema    = $operation->getResponse($response->getStatusCode())->getSchema();
0 ignored issues
show
Unused Code introduced by
$schema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
180
181
        return $body;
182
    }
183
184
    /**
185
     * @return SchemaValidator
186
     */
187
    protected function getValidator(): SchemaValidator
188
    {
189
        return $this->client->getContainer()->get('swagger.request.validator');
190
    }
191
192
    /**
193
     * @param string $path
194
     * @param array  $params
195
     *
196
     * @return string
197
     */
198
    private function assembleUri(string $path, array $params = [])
199
    {
200
        $uri = $path;
201
        if (count($params)) {
202
            $uri = $path . '?' . http_build_query($params);
203
        }
204
205
        return $uri;
206
    }
207
208
    /**
209
     * @param mixed  $expected
210
     * @param mixed  $actual
211
     * @param string $message
212
     *
213
     * @return mixed
214
     */
215
    abstract public function assertSame($expected, $actual, $message = '');
216
}
217