|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 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; |
| 15 | + |
| 16 | +use CodeIgniter\CodeIgniter; |
| 17 | +use CodeIgniter\Config\Factories; |
| 18 | +use CodeIgniter\Config\Services; |
| 19 | +use CodeIgniter\HTTP\IncomingRequest; |
| 20 | +use CodeIgniter\HTTP\ResponseInterface; |
| 21 | +use CodeIgniter\Test\CIUnitTestCase; |
| 22 | +use Config\Toolbar as ToolbarConfig; |
| 23 | +use PHPUnit\Framework\Attributes\BackupGlobals; |
| 24 | +use PHPUnit\Framework\Attributes\Group; |
| 25 | + |
| 26 | +/** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | +#[BackupGlobals(true)] |
| 30 | +#[Group('Others')] |
| 31 | +final class ToolbarTest extends CIUnitTestCase |
| 32 | +{ |
| 33 | + private ToolbarConfig $config; |
| 34 | + private ?IncomingRequest $request = null; |
| 35 | + private ?ResponseInterface $response = null; |
| 36 | + |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + Services::reset(); |
| 41 | + |
| 42 | + is_cli(false); |
| 43 | + |
| 44 | + $this->config = new ToolbarConfig(); |
| 45 | + |
| 46 | + // Mock CodeIgniter core service to provide performance stats |
| 47 | + $app = $this->createMock(CodeIgniter::class); |
| 48 | + $app->method('getPerformanceStats')->willReturn([ |
| 49 | + 'startTime' => microtime(true), |
| 50 | + 'totalTime' => 0.05, |
| 51 | + ]); |
| 52 | + Services::injectMock('codeigniter', $app); |
| 53 | + } |
| 54 | + |
| 55 | + protected function tearDown(): void |
| 56 | + { |
| 57 | + // Restore is_cli state |
| 58 | + is_cli(true); |
| 59 | + |
| 60 | + parent::tearDown(); |
| 61 | + } |
| 62 | + |
| 63 | + public function testPrepareRespectsDisableOnHeaders(): void |
| 64 | + { |
| 65 | + // Set up the new configuration property |
| 66 | + $this->config->disableOnHeaders = ['HX-Request' => 'true']; |
| 67 | + Factories::injectMock('config', 'Toolbar', $this->config); |
| 68 | + |
| 69 | + // Initialize Request with the custom header |
| 70 | + $this->request = service('incomingrequest', null, false); |
| 71 | + $this->request->setHeader('HX-Request', 'true'); |
| 72 | + |
| 73 | + // Initialize Response |
| 74 | + $this->response = service('response', null, false); |
| 75 | + $this->response->setBody('<html><body>Content</body></html>'); |
| 76 | + $this->response->setHeader('Content-Type', 'text/html'); |
| 77 | + |
| 78 | + $toolbar = new Toolbar($this->config); |
| 79 | + $toolbar->prepare($this->request, $this->response); |
| 80 | + |
| 81 | + // Assertions |
| 82 | + $this->assertTrue($this->response->hasHeader('Debugbar-Time')); |
| 83 | + $this->assertStringNotContainsString('id="debugbar_loader"', (string) $this->response->getBody()); |
| 84 | + } |
| 85 | + |
| 86 | + public function testPrepareInjectsNormallyWithoutIgnoredHeader(): void |
| 87 | + { |
| 88 | + $this->config->disableOnHeaders = ['HX-Request' => 'true']; |
| 89 | + Factories::injectMock('config', 'Toolbar', $this->config); |
| 90 | + |
| 91 | + $this->request = service('incomingrequest', null, false); |
| 92 | + $this->response = service('response', null, false); |
| 93 | + $this->response->setBody('<html><body>Content</body></html>'); |
| 94 | + $this->response->setHeader('Content-Type', 'text/html'); |
| 95 | + |
| 96 | + $toolbar = new Toolbar($this->config); |
| 97 | + $toolbar->prepare($this->request, $this->response); |
| 98 | + |
| 99 | + // Assertions |
| 100 | + $this->assertStringContainsString('id="debugbar_loader"', (string) $this->response->getBody()); |
| 101 | + } |
| 102 | +} |
0 commit comments