|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Debug\Toolbar\Collectors; |
| 15 | + |
| 16 | +use CodeIgniter\Log\Logger; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use Config\Logger as LoggerConfig; |
| 19 | +use Config\Services; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Others')] |
| 26 | +final class LogsTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + private Logger $logger; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + // The logs collector relies on the logger being in debug mode |
| 35 | + // so it would populate logCache. |
| 36 | + $this->logger = new Logger(new LoggerConfig(), debug: true); |
| 37 | + Services::injectMock('logger', $this->logger); |
| 38 | + } |
| 39 | + |
| 40 | + public function testDisplay(): void |
| 41 | + { |
| 42 | + // log_message() always creates a new TestLogger instance while |
| 43 | + // testing, so we need to log directly to our instance. |
| 44 | + $this->logger->error('Test error'); |
| 45 | + $this->logger->info('Test info'); |
| 46 | + |
| 47 | + $collector = new Logs(); |
| 48 | + $result = $collector->display(); |
| 49 | + |
| 50 | + $this->assertArrayHasKey('logs', $result); |
| 51 | + $this->assertCount(2, $result['logs']); |
| 52 | + $this->assertSame('error', $result['logs'][0]['level']); |
| 53 | + $this->assertSame('Test error', $result['logs'][0]['msg']); |
| 54 | + $this->assertSame('info', $result['logs'][1]['level']); |
| 55 | + $this->assertSame('Test info', $result['logs'][1]['msg']); |
| 56 | + } |
| 57 | + |
| 58 | + public function testEmpty(): void |
| 59 | + { |
| 60 | + $collector = new Logs(); |
| 61 | + $this->assertTrue($collector->isEmpty()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testNotEmpty(): void |
| 65 | + { |
| 66 | + $this->logger->warning('Test warning'); |
| 67 | + |
| 68 | + $collector = new Logs(); |
| 69 | + $this->assertFalse($collector->isEmpty()); |
| 70 | + } |
| 71 | +} |
0 commit comments