Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\Schema\ArraySchema;use Wotnak\Atproto\Lexicon\Schema\BooleanSchema;use Wotnak\Atproto\Lexicon\Schema\IntegerSchema;use Wotnak\Atproto\Lexicon\Schema\ParamsSchema;use Wotnak\Atproto\Lexicon\Schema\StringSchema;
final class ParamsSchemaTest extends TestCase {
/** @param array<string, mixed> $value */ #[DataProvider('validParametersProvider')] public function testAcceptsParameterValues(array $value): void { static::assertTrue( new ParamsSchema([]) ->safeValidate($value) ->isValid(), ); }
/** * @return array<string, array{array<string, mixed>}> */ public static function validParametersProvider(): array { return [ 'empty object' => [[]], 'string' => [['name' => 'Alice']], 'empty string' => [['name' => '']], 'integer' => [['age' => 30]], 'negative integer' => [['offset' => -5]], 'boolean' => [['active' => TRUE]], 'empty array' => [['tags' => []]], 'string array' => [['tags' => ['one', 'two']]], 'integer array' => [['ids' => [1, 2, 3]]], 'boolean array' => [['flags' => [TRUE, FALSE]]], ]; }
#[DataProvider('invalidParametersProvider')] public function testRejectsInvalidParameterValues(mixed $value, string $path): void { $result = new ParamsSchema([])->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame([$path], \array_column($result->violations, 'path')); }
/** * @return array<string, array{mixed, string}> */ public static function invalidParametersProvider(): array { return [ 'null' => [NULL, '$'], 'string' => ['name=Alice', '$'], 'list' => [['Alice'], '$'], 'null value' => [['value' => NULL], '$.value'], 'float' => [['value' => 3.14], '$.value'], 'object' => [['value' => ['nested' => 'text']], '$.value'], 'mixed scalar array' => [['value' => ['one', 2]], '$.value'], 'nested array' => [['value' => [['nested']]], '$.value'], ]; }
public function testValidatesDeclaredParameters(): void { $schema = new ParamsSchema( [ 'name' => new StringSchema(), 'age' => new IntegerSchema(minimum: 0), 'active' => new BooleanSchema(), 'tags' => new ArraySchema(new StringSchema()), ], ['name'], );
$valid = $schema->safeValidate([ 'name' => 'Alice', 'age' => 30, 'active' => TRUE, 'tags' => ['one', 'two'], ]); $invalid = $schema->safeValidate([ 'age' => -1, 'active' => 'true', 'tags' => ['one', 2], ]);
static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame( ['$.name', '$.age', '$.active', '$.tags[1]'], \array_column($invalid->violations, 'path'), ); }
public function testSafeParseAppliesDeclaredDefaults(): void { $schema = new ParamsSchema([ 'limit' => new IntegerSchema(default: 50), 'reverse' => new BooleanSchema(default: FALSE), ]);
$result = $schema->safeParse([]);
static::assertTrue($result->isValid()); static::assertSame(['limit' => 50, 'reverse' => FALSE], $result->value); }
public function testSafeParseAppliesDefaultsBeforeCheckingRequiredParameters(): void { $schema = new ParamsSchema( ['limit' => new IntegerSchema(default: 50)], ['limit'], );
$parsed = $schema->safeParse([]); $validated = $schema->safeValidate([]);
static::assertTrue($parsed->isValid()); static::assertSame(['limit' => 50], $parsed->value); static::assertFalse($validated->isValid()); static::assertSame(['missing_required'], \array_column($validated->violations, 'code')); }
public function testSafeParseValidatesInjectedDefaultsAgainstConstraints(): void { $schema = new ParamsSchema([ 'name' => new StringSchema(minLength: 5, default: 'bad'), ]);
$result = $schema->safeParse([]);
static::assertFalse($result->isValid()); static::assertSame(['$.name'], \array_column($result->violations, 'path')); static::assertSame(['min_length'], \array_column($result->violations, 'code')); }
}