Files
nat-as-server/tests/Unit/ValidateTest.php
2026-06-04 21:40:59 +02:00

56 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use Tests\Support\JsonErrorResponse;
final class ValidateTest extends TestCase
{
protected function setUp(): void
{
if (!defined('QDB_TESTING')) {
throw new \RuntimeException('PHPUnit bootstrap must define QDB_TESTING');
}
require_once dirname(__DIR__, 2) . '/lib/validate.php';
}
public function testRequireFieldsPasses(): void
{
$body = ['a' => '1', 'b' => 'x'];
require_fields($body, ['a', 'b']);
$this->assertSame(['a' => '1', 'b' => 'x'], $body);
}
public function testRequireFieldsMissing(): void
{
$this->expectException(JsonErrorResponse::class);
$this->expectExceptionMessage('Required fields');
try {
require_fields(['a' => ''], ['a', 'b']);
} catch (JsonErrorResponse $e) {
$this->assertSame('MISSING_FIELDS', $e->errorCode);
$this->assertSame(400, $e->httpStatus);
throw $e;
}
}
public function testValidateStringTrims(): void
{
$s = validate_string(' hello ', 'field', 1, 20);
$this->assertSame('hello', $s);
}
public function testValidateEnum(): void
{
$this->assertSame('admin', validate_enum('admin', 'role', ['admin', 'coach']));
}
public function testValidateIntBounds(): void
{
$this->assertSame(5, validate_int('5', 'n', 1, 10));
}
}