initial prototype based on nat-as-server
Some checks failed
PHPUnit / test (push) Has been cancelled
Some checks failed
PHPUnit / test (push) Has been cancelled
This commit is contained in:
55
tests/Unit/ValidateTest.php
Normal file
55
tests/Unit/ValidateTest.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user