• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zfegg / expressive-test / 9520702861

14 Jun 2024 06:26PM UTC coverage: 95.913%. Remained the same
9520702861

Pull #17

github

web-flow
Merge 8ff6b65e4 into 7012ec157
Pull Request #17: Update deps

352 of 367 relevant lines covered (95.91%)

4.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Helper/MakeHttpRequestTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Zfegg\ExpressiveTest\Helper;
6

7
use Laminas\ServiceManager\ServiceManager;
8
use Psr\Http\Message\ResponseInterface;
9
use Zfegg\ExpressiveTest\PassMiddleware;
10
use Zfegg\ExpressiveTest\TestResponse;
11

12
/**
13
 * Trait MakeHttpRequestTrait
14
 *
15
 * @property ServiceManager $container
16
 */
17
trait MakeHttpRequestTrait
18
{
19
    /**
20
     * Additional headers for the request.
21
     *
22
     * @var array
23
     */
24
    protected $defaultHeaders = [];
25

26
    /**
27
     * Additional cookies for the request.
28
     *
29
     * @var array
30
     */
31
    protected $defaultCookies = [];
32

33
    /**
34
     * Additional server variables for the request.
35
     *
36
     * @var array
37
     */
38
    protected $serverVariables = [];
39

40

41
    /**
42
     * Define additional headers to be sent with the request.
43
     *
44
     * @param  array $headers
45
     *
46
     * @return $this
47
     */
48
    public function withHeaders(array $headers): self
49
    {
50
        $this->defaultHeaders = array_merge($this->defaultHeaders, $headers);
1✔
51

52
        return $this;
1✔
53
    }
54

55
    /**
56
     * Add a header to be sent with the request.
57
     *
58
     * @param  string $name
59
     * @param  string $value
60
     *
61
     * @return $this
62
     */
63
    public function withHeader(string $name, string $value): self
64
    {
65
        $this->defaultHeaders[$name] = $value;
2✔
66

67
        return $this;
2✔
68
    }
69

70
    /**
71
     * Flush all the configured headers.
72
     *
73
     * @return $this
74
     */
75
    public function flushHeaders(): self
76
    {
77
        $this->defaultHeaders = [];
1✔
78

79
        return $this;
1✔
80
    }
81

82
    /**
83
     * Define a set of server variables to be sent with the requests.
84
     *
85
     * @param  array $server
86
     *
87
     * @return $this
88
     */
89
    public function withServerVariables(array $server): self
90
    {
91
        $this->serverVariables = $server;
1✔
92

93
        return $this;
1✔
94
    }
95

96
    /**
97
     * Disable middleware for the test.
98
     *
99
     * @param  string|array $middleware
100
     *
101
     * @return $this
102
     */
103
    public function withoutMiddleware($middleware): self
104
    {
105
        foreach ((array)$middleware as $abstract) {
1✔
106
            $this->container->setService($abstract, new PassMiddleware());
1✔
107
        }
108

109
        return $this;
1✔
110
    }
111

112
    /**
113
     * Define additional cookies to be sent with the request.
114
     *
115
     * @param  array $cookies
116
     *
117
     * @return $this
118
     */
119
    public function withCookies(array $cookies): self
120
    {
121
        $this->defaultCookies = array_merge($this->defaultCookies, $cookies);
1✔
122

123
        return $this;
1✔
124
    }
125

126
    /**
127
     * Add a cookie to be sent with the request.
128
     *
129
     * @param  string $name
130
     * @param  string $value
131
     *
132
     * @return $this
133
     */
134
    public function withCookie(string $name, string $value): self
135
    {
136
        $this->defaultCookies[$name] = $value;
1✔
137

138
        return $this;
1✔
139
    }
140

141

142
    /**
143
     * Set the referer header and previous URL session value in order to simulate a previous request.
144
     *
145
     * @param  string $url
146
     *
147
     * @return $this
148
     */
149
    public function from(string $url): self
150
    {
151
        return $this->withHeader('referer', $url);
1✔
152
    }
153

154
    /**
155
     * Visit the given URI with a GET request.
156
     *
157
     * @param  string $uri
158
     * @param  array  $headers
159
     *
160
     * @return TestResponse
161
     */
162
    public function get(string $uri, array $headers = []): TestResponse
163
    {
164
        $server = $this->transformHeadersToServerVars($headers);
1✔
165

166
        return $this->call('GET', $uri, [], [], [], $server);
1✔
167
    }
168

169
    /**
170
     * Visit the given URI with a GET request, expecting a JSON response.
171
     *
172
     * @param  string $uri
173
     * @param  array  $headers
174
     *
175
     * @return TestResponse
176
     */
177
    public function getJson(string $uri, array $headers = []): TestResponse
178
    {
179
        return $this->json('GET', $uri, [], $headers);
1✔
180
    }
181

182
    /**
183
     * Visit the given URI with a POST request.
184
     *
185
     * @param  string $uri
186
     * @param  array  $data
187
     * @param  array  $headers
188
     *
189
     * @return TestResponse
190
     */
191
    public function post(string $uri, array $data = [], array $headers = []): TestResponse
192
    {
193
        $server = $this->transformHeadersToServerVars($headers);
1✔
194

195
        return $this->call('POST', $uri, $data, [], [], $server);
1✔
196
    }
197

198
    /**
199
     * Visit the given URI with a POST request, expecting a JSON response.
200
     *
201
     * @param  string $uri
202
     * @param  array  $data
203
     * @param  array  $headers
204
     *
205
     * @return TestResponse
206
     */
207
    public function postJson(string $uri, array $data = [], array $headers = []): TestResponse
208
    {
209
        return $this->json('POST', $uri, $data, $headers);
1✔
210
    }
211

212
    /**
213
     * Visit the given URI with a PUT request.
214
     *
215
     * @param  string $uri
216
     * @param  array  $data
217
     * @param  array  $headers
218
     *
219
     * @return TestResponse
220
     */
221
    public function put(string $uri, array $data = [], array $headers = []): TestResponse
222
    {
223
        $server = $this->transformHeadersToServerVars($headers);
1✔
224

225
        return $this->call('PUT', $uri, $data, [], [], $server);
1✔
226
    }
227

228
    /**
229
     * Visit the given URI with a PUT request, expecting a JSON response.
230
     *
231
     * @param  string $uri
232
     * @param  array  $data
233
     * @param  array  $headers
234
     *
235
     * @return TestResponse
236
     */
237
    public function putJson(string $uri, array $data = [], array $headers = []): TestResponse
238
    {
239
        return $this->json('PUT', $uri, $data, $headers);
1✔
240
    }
241

242
    /**
243
     * Visit the given URI with a PATCH request.
244
     *
245
     * @param  string $uri
246
     * @param  array  $data
247
     * @param  array  $headers
248
     *
249
     * @return TestResponse
250
     */
251
    public function patch(string $uri, array $data = [], array $headers = []): TestResponse
252
    {
253
        $server = $this->transformHeadersToServerVars($headers);
1✔
254

255
        return $this->call('PATCH', $uri, $data, [], [], $server);
1✔
256
    }
257

258
    /**
259
     * Visit the given URI with a PATCH request, expecting a JSON response.
260
     *
261
     * @param  string $uri
262
     * @param  array  $data
263
     * @param  array  $headers
264
     *
265
     * @return TestResponse
266
     */
267
    public function patchJson(string $uri, array $data = [], array $headers = []): TestResponse
268
    {
269
        return $this->json('PATCH', $uri, $data, $headers);
1✔
270
    }
271

272
    /**
273
     * Visit the given URI with a DELETE request.
274
     *
275
     * @param  string $uri
276
     * @param  array  $data
277
     * @param  array  $headers
278
     *
279
     * @return TestResponse
280
     */
281
    public function delete(string $uri, array $data = [], array $headers = []): TestResponse
282
    {
283
        $server = $this->transformHeadersToServerVars($headers);
1✔
284

285
        return $this->call('DELETE', $uri, $data, [], [], $server);
1✔
286
    }
287

288
    /**
289
     * Visit the given URI with a OPTIONS request.
290
     *
291
     * @param  string $uri
292
     * @param  array  $data
293
     * @param  array  $headers
294
     *
295
     * @return TestResponse
296
     */
297
    public function options(string $uri, array $data = [], array $headers = []): TestResponse
298
    {
299
        $server = $this->transformHeadersToServerVars($headers);
1✔
300

301
        return $this->call('OPTIONS', $uri, $data, [], [], $server);
1✔
302
    }
303

304
    /**
305
     * Call the given URI with a JSON request.
306
     *
307
     * @param  string $method
308
     * @param  string $uri
309
     * @param  array  $data
310
     * @param  array  $headers
311
     *
312
     * @return TestResponse
313
     */
314
    public function json(string $method, string $uri, array $data = [], array $headers = []): TestResponse
315
    {
316
        $content = json_encode($data);
4✔
317

318
        $headers = array_merge(
4✔
319
            [
4✔
320
                'CONTENT_LENGTH' => mb_strlen($content, '8bit'),
4✔
321
                'CONTENT_TYPE'   => 'application/json',
4✔
322
                'Accept'         => 'application/json',
4✔
323
            ],
4✔
324
            $headers
4✔
325
        );
4✔
326

327
        return $this->call(
4✔
328
            $method,
4✔
329
            $uri,
4✔
330
            [],
4✔
331
            [],
4✔
332
            [],
4✔
333
            $this->transformHeadersToServerVars($headers),
4✔
334
            $content
4✔
335
        );
4✔
336
    }
337

338
    /**
339
     * Call the given URI and return the Response.
340
     *
341
     * @param string               $uri        The URI
342
     * @param string               $method     The HTTP method
343
     * @param array                $parameters The query (GET) or request (POST) parameters
344
     * @param array                $cookies    The request cookies ($_COOKIE)
345
     * @param array                $files      The request files ($_FILES)
346
     * @param array                $server     The server parameters ($_SERVER)
347
     * @param string|null          $content
348
     *
349
     * @return TestResponse
350
     */
351
    public function call(
352
        string $method,
353
        string $uri,
354
        array $parameters = [],
355
        array $cookies = [],
356
        array $files = [],
357
        array $server = [],
358
        ?string $content = null
359
    ): TestResponse {
360
        if (strtoupper($method) == 'GET' && $parameters) {
11✔
361
            if (strpos($uri, '?') !== false) {
1✔
362
                $uri .= '&' . http_build_query($parameters);
1✔
363
            } else {
364
                $uri .= '?' . http_build_query($parameters);
1✔
365
            }
366
            $parameters = [];
1✔
367
        }
368

369
        $response = $this->runApp(
11✔
370
            $method,
11✔
371
            $uri,
11✔
372
            $parameters,
11✔
373
            $server + $this->serverVariables,
11✔
374
            $content,
11✔
375
            $cookies + $this->defaultCookies,
11✔
376
            $files
11✔
377
        );
11✔
378

379
        return $this->createTestResponse($response);
11✔
380
    }
381

382
    /**
383
     * Transform headers array to array of $_SERVER vars with HTTP_* format.
384
     *
385
     * @param  array $headers
386
     *
387
     * @return array
388
     */
389
    protected function transformHeadersToServerVars(array $headers): array
390
    {
391
        $servers = [];
10✔
392
        foreach (($this->defaultHeaders + $headers) as $name => $value) {
10✔
393
            $servers[$this->formatServerHeaderKey($name)] = $value;
4✔
394
        }
395

396
        return $servers;
10✔
397
    }
398

399
    /**
400
     * Format the header name for the server array.
401
     *
402
     * @param  string $name
403
     *
404
     * @return string
405
     */
406
    protected function formatServerHeaderKey($name): string
407
    {
408
        if (strpos($name, 'HTTP_') !== 0 && $name !== 'CONTENT_TYPE' && $name !== 'REMOTE_ADDR') {
4✔
409
            return 'HTTP_' . $name;
4✔
410
        }
411

412
        return $name;
4✔
413
    }
414

415
    /**
416
     * Create the test response instance from the given response.
417
     */
418
    protected function createTestResponse(ResponseInterface $response): TestResponse
419
    {
420
        return TestResponse::fromBaseResponse($response);
11✔
421
    }
422
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc