Skip to content

Commit b352164

Browse files
authored
fix: debug toolbar logs collector behavior on isEmpty() (#9724)
* fix: Debug toolbar logs controller * test: added test for toolbar logs collector (#9724) * docs: updated changelog * docs: tweaked changelog
1 parent 52d16f8 commit b352164

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

system/Debug/Toolbar/Collectors/Logs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Logs extends BaseCollector
4747
*
4848
* @var list<array{level: string, msg: string}>
4949
*/
50-
protected $data;
50+
protected $data = [];
5151

5252
/**
5353
* Returns the data of this collector to be formatted in the toolbar.
@@ -68,7 +68,7 @@ public function isEmpty(): bool
6868
{
6969
$this->collectLogs();
7070

71-
return $this->data !== [];
71+
return $this->data === [];
7272
}
7373

7474
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

user_guide_src/source/changelogs/v4.6.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Bugs Fixed
4444
- **Forge:** Fixed a bug in ``Postgre`` and ``SQLSRV`` where changing a column's default value using ``Forge::modifyColumn()`` method produced incorrect SQL syntax.
4545
- **Model:** Fixed a bug in ``Model::replace()`` where ``created_at`` field (when available) wasn't set correctly.
4646
- **Model:** Fixed a bug in ``Model::insertBatch()`` and ``Model::updateBatch()`` where casts were not applied to inserted or updated values.
47+
- **Toolbar:** Fixed bugs in ``Collectors\Logs`` that were preventing the "Logs" tab from appearing on the Debug Toolbar.
4748
- **Validation:** Fixed a bug in the ``FormatRules::valid_base64()`` validation rule that caused a TypeError when checking invalid base64 strings.
4849

4950
See the repo's

0 commit comments

Comments
 (0)