|
6 | 6 |
|
7 | 7 | use GuzzleHttp\Client; |
8 | 8 | use GuzzleHttp\Exception\BadResponseException; |
| 9 | +use GuzzleHttp\Exception\ClientException; |
| 10 | +use GuzzleHttp\Exception\ConnectException; |
9 | 11 | use Hyperf\Guzzle\ClientFactory; |
10 | 12 | use PHPUnit\Framework\TestCase; |
| 13 | +use Psr\EventDispatcher\EventDispatcherInterface; |
11 | 14 | use Psr\Http\Message\RequestInterface; |
12 | 15 | use Psr\Http\Message\ResponseInterface; |
13 | 16 | use Psr\Http\Message\StreamInterface; |
| 17 | +use Serendipity\Domain\Contract\Support\ThrownFactory; |
| 18 | +use Serendipity\Domain\Event\RequestExecutedEvent; |
| 19 | +use Serendipity\Domain\Exception\Parser\Thrown; |
14 | 20 | use Serendipity\Domain\Exception\RepositoryException; |
| 21 | +use Serendipity\Domain\Exception\ThrowableType; |
15 | 22 |
|
16 | 23 | class HttpRepositoryTest extends TestCase |
17 | 24 | { |
@@ -76,4 +83,202 @@ public function testShouldRaiseGeneralException(): void |
76 | 83 | $repository = new HttpRepositoryTestMock($clientFactory); |
77 | 84 | $repository->exposeRequest(); |
78 | 85 | } |
| 86 | + |
| 87 | + public function testShouldExtractHeadersWithMultipleValues(): void |
| 88 | + { |
| 89 | + $stream = $this->createMock(StreamInterface::class); |
| 90 | + $stream->expects($this->once()) |
| 91 | + ->method('getContents') |
| 92 | + ->willReturn('{"data": "test"}'); |
| 93 | + |
| 94 | + $response = $this->createMock(ResponseInterface::class); |
| 95 | + $response->expects($this->once()) |
| 96 | + ->method('getHeaders') |
| 97 | + ->willReturn([ |
| 98 | + 'Content-Type' => ['application/json'], |
| 99 | + 'Set-Cookie' => ['cookie1=value1', 'cookie2=value2'], |
| 100 | + ]); |
| 101 | + $response->expects($this->once()) |
| 102 | + ->method('getBody') |
| 103 | + ->willReturn($stream); |
| 104 | + |
| 105 | + $client = $this->createMock(Client::class); |
| 106 | + $client->expects($this->once()) |
| 107 | + ->method('request') |
| 108 | + ->with('GET', '/test', []) |
| 109 | + ->willReturn($response); |
| 110 | + |
| 111 | + $clientFactory = $this->createMock(ClientFactory::class); |
| 112 | + $clientFactory->expects($this->once()) |
| 113 | + ->method('create') |
| 114 | + ->willReturn($client); |
| 115 | + |
| 116 | + $repository = new HttpRepositoryTestMock($clientFactory); |
| 117 | + $message = $repository->exposeRequest('GET', '/test'); |
| 118 | + |
| 119 | + $this->assertEquals('{"data": "test"}', $message->content()); |
| 120 | + $this->assertEquals('application/json', $message->properties()->get('Content-Type')); |
| 121 | + $this->assertEquals(['cookie1=value1', 'cookie2=value2'], $message->properties()->get('Set-Cookie')); |
| 122 | + } |
| 123 | + |
| 124 | + public function testShouldHandleClientExceptionWithResponse(): void |
| 125 | + { |
| 126 | + $stream = $this->createMock(StreamInterface::class); |
| 127 | + $stream->method('getContents') |
| 128 | + ->willReturn('{"error": "Bad Request"}'); |
| 129 | + |
| 130 | + $response = $this->createMock(ResponseInterface::class); |
| 131 | + $response->method('getHeaders') |
| 132 | + ->willReturn(['Content-Type' => ['application/json']]); |
| 133 | + $response->method('getBody') |
| 134 | + ->willReturn($stream); |
| 135 | + |
| 136 | + $exception = new ClientException( |
| 137 | + 'Bad Request', |
| 138 | + $this->createMock(RequestInterface::class), |
| 139 | + $response |
| 140 | + ); |
| 141 | + |
| 142 | + $client = $this->createMock(Client::class); |
| 143 | + $client->expects($this->once()) |
| 144 | + ->method('request') |
| 145 | + ->with('POST', '', []) |
| 146 | + ->willThrowException($exception); |
| 147 | + |
| 148 | + $clientFactory = $this->createMock(ClientFactory::class); |
| 149 | + $clientFactory->expects($this->once()) |
| 150 | + ->method('create') |
| 151 | + ->willReturn($client); |
| 152 | + |
| 153 | + $thrownFactory = $this->createMock(ThrownFactory::class); |
| 154 | + $thrownFactory->expects($this->once()) |
| 155 | + ->method('make') |
| 156 | + ->with($exception) |
| 157 | + ->willReturn(Thrown::createFrom($exception)); |
| 158 | + |
| 159 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 160 | + $dispatcher->expects($this->once()) |
| 161 | + ->method('dispatch'); |
| 162 | + |
| 163 | + try { |
| 164 | + $repository = new HttpRepositoryTestMock($clientFactory, $dispatcher, $thrownFactory); |
| 165 | + $repository->exposeRequest(); |
| 166 | + $this->fail('Expected RepositoryException to be thrown'); |
| 167 | + } catch (RepositoryException $e) { |
| 168 | + $this->assertInstanceOf(RepositoryException::class, $e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public function testShouldHandleNonClientException(): void |
| 173 | + { |
| 174 | + $this->expectException(RepositoryException::class); |
| 175 | + |
| 176 | + $exception = new ConnectException( |
| 177 | + 'Connection refused', |
| 178 | + $this->createMock(RequestInterface::class) |
| 179 | + ); |
| 180 | + |
| 181 | + $client = $this->createMock(Client::class); |
| 182 | + $client->expects($this->once()) |
| 183 | + ->method('request') |
| 184 | + ->with('POST', '', []) |
| 185 | + ->willThrowException($exception); |
| 186 | + |
| 187 | + $clientFactory = $this->createMock(ClientFactory::class); |
| 188 | + $clientFactory->expects($this->once()) |
| 189 | + ->method('create') |
| 190 | + ->willReturn($client); |
| 191 | + |
| 192 | + $thrownFactory = $this->createMock(ThrownFactory::class); |
| 193 | + $thrownFactory->expects($this->once()) |
| 194 | + ->method('make') |
| 195 | + ->with($exception) |
| 196 | + ->willReturn(Thrown::createFrom($exception)); |
| 197 | + |
| 198 | + $repository = new HttpRepositoryTestMock($clientFactory, null, $thrownFactory); |
| 199 | + $repository->exposeRequest(); |
| 200 | + } |
| 201 | + |
| 202 | + public function testShouldDispatchEventOnSuccess(): void |
| 203 | + { |
| 204 | + $stream = $this->createMock(StreamInterface::class); |
| 205 | + $stream->expects($this->once()) |
| 206 | + ->method('getContents') |
| 207 | + ->willReturn('{"message": "Success"}'); |
| 208 | + |
| 209 | + $response = $this->createMock(ResponseInterface::class); |
| 210 | + $response->expects($this->once()) |
| 211 | + ->method('getHeaders') |
| 212 | + ->willReturn([]); |
| 213 | + $response->expects($this->once()) |
| 214 | + ->method('getBody') |
| 215 | + ->willReturn($stream); |
| 216 | + |
| 217 | + $client = $this->createMock(Client::class); |
| 218 | + $client->expects($this->once()) |
| 219 | + ->method('request') |
| 220 | + ->with('POST', '/api', ['json' => ['key' => 'value']]) |
| 221 | + ->willReturn($response); |
| 222 | + |
| 223 | + $clientFactory = $this->createMock(ClientFactory::class); |
| 224 | + $clientFactory->expects($this->once()) |
| 225 | + ->method('create') |
| 226 | + ->willReturn($client); |
| 227 | + |
| 228 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 229 | + $dispatcher->expects($this->once()) |
| 230 | + ->method('dispatch') |
| 231 | + ->with($this->callback(function ($event) { |
| 232 | + return $event instanceof RequestExecutedEvent |
| 233 | + && $event->method === 'POST' |
| 234 | + && $event->uri === '/api' |
| 235 | + && isset($event->options['json']) |
| 236 | + && $event->message !== null; |
| 237 | + })); |
| 238 | + |
| 239 | + $repository = new HttpRepositoryTestMock($clientFactory, $dispatcher); |
| 240 | + $repository->exposeRequest('POST', '/api', ['json' => ['key' => 'value']]); |
| 241 | + } |
| 242 | + |
| 243 | + public function testShouldDispatchEventOnFailure(): void |
| 244 | + { |
| 245 | + $exception = new ConnectException( |
| 246 | + 'Connection refused', |
| 247 | + $this->createMock(RequestInterface::class) |
| 248 | + ); |
| 249 | + |
| 250 | + $client = $this->createMock(Client::class); |
| 251 | + $client->expects($this->once()) |
| 252 | + ->method('request') |
| 253 | + ->with('POST', '/api', []) |
| 254 | + ->willThrowException($exception); |
| 255 | + |
| 256 | + $clientFactory = $this->createMock(ClientFactory::class); |
| 257 | + $clientFactory->expects($this->once()) |
| 258 | + ->method('create') |
| 259 | + ->willReturn($client); |
| 260 | + |
| 261 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 262 | + $dispatcher->expects($this->once()) |
| 263 | + ->method('dispatch') |
| 264 | + ->with($this->callback(function ($event) { |
| 265 | + return $event instanceof RequestExecutedEvent |
| 266 | + && $event->method === 'POST' |
| 267 | + && $event->uri === '/api' |
| 268 | + && $event->message !== null; |
| 269 | + })); |
| 270 | + |
| 271 | + $thrownFactory = $this->createMock(ThrownFactory::class); |
| 272 | + $thrownFactory->expects($this->once()) |
| 273 | + ->method('make') |
| 274 | + ->with($exception) |
| 275 | + ->willReturn(Thrown::createFrom($exception)); |
| 276 | + |
| 277 | + try { |
| 278 | + $repository = new HttpRepositoryTestMock($clientFactory, $dispatcher, $thrownFactory); |
| 279 | + $repository->exposeRequest('POST', '/api'); |
| 280 | + } catch (RepositoryException $exception) { |
| 281 | + // Expected exception |
| 282 | + } |
| 283 | + } |
79 | 284 | } |
0 commit comments