laravel/tests/bootstrap.php
2026-11-28 15:28:05 +02:00

68 lines
2 KiB
PHP

<?php
// Test helpers. Not part of the package.
spl_autoload_register(function ($class) {
$prefix = 'InterwebMz\\Laravel\\';
if (strncmp($class, $prefix, strlen($prefix)) === 0) {
$file = __DIR__ . '/../src/' . str_replace('\\', '/', substr($class, strlen($prefix))) . '.php';
if (is_file($file)) {
require $file;
}
}
});
/** Client that answers from a list instead of the network, and keeps what it was asked. */
class FakeClient extends \InterwebMz\Laravel\Client
{
/** @var list<array{0:int,1:array,2:string}> */
public $answers = [];
/** @var list<array{method:string,url:string,headers:array,body:?string}> */
public $sent = [];
protected function send(string $method, string $url, array $headers, ?string $payload): array
{
$this->sent[] = ['method' => $method, 'url' => $url, 'headers' => $headers, 'body' => $payload];
$a = array_shift($this->answers);
if ($a === null) {
return [0, [], '', 'no more fake answers'];
}
return [$a[0], $a[1] ?? [], is_string($a[2]) ? $a[2] : json_encode($a[2]), null];
}
public function header(int $i, string $name): ?string
{
foreach ($this->sent[$i]['headers'] as $h) {
if (stripos($h, $name . ':') === 0) {
return trim(substr($h, strlen($name) + 1));
}
}
return null;
}
}
/** Client that talks to another server (a test server), only for the live tests. */
class LiveClient extends \InterwebMz\Laravel\Client
{
protected function baseUrl(): string
{
return getenv('TEST_API_URL') ?: parent::baseUrl();
}
}
$GLOBALS['__fails'] = 0;
$GLOBALS['__count'] = 0;
function check(string $what, bool $ok): void
{
$GLOBALS['__count']++;
if (!$ok) {
$GLOBALS['__fails']++;
}
echo ($ok ? 'ok ' : 'FAIL ') . $what . PHP_EOL;
}
function done(): void
{
echo PHP_EOL . $GLOBALS['__count'] . ' checks, ' . $GLOBALS['__fails'] . ' failed' . PHP_EOL;
exit($GLOBALS['__fails'] ? 1 : 0);
}