forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbarTest.php
More file actions
104 lines (84 loc) · 3.22 KB
/
ToolbarTest.php
File metadata and controls
104 lines (84 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Debug;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Toolbar as ToolbarConfig;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[BackupGlobals(true)]
#[Group('Others')]
final class ToolbarTest extends CIUnitTestCase
{
private ToolbarConfig $config;
private ?IncomingRequest $request = null;
private ?ResponseInterface $response = null;
private bool $originalIsCli;
protected function setUp(): void
{
parent::setUp();
Services::reset();
$this->originalIsCli = is_cli();
is_cli(false);
$this->config = new ToolbarConfig();
// Mock CodeIgniter core service to provide performance stats
$app = $this->createMock(CodeIgniter::class);
$app->method('getPerformanceStats')->willReturn([
'startTime' => microtime(true),
'totalTime' => 0.05,
]);
Services::injectMock('codeigniter', $app);
}
protected function tearDown(): void
{
// Restore original is_cli state
is_cli($this->originalIsCli);
parent::tearDown();
}
public function testPrepareRespectsDisableOnHeaders(): void
{
// Set up the new configuration property
$this->config->disableOnHeaders = ['HX-Request'];
Factories::injectMock('config', 'Toolbar', $this->config);
// Initialize Request with the custom header
$this->request = service('incomingrequest', null, false);
$this->request->setHeader('HX-Request', 'true');
// Initialize Response
$this->response = service('response', null, false);
$this->response->setBody('<html><body>Content</body></html>');
$this->response->setHeader('Content-Type', 'text/html');
$toolbar = new Toolbar($this->config);
$toolbar->prepare($this->request, $this->response);
// Assertions
$this->assertTrue($this->response->hasHeader('Debugbar-Time'));
$this->assertStringNotContainsString('id="debugbar_loader"', (string) $this->response->getBody());
}
public function testPrepareInjectsNormallyWithoutIgnoredHeader(): void
{
$this->config->disableOnHeaders = ['HX-Request'];
Factories::injectMock('config', 'Toolbar', $this->config);
$this->request = service('incomingrequest', null, false);
$this->response = service('response', null, false);
$this->response->setBody('<html><body>Content</body></html>');
$this->response->setHeader('Content-Type', 'text/html');
$toolbar = new Toolbar($this->config);
$toolbar->prepare($this->request, $this->response);
// Assertions
$this->assertStringContainsString('id="debugbar_loader"', (string) $this->response->getBody());
}
}