|
14 | 14 | namespace CodeIgniter\Test\Utilities; |
15 | 15 |
|
16 | 16 | /** |
17 | | - * Class NativeHeadersStack |
18 | | - * |
19 | 17 | * A utility class for simulating native PHP header handling in unit tests. |
20 | | - * It allows the inspection, manipulation, and mocking of HTTP headers without |
21 | | - * affecting the actual HTTP output. |
22 | 18 | * |
23 | 19 | * @internal This class is for testing purposes only. |
24 | 20 | */ |
25 | 21 | final class NativeHeadersStack |
26 | 22 | { |
27 | | - private static bool $headersSent = false; |
28 | | - |
29 | | - /** |
30 | | - * @var array<string, list<string>> |
31 | | - */ |
32 | | - private static array $headers = []; |
33 | | - |
34 | | - private static ?int $responseCode = null; |
35 | | - |
36 | | - /** |
37 | | - * Resets the state of the class to its default values. |
38 | | - */ |
39 | | - public static function reset(): void |
40 | | - { |
41 | | - self::$headersSent = false; |
42 | | - self::$headers = []; |
43 | | - self::$responseCode = null; |
44 | | - } |
45 | | - |
46 | | - /** |
47 | | - * Sets the state of whether headers have been sent. |
48 | | - */ |
49 | | - public static function setHeadersSent(bool $sent): void |
50 | | - { |
51 | | - self::$headersSent = $sent; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Simulates PHP's native `headers_sent()` function. |
56 | | - */ |
57 | | - public static function headersSent(): bool |
58 | | - { |
59 | | - return self::$headersSent; |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Sets a header by name, replacing or appending it. |
64 | | - * This is the main method for header manipulation. |
65 | | - * |
66 | | - * @param string $header The header string (e.g., 'Content-Type: application/json'). |
67 | | - * @param bool $replace Whether to replace a previous similar header. |
68 | | - * @param int|null $responseCode Forces the HTTP response code to the specified value. |
69 | | - */ |
70 | | - public static function set(string $header, bool $replace = true, ?int $responseCode = null): void |
71 | | - { |
72 | | - if (str_contains($header, ':')) { |
73 | | - [$name, $value] = explode(':', $header, 2); |
74 | | - $name = trim($name); |
75 | | - $value = trim($value); |
76 | | - |
77 | | - if ($replace || ! isset(self::$headers[strtolower($name)])) { |
78 | | - self::$headers[strtolower($name)] = []; |
79 | | - } |
80 | | - self::$headers[strtolower($name)][] = "{$name}: {$value}"; |
81 | | - } else { |
82 | | - // Handle non-key-value headers like "HTTP/1.1 404 Not Found" |
83 | | - self::$headers['status'][] = $header; |
84 | | - } |
85 | | - |
86 | | - if ($responseCode !== null) { |
87 | | - self::$responseCode = $responseCode; |
88 | | - } |
89 | | - } |
90 | | - |
91 | 23 | /** |
92 | | - * Pushes a header to the stack without replacing existing ones. |
| 24 | + * Simulates whether headers have been sent. |
93 | 25 | */ |
94 | | - public static function push(string $header): void |
95 | | - { |
96 | | - self::set($header, false); |
97 | | - } |
| 26 | + public static bool $headersSent = false; |
98 | 27 |
|
99 | 28 | /** |
100 | | - * A convenience method to push multiple headers at once. |
| 29 | + * Stores the list of headers. |
101 | 30 | * |
102 | | - * @param list<string> $headers An array of headers to push onto the stack. |
| 31 | + * @var list<string> |
103 | 32 | */ |
104 | | - public static function pushMany(array $headers): void |
105 | | - { |
106 | | - foreach ($headers as $header) { |
107 | | - // Default to not replacing for multiple adds |
108 | | - self::set($header, false); |
109 | | - } |
110 | | - } |
| 33 | + public static array $headers = []; |
111 | 34 |
|
112 | 35 | /** |
113 | | - * Simulates PHP's `headers_list()` function. |
114 | | - * |
115 | | - * @return list<string> The list of simulated headers. |
| 36 | + * Resets the header stack to defaults. |
| 37 | + * Call this in setUp() to ensure clean state between tests. |
116 | 38 | */ |
117 | | - public static function listHeaders(): array |
| 39 | + public static function reset(): void |
118 | 40 | { |
119 | | - $list = []; |
120 | | - |
121 | | - foreach (self::$headers as $values) { |
122 | | - $list = array_merge($list, $values); |
123 | | - } |
124 | | - |
125 | | - return $list; |
| 41 | + self::$headersSent = false; |
| 42 | + self::$headers = []; |
126 | 43 | } |
127 | 44 |
|
128 | 45 | /** |
129 | | - * Checks if a header with the given name exists in the stack (case-insensitive). |
| 46 | + * Checks if a specific header exists in the stack. |
130 | 47 | * |
131 | | - * @param string $name The header name to search for (e.g., 'Content-Type'). |
| 48 | + * @param string $header The exact header string (e.g., 'Content-Type: text/html') |
132 | 49 | */ |
133 | | - public static function hasHeader(string $name): bool |
| 50 | + public static function has(string $header): bool |
134 | 51 | { |
135 | | - return isset(self::$headers[strtolower($name)]); |
| 52 | + return in_array($header, self::$headers, true); |
136 | 53 | } |
137 | 54 |
|
138 | 55 | /** |
139 | | - * Simulates PHP's `http_response_code()` function. |
| 56 | + * Adds a header to the stack. |
140 | 57 | * |
141 | | - * @return int|null The stored response code, or null if not set. |
| 58 | + * @param string $header The header to add (e.g., 'Content-Type: text/html') |
142 | 59 | */ |
143 | | - public static function getResponseCode(): ?int |
| 60 | + public static function push(string $header): void |
144 | 61 | { |
145 | | - return self::$responseCode; |
| 62 | + self::$headers[] = $header; |
146 | 63 | } |
147 | 64 | } |
0 commit comments