1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Simplex\Quickstart\Shared\Testing; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Simplex\HttpApplication; |
8
|
|
|
use Zend\Diactoros\Response; |
9
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
10
|
|
|
use Zend\Diactoros\Stream; |
11
|
|
|
use Zend\Diactoros\Uri; |
12
|
|
|
use Psr\Http; |
13
|
|
|
|
14
|
|
|
trait HttpRequestCapabilities |
15
|
|
|
{ |
16
|
|
View Code Duplication |
public function sendGet(string $uri, array $queryParams = [], array $headers = []): ResponseInterface |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$request = $this->createRequest('GET', $uri) |
19
|
|
|
->withQueryParams($queryParams); |
20
|
|
|
|
21
|
|
|
$request = $this->addHeaders($headers, $request); |
22
|
|
|
|
23
|
|
|
return $this->send($request); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function createRequest(string $method, string $uri): ServerRequestInterface |
27
|
|
|
{ |
28
|
|
|
return (ServerRequestFactory::fromGlobals([])) |
29
|
|
|
->withMethod($method) |
30
|
|
|
->withUri(new Uri($uri)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function addHeaders(array $headers, ServerRequestInterface $request): ServerRequestInterface |
34
|
|
|
{ |
35
|
|
|
if (empty($headers)) { |
36
|
|
|
return $request; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($headers as $headerName => $headerValue) { |
40
|
|
|
$request = $request->withHeader($headerName, $headerValue); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $request; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function send(ServerRequestInterface $request): ResponseInterface |
47
|
|
|
{ |
48
|
|
|
$application = new HttpApplication($this->getContainer()); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $application->handleRequest($request, new Response()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function sendPost(string $uri, array $body, array $headers = []): ResponseInterface |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$stream = $this->streamFor($body); |
56
|
|
|
|
57
|
|
|
$request = $this->createRequest('POST', $uri) |
58
|
|
|
->withBody($stream); |
59
|
|
|
|
60
|
|
|
$request = $this->addHeaders($headers, $request); |
61
|
|
|
|
62
|
|
|
return $this->send($request); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see https://github.com/guzzle/psr7/blob/master/src/functions.php |
67
|
|
|
*/ |
68
|
|
|
private function streamFor(array $body): Stream |
69
|
|
|
{ |
70
|
|
|
$resource = json_encode($body); |
71
|
|
|
|
72
|
|
|
$stream = fopen('php://temp', 'r+'); |
73
|
|
|
if ($resource !== '') { |
74
|
|
|
fwrite($stream, $resource); |
75
|
|
|
fseek($stream, 0); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new Stream($stream); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function sendPut(string $uri, array $body, array $headers = []): ResponseInterface |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$stream = $this->streamFor($body); |
84
|
|
|
|
85
|
|
|
$request = $this->createRequest('PUT', $uri) |
86
|
|
|
->withBody($stream); |
87
|
|
|
|
88
|
|
|
$request = $this->addHeaders($headers, $request); |
89
|
|
|
|
90
|
|
|
return $this->send($request); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function sendDelete(string $uri, array $headers = []): ResponseInterface |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$request = $this->createRequest('DELETE', $uri); |
96
|
|
|
|
97
|
|
|
$request = $this->addHeaders($headers, $request); |
98
|
|
|
|
99
|
|
|
return $this->send($request); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getBody(ResponseInterface $response): array |
103
|
|
|
{ |
104
|
|
|
return json_decode((string) $response->getBody(), true); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getRawBody(ResponseInterface $response): string |
108
|
|
|
{ |
109
|
|
|
return (string) $response->getBody(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.