|
| 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 | +/** |
| 17 | + * Class MockNativeHeaders |
| 18 | + * |
| 19 | + * This class serves as a container to hold the state of HTTP headers |
| 20 | + * during unit testing. It allows the framework to simulate sending headers |
| 21 | + * without actually outputting them to the CLI or browser. |
| 22 | + */ |
| 23 | +class MockNativeHeaders |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Simulates the state of whether headers have been sent. |
| 27 | + */ |
| 28 | + public static bool $headersSent = false; |
| 29 | + |
| 30 | + /** |
| 31 | + * Stores the list of headers that have been sent. |
| 32 | + */ |
| 33 | + public static array $headers = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Resets the class state to defaults. |
| 37 | + * Useful for cleaning up between individual tests. |
| 38 | + */ |
| 39 | + public static function reset(): void |
| 40 | + { |
| 41 | + self::$headersSent = false; |
| 42 | + self::$headers = []; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Mock implementation of the native PHP headers_sent() function. |
| 48 | + * |
| 49 | + * Instead of checking the actual PHP output buffer, this function |
| 50 | + * checks the static property in MockNativeHeaders. |
| 51 | + * |
| 52 | + * @return bool True if headers are considered sent, false otherwise. |
| 53 | + */ |
| 54 | +function headers_sent(): bool |
| 55 | +{ |
| 56 | + return MockNativeHeaders::$headersSent; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Mock implementation of the native PHP headers_list() function. |
| 61 | + * |
| 62 | + * Retrieves the array of headers stored in the MockNativeHeaders class |
| 63 | + * rather than the actual headers sent by the server. |
| 64 | + * |
| 65 | + * @return array The list of simulated headers. |
| 66 | + */ |
| 67 | +function headers_list(): array |
| 68 | +{ |
| 69 | + return MockNativeHeaders::$headers; |
| 70 | +} |
0 commit comments