|
22 | 22 | use PHPUnit\Framework\Attributes\Group; |
23 | 23 | use ReflectionMethod; |
24 | 24 | use ReflectionNamedType; |
| 25 | +use stdClass; |
| 26 | +use Stringable; |
25 | 27 | use Tests\Support\Log\Handlers\TestHandler; |
26 | 28 |
|
27 | 29 | /** |
@@ -438,4 +440,85 @@ public function testDetermineFileNoStackTrace(): void |
438 | 440 |
|
439 | 441 | $this->assertSame($expected, $logger->determineFile()); |
440 | 442 | } |
| 443 | + |
| 444 | + public function testLogInterpolatesArrayContext(): void |
| 445 | + { |
| 446 | + $config = new LoggerConfig(); |
| 447 | + $logger = new Logger($config); |
| 448 | + |
| 449 | + Time::setTestNow('2023-11-25 12:00:00'); |
| 450 | + |
| 451 | + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . print_r(['foo' => 'bar'], true); |
| 452 | + |
| 453 | + $logger->log('debug', 'Test message {foo}', ['foo' => ['foo' => 'bar']]); |
| 454 | + |
| 455 | + $logs = TestHandler::getLogs(); |
| 456 | + |
| 457 | + $this->assertCount(1, $logs); |
| 458 | + $this->assertSame($expected, $logs[0]); |
| 459 | + } |
| 460 | + |
| 461 | + public function testLogInterpolatesObjectContext(): void |
| 462 | + { |
| 463 | + $config = new LoggerConfig(); |
| 464 | + $logger = new Logger($config); |
| 465 | + |
| 466 | + Time::setTestNow('2023-11-25 12:00:00'); |
| 467 | + |
| 468 | + $obj = new stdClass(); |
| 469 | + $obj->foo = 'bar'; |
| 470 | + |
| 471 | + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message ' . print_r($obj, true); |
| 472 | + |
| 473 | + $logger->log('debug', 'Test message {foo}', ['foo' => $obj]); |
| 474 | + |
| 475 | + $logs = TestHandler::getLogs(); |
| 476 | + |
| 477 | + $this->assertCount(1, $logs); |
| 478 | + $this->assertSame($expected, $logs[0]); |
| 479 | + } |
| 480 | + |
| 481 | + public function testLogInterpolatesMultipleEnvironmentVars(): void |
| 482 | + { |
| 483 | + $config = new LoggerConfig(); |
| 484 | + $logger = new Logger($config); |
| 485 | + |
| 486 | + Time::setTestNow('2023-11-25 12:00:00'); |
| 487 | + |
| 488 | + $_ENV['foo'] = 'bar'; |
| 489 | + $_ENV['baz'] = 'qux'; |
| 490 | + |
| 491 | + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Test message bar and qux'; |
| 492 | + |
| 493 | + $logger->log('debug', 'Test message {env:foo} and {env:baz}'); |
| 494 | + |
| 495 | + $logs = TestHandler::getLogs(); |
| 496 | + |
| 497 | + $this->assertCount(1, $logs); |
| 498 | + $this->assertSame($expected, $logs[0]); |
| 499 | + } |
| 500 | + |
| 501 | + public function testLogAcceptsStringableMessage(): void |
| 502 | + { |
| 503 | + $config = new LoggerConfig(); |
| 504 | + $logger = new Logger($config); |
| 505 | + |
| 506 | + Time::setTestNow('2023-11-25 12:00:00'); |
| 507 | + |
| 508 | + $message = new class () implements Stringable { |
| 509 | + public function __toString(): string |
| 510 | + { |
| 511 | + return 'Stringable message {foo}'; |
| 512 | + } |
| 513 | + }; |
| 514 | + |
| 515 | + $expected = 'DEBUG - ' . Time::now()->format('Y-m-d') . ' --> Stringable message bar'; |
| 516 | + |
| 517 | + $logger->log('debug', $message, ['foo' => 'bar']); |
| 518 | + |
| 519 | + $logs = TestHandler::getLogs(); |
| 520 | + |
| 521 | + $this->assertCount(1, $logs); |
| 522 | + $this->assertSame($expected, $logs[0]); |
| 523 | + } |
441 | 524 | } |
0 commit comments