Skip to content

Commit 17cf621

Browse files
committed
tests: refactor tests to use MockCommon helpers
1 parent cce2477 commit 17cf621

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

system/Debug/Toolbar.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,6 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
392392

393393
$config = config(ToolbarConfig::class);
394394

395-
try {
396-
$stats = $app->getPerformanceStats();
397-
if (! isset($stats['startTime']) || ! isset($stats['totalTime'])) {
398-
return;
399-
}
400-
} catch (Throwable) {
401-
return;
402-
}
403-
404395
foreach ($config->disableOnHeaders as $header) {
405396
if ($request->hasHeader($header)) {
406397
$this->isCustomAjax = true;
@@ -409,6 +400,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
409400
}
410401

411402
$toolbar = service('toolbar', $config);
403+
$stats = $app->getPerformanceStats();
412404
$data = $toolbar->run(
413405
$stats['startTime'],
414406
$stats['totalTime'],

tests/system/Debug/ToolbarTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ final class ToolbarTest extends CIUnitTestCase
3333
private ToolbarConfig $config;
3434
private ?IncomingRequest $request = null;
3535
private ?ResponseInterface $response = null;
36+
private bool $originalIsCli;
3637

3738
protected function setUp(): void
3839
{
3940
parent::setUp();
4041
Services::reset();
4142

43+
$this->originalIsCli = is_cli();
44+
is_cli(false);
45+
4246
$this->config = new ToolbarConfig();
4347

4448
// Mock CodeIgniter core service to provide performance stats
@@ -50,6 +54,14 @@ protected function setUp(): void
5054
Services::injectMock('codeigniter', $app);
5155
}
5256

57+
protected function tearDown(): void
58+
{
59+
// Restore original is_cli state
60+
is_cli($this->originalIsCli);
61+
62+
parent::tearDown();
63+
}
64+
5365
public function testPrepareRespectsDisableOnHeaders(): void
5466
{
5567
// Set up the new configuration property
@@ -90,11 +102,3 @@ public function testPrepareInjectsNormallyWithoutIgnoredHeader(): void
90102
$this->assertStringContainsString('id="debugbar_loader"', (string) $this->response->getBody());
91103
}
92104
}
93-
94-
/**
95-
* Mock is_cli() to return false within this namespace.
96-
*/
97-
function is_cli(): bool
98-
{
99-
return false;
100-
}

0 commit comments

Comments
 (0)