Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
69 / 69 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use Phexium\Plugin\Logger\Adapter\FileLogger; |
| 11 | use Psr\Log\LogLevel; |
| 12 | |
| 13 | beforeEach(function (): void { |
| 14 | $this->tempFile = sys_get_temp_dir().'/test_'.uniqid().'/nested/test.log'; |
| 15 | $this->logger = new FileLogger($this->tempFile, LogLevel::DEBUG); |
| 16 | }); |
| 17 | |
| 18 | afterEach(function (): void { |
| 19 | if (file_exists($this->tempFile)) { |
| 20 | unlink($this->tempFile); |
| 21 | rmdir(dirname($this->tempFile)); |
| 22 | rmdir(dirname($this->tempFile, 2)); |
| 23 | } |
| 24 | }); |
| 25 | |
| 26 | test('All Log Levels are Working', function (): void { |
| 27 | $levels = [ |
| 28 | LogLevel::EMERGENCY => 'emergency', |
| 29 | LogLevel::ALERT => 'alert', |
| 30 | LogLevel::CRITICAL => 'critical', |
| 31 | LogLevel::ERROR => 'error', |
| 32 | LogLevel::WARNING => 'warning', |
| 33 | LogLevel::NOTICE => 'notice', |
| 34 | LogLevel::INFO => 'info', |
| 35 | LogLevel::DEBUG => 'debug', |
| 36 | ]; |
| 37 | |
| 38 | foreach ($levels as $method) { |
| 39 | $this->logger->{$method}(sprintf('Test %s message', $method)); |
| 40 | } |
| 41 | |
| 42 | $logContent = file_get_contents($this->tempFile); |
| 43 | |
| 44 | foreach ($levels as $method) { |
| 45 | $ucMethod = strtoupper($method); |
| 46 | expect($logContent)->toContain('['.$ucMethod.']') |
| 47 | ->toContain(sprintf('Test %s message', $method)) |
| 48 | ; |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | test('Handles Context Data', function (): void { |
| 53 | $context = ['user_id' => 123]; |
| 54 | $this->logger->info('User action', $context); |
| 55 | |
| 56 | $logContent = file_get_contents($this->tempFile); |
| 57 | |
| 58 | expect($logContent)->toContain('User action {"user_id":123}'); |
| 59 | }); |
| 60 | |
| 61 | test('Log without context has no trailing content', function (): void { |
| 62 | $this->logger->info('Simple message'); |
| 63 | |
| 64 | $logContent = trim(file_get_contents($this->tempFile)); |
| 65 | |
| 66 | expect($logContent)->toEndWith('Simple message'); |
| 67 | }); |
| 68 | |
| 69 | test('Respects Minimum Log Level', function (): void { |
| 70 | $logger = new FileLogger($this->tempFile, LogLevel::WARNING); |
| 71 | |
| 72 | $logger->error('Error message'); |
| 73 | $logger->warning('Warning message'); |
| 74 | $logger->notice('Notice message'); |
| 75 | $logger->log('UnknownLevel', 'UnknownLevel message'); |
| 76 | |
| 77 | $logContent = file_get_contents($this->tempFile); |
| 78 | |
| 79 | expect($logContent)->toContain('Error message') |
| 80 | ->toContain('Warning message') |
| 81 | ->not->toContain('Notice message') |
| 82 | ->not->toContain('UnknownLevel message') |
| 83 | ; |
| 84 | }); |
| 85 | |
| 86 | test('Log entries end with newline', function (): void { |
| 87 | $this->logger->info('Message'); |
| 88 | |
| 89 | $logContent = file_get_contents($this->tempFile); |
| 90 | |
| 91 | expect($logContent)->toContain('Message') |
| 92 | ->toEndWith("\n") |
| 93 | ; |
| 94 | }); |
| 95 | |
| 96 | test('Handles Stringable message', function (): void { |
| 97 | $stringableMessage = new class implements Stringable { |
| 98 | public function __toString(): string |
| 99 | { |
| 100 | return 'Stringable message'; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | $this->logger->info($stringableMessage); |
| 105 | |
| 106 | $logContent = file_get_contents($this->tempFile); |
| 107 | |
| 108 | expect($logContent)->toContain('Stringable message'); |
| 109 | }); |