|
15 | 15 | use PHPUnit\Framework\Attributes\CoversClass; |
16 | 16 | use PHPUnit\Framework\Attributes\TestWith; |
17 | 17 | use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Log\AbstractLogger; |
| 19 | +use Psr\Log\LoggerInterface; |
18 | 20 | use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
19 | 21 | use Symfony\Component\HttpClient\Exception\ServerException; |
20 | 22 | use Symfony\Component\HttpClient\Exception\TransportException; |
@@ -265,6 +267,124 @@ public function testRecordsFailureOnlyOnceWhenServerErrorResponseAlsoFailsWhileS |
265 | 267 | } |
266 | 268 | } |
267 | 269 |
|
| 270 | + public function testLogsRecordedSuccess(): void |
| 271 | + { |
| 272 | + $records = []; |
| 273 | + $storage = new InMemoryStorage(); |
| 274 | + $circuitBreaker = new CircuitBreaker($storage, new CircuitBreakerConfig(failureThreshold: 1)); |
| 275 | + $client = new CircuitBreakerHttpClient( |
| 276 | + new MockHttpClient(new MockResponse('ok', ['http_code' => 200])), |
| 277 | + $circuitBreaker, |
| 278 | + new DefaultFailureChecker(), |
| 279 | + 'api', |
| 280 | + ); |
| 281 | + $client->setLogger(self::createLogger($records)); |
| 282 | + |
| 283 | + self::assertSame('ok', $client->request('GET', 'https://example.com')->getContent(false)); |
| 284 | + |
| 285 | + self::assertSame([ |
| 286 | + [ |
| 287 | + 'level' => 'debug', |
| 288 | + 'message' => 'Circuit breaker recorded HTTP request success.', |
| 289 | + 'context' => [ |
| 290 | + 'service_name' => 'api', |
| 291 | + 'method' => 'GET', |
| 292 | + 'url' => 'https://example.com', |
| 293 | + 'status_code' => 200, |
| 294 | + ], |
| 295 | + ], |
| 296 | + ], $records); |
| 297 | + } |
| 298 | + |
| 299 | + public function testLogsRecordedFailure(): void |
| 300 | + { |
| 301 | + $records = []; |
| 302 | + $storage = new InMemoryStorage(); |
| 303 | + $circuitBreaker = new CircuitBreaker($storage, new CircuitBreakerConfig(failureThreshold: 1)); |
| 304 | + $client = new CircuitBreakerHttpClient( |
| 305 | + new MockHttpClient(new MockResponse('', ['http_code' => 500])), |
| 306 | + $circuitBreaker, |
| 307 | + new DefaultFailureChecker(), |
| 308 | + 'api', |
| 309 | + ); |
| 310 | + $client->setLogger(self::createLogger($records)); |
| 311 | + |
| 312 | + self::assertSame(500, $client->request('GET', 'https://example.com')->getStatusCode()); |
| 313 | + |
| 314 | + self::assertSame([ |
| 315 | + [ |
| 316 | + 'level' => 'debug', |
| 317 | + 'message' => 'Circuit breaker recorded HTTP request failure.', |
| 318 | + 'context' => [ |
| 319 | + 'service_name' => 'api', |
| 320 | + 'method' => 'GET', |
| 321 | + 'url' => 'https://example.com', |
| 322 | + 'status_code' => 500, |
| 323 | + ], |
| 324 | + ], |
| 325 | + ], $records); |
| 326 | + } |
| 327 | + |
| 328 | + public function testLogsRecordedTransportFailure(): void |
| 329 | + { |
| 330 | + $records = []; |
| 331 | + $storage = new InMemoryStorage(); |
| 332 | + $circuitBreaker = new CircuitBreaker($storage, new CircuitBreakerConfig(failureThreshold: 1)); |
| 333 | + $client = new CircuitBreakerHttpClient( |
| 334 | + new MockHttpClient(new MockResponse([new TransportException('Network failure')])), |
| 335 | + $circuitBreaker, |
| 336 | + new DefaultFailureChecker(), |
| 337 | + 'api', |
| 338 | + ); |
| 339 | + $client->setLogger(self::createLogger($records)); |
| 340 | + |
| 341 | + $this->expectException(TransportException::class); |
| 342 | + |
| 343 | + try { |
| 344 | + $client->request('GET', 'https://example.com')->getContent(); |
| 345 | + } finally { |
| 346 | + self::assertSame([ |
| 347 | + [ |
| 348 | + 'level' => 'debug', |
| 349 | + 'message' => 'Circuit breaker recorded HTTP request failure.', |
| 350 | + 'context' => [ |
| 351 | + 'service_name' => 'api', |
| 352 | + 'method' => 'GET', |
| 353 | + 'url' => 'https://example.com', |
| 354 | + 'status_code' => 200, |
| 355 | + 'error' => 'Network failure', |
| 356 | + ], |
| 357 | + ], |
| 358 | + ], $records); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + public function testLogsBlockedRequestWhenCircuitIsOpen(): void |
| 363 | + { |
| 364 | + $records = []; |
| 365 | + $storage = new InMemoryStorage(); |
| 366 | + $circuitBreaker = new CircuitBreaker($storage, new CircuitBreakerConfig(exceptionsEnabled: false)); |
| 367 | + $circuitBreaker->forceState('api', CircuitState::OPEN); |
| 368 | + $innerClient = new MockHttpClient(); |
| 369 | + |
| 370 | + $client = new CircuitBreakerHttpClient($innerClient, $circuitBreaker, new DefaultFailureChecker(), 'api'); |
| 371 | + $client->setLogger(self::createLogger($records)); |
| 372 | + |
| 373 | + self::assertSame(503, $client->request('GET', 'https://example.com')->getStatusCode()); |
| 374 | + |
| 375 | + self::assertSame([ |
| 376 | + [ |
| 377 | + 'level' => 'debug', |
| 378 | + 'message' => 'Circuit breaker blocked HTTP request.', |
| 379 | + 'context' => [ |
| 380 | + 'service_name' => 'api', |
| 381 | + 'method' => 'GET', |
| 382 | + 'url' => 'https://example.com', |
| 383 | + ], |
| 384 | + ], |
| 385 | + ], $records); |
| 386 | + } |
| 387 | + |
268 | 388 | /** |
269 | 389 | * @param array<string, mixed> $options |
270 | 390 | */ |
@@ -382,4 +502,32 @@ public function testRejectsInvalidOverriddenServiceName(array $options): void |
382 | 502 |
|
383 | 503 | $client->request('GET', 'https://example.com', $options); |
384 | 504 | } |
| 505 | + |
| 506 | + /** |
| 507 | + * @param list<array{level: mixed, message: string, context: array<string, mixed>}> $records |
| 508 | + */ |
| 509 | + private static function createLogger(array &$records): LoggerInterface |
| 510 | + { |
| 511 | + return new class($records) extends AbstractLogger { |
| 512 | + /** |
| 513 | + * @param list<array{level: mixed, message: string, context: array<string, mixed>}> $records |
| 514 | + */ |
| 515 | + public function __construct( |
| 516 | + private array &$records, |
| 517 | + ) { |
| 518 | + } |
| 519 | + |
| 520 | + /** |
| 521 | + * @param array<string, mixed> $context |
| 522 | + */ |
| 523 | + public function log($level, \Stringable|string $message, array $context = []): void |
| 524 | + { |
| 525 | + $this->records[] = [ |
| 526 | + 'level' => $level, |
| 527 | + 'message' => (string) $message, |
| 528 | + 'context' => $context, |
| 529 | + ]; |
| 530 | + } |
| 531 | + }; |
| 532 | + } |
385 | 533 | } |
0 commit comments