Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183<?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\BooleanSchema;use Wotnak\Atproto\Lexicon\Schema\IntegerSchema;use Wotnak\Atproto\Lexicon\Schema\ObjectSchema;use Wotnak\Atproto\Lexicon\Schema\StringSchema;
final class ObjectSchemaTest extends TestCase {
public function testValidatesRequiredOptionalNullableAndExtraProperties(): void { $schema = self::personSchema();
static::assertTrue( $schema->safeValidate([ 'name' => 'Alice', 'age' => 30, 'gender' => 'female', ])->isValid(), ); static::assertTrue($schema->safeValidate(['name' => 'Alice'])->isValid()); static::assertTrue( $schema->safeValidate([ 'name' => 'Alice', 'gender' => NULL, ])->isValid(), ); static::assertTrue( $schema->safeValidate([ 'name' => 'Alice', 'age' => 30, 'extra' => 'value', ])->isValid(), ); }
public function testReportsMissingAndInvalidProperties(): void { $missing = self::personSchema() ->safeValidate([ 'age' => 30, 'gender' => 'female', ]); static::assertFalse($missing->isValid()); static::assertSame(['$.name'], \array_column($missing->violations, 'path'));
$invalid = self::personSchema() ->safeValidate([ 'name' => 'Alice', 'age' => 'thirty', 'gender' => 'unknown', ]); static::assertFalse($invalid->isValid()); static::assertSame(['$.age', '$.gender'], \array_column($invalid->violations, 'path')); }
#[DataProvider('nonObjectProvider')] public function testRejectsNonObjects(mixed $value): void { static::assertFalse(self::personSchema()->safeValidate($value)->isValid()); }
/** @return array<string, array{mixed}> */ public static function nonObjectProvider(): array { return [ 'null' => [NULL], 'string' => ['not an object'], 'integer' => [42], 'boolean' => [TRUE], 'empty list' => [[]], 'list' => [['Alice', 30]], ]; }
public function testAcceptsNativeEmptyJsonObject(): void { $value = new \stdClass(); $result = new ObjectSchema([])->safeValidate($value);
static::assertTrue($result->isValid()); static::assertEquals($value, $result->value); static::assertSame('{}', \json_encode($result->value, JSON_THROW_ON_ERROR)); }
/** * @param array<string, mixed> $value */ #[DataProvider('compoundObjectProvider')] public function testRejectsCompoundDataAsAnObject(array $value): void { $result = new ObjectSchema([])->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame(['invalid_type'], \array_column($result->violations, 'code')); }
/** * @return array<string, array{array<string, mixed>}> */ public static function compoundObjectProvider(): array { return [ 'CID link' => [[ '$link' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', ]], 'bytes' => [['$bytes' => 'SGVsbG8=']], 'blob' => [[ '$type' => 'blob', 'ref' => ['$link' => 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4'], 'mimeType' => 'image/jpeg', 'size' => 10_000, ]], ]; }
public function testSafeParseAppliesDefaultsToOmittedFieldsIncludingRequiredFields(): void { $schema = new ObjectSchema( properties: [ 'name' => new StringSchema(default: 'fallback'), 'enabled' => new BooleanSchema(default: FALSE), 'count' => new IntegerSchema(default: 0), 'nullable' => new StringSchema(default: 'not-used'), 'required' => new StringSchema(default: 'not-used'), ], required: ['required'], nullable: ['nullable'], );
$result = $schema->safeParse(['nullable' => NULL]);
static::assertTrue($result->isValid()); static::assertSame( [ 'nullable' => NULL, 'name' => 'fallback', 'enabled' => FALSE, 'count' => 0, 'required' => 'not-used', ], $result->value, );
$validated = $schema->safeValidate(['nullable' => NULL]); static::assertFalse($validated->isValid()); static::assertSame(['$.required'], \array_column($validated->violations, 'path')); }
public function testSafeParseValidatesInjectedDefaultsAgainstConstraints(): void { $schema = new ObjectSchema([ 'name' => new StringSchema(minLength: 5, default: 'bad'), ]);
$result = $schema->safeParse(new \stdClass());
static::assertFalse($result->isValid()); static::assertSame(['$.name'], \array_column($result->violations, 'path')); static::assertSame(['min_length'], \array_column($result->violations, 'code')); }
public function testUnknownPropertiesArePreservedWithoutSchemaValidation(): void { $value = [ 'nested' => ['score' => 1.5], ]; $result = new ObjectSchema([])->safeParse($value);
static::assertTrue($result->isValid()); static::assertSame($value, $result->value); }
private static function personSchema(): ObjectSchema { return new ObjectSchema( properties: [ 'name' => new StringSchema(), 'age' => new IntegerSchema(), 'gender' => new StringSchema(enum: ['male', 'female']), ], required: ['name'], nullable: ['gender'], ); }
}