Files
duckbrain-toolbox/packages/test/tests/Support/HttpStubs.php
T
2026-10-05 15:08:39 -03:00

64 lines
1.6 KiB
PHP

<?php
namespace Tests\Support;
/**
* Colector estático de headers y cookies emitidos durante los tests.
*
* En el SAPI CLI, `header()` y `setcookie()` son no-op y `headers_list()`
* queda vacío; las funciones de `tests/Support/http_stubs.php` registran
* aquí lo que la aplicación intenta emitir.
*/
class HttpStubs
{
public static array $headers = [];
public static array $cookies = [];
public static function header(string $value, bool $replace = true, int $responseCode = 0): void
{
if (preg_match('/^HTTP\/\S+\s+(\d{3})/', $value, $matches)) {
http_response_code((int) $matches[1]);
}
if ($responseCode > 0) {
http_response_code($responseCode);
}
static::$headers[] = $value;
}
public static function setcookie(
string $name,
string $value = '',
int $expires = 0,
string $path = '',
string $domain = '',
bool $secure = false,
bool $httponly = false
): void {
static::$cookies[$name] = $value;
}
public static function reset(): void
{
static::$headers = [];
static::$cookies = [];
}
public static function headerValue(string $prefix): ?string
{
foreach (static::$headers as $header) {
if (stripos($header, $prefix) === 0) {
return substr($header, strlen($prefix));
}
}
return null;
}
public static function hasHeader(string $prefix): bool
{
return static::headerValue($prefix) !== null;
}
}