|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\Logging\Tests\Logger; |
| 6 | + |
| 7 | +use KaririCode\Contract\Logging\Logger; |
| 8 | +use KaririCode\Logging\LogLevel; |
| 9 | +use KaririCode\Logging\SilentLogger; |
| 10 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +#[CoversClass(SilentLogger::class)] |
| 14 | +final class SilentLoggerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Tests that the logger can be instantiated with its default name. |
| 18 | + */ |
| 19 | + public function testShouldBeInstantiableWithDefaultName(): void |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + $logger = new SilentLogger(); |
| 23 | + |
| 24 | + // Assert |
| 25 | + $this->assertInstanceOf(Logger::class, $logger, 'SilentLogger must implement the Logger interface.'); |
| 26 | + $this->assertEquals('silent', $logger->getName(), 'The default name for the logger should be "silent".'); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Tests that the logger can be instantiated with a custom name. |
| 31 | + */ |
| 32 | + public function testShouldBeInstantiableWithCustomName(): void |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + $logger = new SilentLogger('custom_channel'); |
| 36 | + |
| 37 | + // Assert |
| 38 | + $this->assertEquals('custom_channel', $logger->getName(), 'The logger should accept and return a custom name.'); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Tests that calling the main log() method does not produce any errors or output. |
| 43 | + * This is the core behavior of the Null Object Pattern. |
| 44 | + */ |
| 45 | + public function testLogMethodShouldDoNothingAndNotThrowErrors(): void |
| 46 | + { |
| 47 | + // Arrange |
| 48 | + $logger = new SilentLogger(); |
| 49 | + $this->expectNotToPerformAssertions(); // The assertion is that no error/exception occurs. |
| 50 | + |
| 51 | + // Act |
| 52 | + $logger->log(LogLevel::INFO, 'This message should be discarded silently.'); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Tests that convenience methods like info(), error(), etc., can be called safely. |
| 57 | + * These methods are provided by the LoggerTrait and rely on the empty log() method. |
| 58 | + */ |
| 59 | + public function testConvenienceMethodsShouldDoNothingAndNotThrowErrors(): void |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + $logger = new SilentLogger(); |
| 63 | + $this->expectNotToPerformAssertions(); |
| 64 | + |
| 65 | + // Act |
| 66 | + $logger->info('Info message to be discarded.'); |
| 67 | + $logger->error('Error message to be discarded.', ['exception' => 'details']); |
| 68 | + $logger->warning('Warning message to be discarded.'); |
| 69 | + } |
| 70 | +} |
0 commit comments