first commit.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
/**
|
||||
* Respuesta capturada de una petición despachada in-process.
|
||||
*/
|
||||
class Response
|
||||
{
|
||||
public function __construct(
|
||||
public int $status,
|
||||
public string $body,
|
||||
public array $headers = [],
|
||||
public array $cookies = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public function header(string $name): ?string
|
||||
{
|
||||
foreach ($this->headers as $header) {
|
||||
if (stripos($header, $name . ':') === 0) {
|
||||
return trim(substr($header, strlen($name) + 1));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasHeader(string $name): bool
|
||||
{
|
||||
return $this->header($name) !== null;
|
||||
}
|
||||
|
||||
public function redirectTarget(): ?string
|
||||
{
|
||||
return $this->header('HX-Redirect') ?? $this->header('Location');
|
||||
}
|
||||
|
||||
public function trigger(): ?string
|
||||
{
|
||||
return $this->header('HX-Trigger');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Stubs por namespace de `header()` y `setcookie()`.
|
||||
*
|
||||
* Como el SAPI CLI no emite headers, estas funciones interceptan las llamadas
|
||||
* no cualificadas de los namespaces de la aplicación y las registran en
|
||||
* {@see \Tests\Support\HttpStubs}.
|
||||
*
|
||||
* La plantilla cubre únicamente el namespace del núcleo (`Libs`), que es el
|
||||
* único que emite cabeceras en Duckbrain. Si tu proyecto emite headers o
|
||||
* cookies desde otro namespace (`Models`, `Handlers`, `Controllers\...`, etc.),
|
||||
* copia el bloque de `Libs` cambiando el nombre del namespace. Las llamadas
|
||||
* desde namespaces no registrados aquí se ignorarán en los tests.
|
||||
*/
|
||||
|
||||
namespace Libs {
|
||||
function header(string $header, bool $replace = true, int $response_code = 0): void
|
||||
{
|
||||
\Tests\Support\HttpStubs::header($header, $replace, $response_code);
|
||||
}
|
||||
|
||||
function setcookie(string $name, string $value = '', int $expires = 0, string $path = '', string $domain = '', bool $secure = false, bool $httponly = false): void
|
||||
{
|
||||
\Tests\Support\HttpStubs::setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user