|
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,90 @@ 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 testLogsBlockedRequestWhenCircuitIsOpen(): void |
| 329 | + { |
| 330 | + $records = []; |
| 331 | + $storage = new InMemoryStorage(); |
| 332 | + $circuitBreaker = new CircuitBreaker($storage, new CircuitBreakerConfig(exceptionsEnabled: false)); |
| 333 | + $circuitBreaker->forceState('api', CircuitState::OPEN); |
| 334 | + $innerClient = new MockHttpClient(); |
| 335 | + |
| 336 | + $client = new CircuitBreakerHttpClient($innerClient, $circuitBreaker, new DefaultFailureChecker(), 'api'); |
| 337 | + $client->setLogger(self::createLogger($records)); |
| 338 | + |
| 339 | + self::assertSame(503, $client->request('GET', 'https://example.com')->getStatusCode()); |
| 340 | + |
| 341 | + self::assertSame([ |
| 342 | + [ |
| 343 | + 'level' => 'debug', |
| 344 | + 'message' => 'Circuit breaker blocked HTTP request.', |
| 345 | + 'context' => [ |
| 346 | + 'service_name' => 'api', |
| 347 | + 'method' => 'GET', |
| 348 | + 'url' => 'https://example.com', |
| 349 | + ], |
| 350 | + ], |
| 351 | + ], $records); |
| 352 | + } |
| 353 | + |
268 | 354 | /** |
269 | 355 | * @param array<string, mixed> $options |
270 | 356 | */ |
@@ -382,4 +468,32 @@ public function testRejectsInvalidOverriddenServiceName(array $options): void |
382 | 468 |
|
383 | 469 | $client->request('GET', 'https://example.com', $options); |
384 | 470 | } |
| 471 | + |
| 472 | + /** |
| 473 | + * @param list<array{level: mixed, message: string, context: array<string, mixed>}> $records |
| 474 | + */ |
| 475 | + private static function createLogger(array &$records): LoggerInterface |
| 476 | + { |
| 477 | + return new class($records) extends AbstractLogger { |
| 478 | + /** |
| 479 | + * @param list<array{level: mixed, message: string, context: array<string, mixed>}> $records |
| 480 | + */ |
| 481 | + public function __construct( |
| 482 | + private array &$records, |
| 483 | + ) { |
| 484 | + } |
| 485 | + |
| 486 | + /** |
| 487 | + * @param array<string, mixed> $context |
| 488 | + */ |
| 489 | + public function log($level, \Stringable|string $message, array $context = []): void |
| 490 | + { |
| 491 | + $this->records[] = [ |
| 492 | + 'level' => $level, |
| 493 | + 'message' => (string) $message, |
| 494 | + 'context' => $context, |
| 495 | + ]; |
| 496 | + } |
| 497 | + }; |
| 498 | + } |
385 | 499 | } |
0 commit comments