Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\InMemoryLexiconRepository;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\Schema\ObjectSchema;use Wotnak\Atproto\Lexicon\Schema\RecordSchema;use Wotnak\Atproto\Lexicon\Schema\SchemaInterface;use Wotnak\Atproto\Lexicon\SchemaCompiler;
final class LexiconSchemaBuilderTest extends TestCase {
public function testValidatesRecordsCorrectly(): void { $schema = self::schema('com.example.kitchenSink#main'); static::assertInstanceOf(RecordSchema::class, $schema);
$value = [ '$type' => 'com.example.kitchenSink', 'object' => [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'refToEnumWithDefault' => 'option3', ], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'datetime' => '2026-08-29T12:00:00.000Z', 'atUri' => 'at://did:web:example.com/com.example.test/self', 'did' => 'did:web:example.com', 'cid' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a', 'bytes' => ['$bytes' => 'AAECAw=='], 'cidLink' => ['$link' => 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'], ];
$result = $schema->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); }
public function testValidatesObjectsCorrectlyAndAppliesDefaultsThroughRefs(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema);
$value = self::validObject(); $result = $schema->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value + ['refToEnumWithDefault' => 'option3'], $result->value); }
public function testRejectsInvalidEnumValues(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject() + ['refToEnumWithDefault' => 'invalidOption'];
$result = $schema->safeParse($value);
static::assertFalse($result->isValid()); static::assertSame(['invalid_enum'], \array_column($result->violations, 'code')); static::assertSame(['$.refToEnumWithDefault'], \array_column($result->violations, 'path')); }
public function testDoesNotApplyDefaultsWhenValidating(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject();
$result = $schema->safeValidate($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); }
public function testAllowsMissingOptionalRecordFields(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema);
$result = $schema->safeValidate(self::validObject());
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
public function testRejectsMissingRequiredRecordFields(): void { $schema = self::schema('com.example.kitchenSink#object'); static::assertInstanceOf(ObjectSchema::class, $schema); $value = self::validObject(); unset($value['array']);
$result = $schema->safeParse($value);
static::assertFalse($result->isValid()); static::assertSame(['missing_required'], \array_column($result->violations, 'code')); static::assertSame(['$.array'], \array_column($result->violations, 'path')); }
public function testRejectsReferencesWithMultipleHashSegments(): void { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => 'com.example.invalid', 'defs' => [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.invalid#test#test'], ], ], ], ]);
static::assertFalse($result->isValid()); static::assertContains('invalid_ref', \array_column($result->violations, 'code')); static::assertContains('$.defs.main.properties.test.ref', \array_column($result->violations, 'path')); }
public function testReportsMissingExternalLexiconWhenResolvingReference(): void { $document = new LexiconDocument( 'com.example.invalid', [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.missing#main'], ], ], ], [], ); $repository = new InMemoryLexiconRepository(); $repository->add($document); $schema = new SchemaCompiler($repository)->compileRef('com.example.invalid', 'com.example.invalid'); static::assertInstanceOf(ObjectSchema::class, $schema);
$result = $schema->safeValidate(['test' => []]);
static::assertFalse($result->isValid()); static::assertSame(['unresolved_ref'], \array_column($result->violations, 'code')); static::assertSame(['$.test'], \array_column($result->violations, 'path')); }
/** @param array<string, mixed> $schema */ #[DataProvider('incompleteSchemaProvider')] public function testCompilerRejectsIncompleteSchemas(array $schema, string $message): void { $document = new LexiconDocument('com.example.invalid', ['main' => ['type' => 'integer']], []); $compiler = new SchemaCompiler(new InMemoryLexiconRepository());
$this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage($message);
$compiler->compileSchema($document, $schema); }
/** * @return array<string, array{array<string, mixed>, string}> */ public static function incompleteSchemaProvider(): array { return [ 'array items' => [['type' => 'array'], 'item schema'], 'reference' => [['type' => 'ref'], 'reference'], ]; }
public function testCompilerRejectsIncompleteRecordDefinition(): void { $document = new LexiconDocument('com.example.invalid', ['main' => ['type' => 'record']], []); $repository = new InMemoryLexiconRepository(); $repository->add($document);
$this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('record schema');
new SchemaCompiler($repository)->compileRef('com.example.invalid', 'com.example.invalid'); }
public function testRejectsMissingLocalDefinitionWhenResolvingReference(): void { $result = new LexiconParser()->parse([ 'lexicon' => 1, 'id' => 'com.example.invalid', 'defs' => [ 'main' => [ 'type' => 'object', 'properties' => [ 'test' => ['type' => 'ref', 'ref' => 'com.example.invalid#nonexistent'], ], ], ], ]);
static::assertFalse($result->isValid()); static::assertContains('invalid_ref_target', \array_column($result->violations, 'code')); static::assertContains('$.defs.main.properties.test.ref', \array_column($result->violations, 'path')); }
private static function schema(string $ref): SchemaInterface { $result = new LexiconParser()->parse(self::kitchenSinkDocument()); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository = new InMemoryLexiconRepository(); $repository->add($result->value); $schema = new SchemaCompiler($repository)->compileRef($ref, 'com.example.kitchenSink'); static::assertInstanceOf(SchemaInterface::class, $schema); return $schema; }
/** * @return array<string, mixed> */ private static function validObject(): array { return [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', ]; }
/** * @return array<string, mixed> */ private static function kitchenSinkDocument(): array { return [ 'lexicon' => 1, 'id' => 'com.example.kitchenSink', 'defs' => [ 'main' => [ 'type' => 'record', 'description' => 'A record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['object', 'array', 'boolean', 'integer', 'string', 'bytes', 'cidLink'], 'properties' => [ 'object' => ['type' => 'ref', 'ref' => '#object'], 'array' => ['type' => 'array', 'items' => ['type' => 'string']], 'boolean' => ['type' => 'boolean'], 'integer' => ['type' => 'integer'], 'string' => ['type' => 'string'], 'bytes' => ['type' => 'bytes'], 'cidLink' => ['type' => 'cid-link'], ], ], ], 'object' => [ 'type' => 'object', 'required' => ['object', 'array', 'boolean', 'integer', 'string'], 'properties' => [ 'object' => ['type' => 'ref', 'ref' => '#subObject'], 'array' => ['type' => 'array', 'items' => ['type' => 'string']], 'boolean' => ['type' => 'boolean'], 'integer' => ['type' => 'integer'], 'string' => ['type' => 'string'], 'refToEnumWithDefault' => ['type' => 'ref', 'ref' => '#enumWithDefault'], ], ], 'subObject' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean'], ], ], 'enumWithDefault' => [ 'type' => 'string', 'default' => 'option3', 'enum' => ['option1', 'option2', 'option3'], ], ], ]; }
}