Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310<?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\RecordSchema;use Wotnak\Atproto\Lexicon\Schema\SchemaInterface;use Wotnak\Atproto\Lexicon\Schema\TokenSchema;use Wotnak\Atproto\Lexicon\SchemaCompiler;
final class SchemaCompilerCompoundIntegrationTest extends TestCase {
#[DataProvider('tokenProvider')] public function testTokenSchemaIdentifiesItsToken(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition);
static::assertTrue($schema->safeValidate($tokenValue)->isValid()); }
#[DataProvider('tokenProvider')] public function testTokenSchemaParsesItsToken(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition);
$result = $schema->safeParse($tokenValue);
static::assertTrue($result->isValid()); static::assertSame($tokenValue, $result->value); }
#[DataProvider('tokenProvider')] public function testTokenSchemaSerializesToJson(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition);
static::assertSame(\json_encode($tokenValue, JSON_THROW_ON_ERROR), \json_encode($schema, JSON_THROW_ON_ERROR)); }
#[DataProvider('tokenProvider')] public function testTokenSchemaStringifiesToItsToken(string $definition, string $tokenValue): void { $schema = self::tokenSchema($definition);
static::assertSame($tokenValue, (string) $schema); }
/** @return array<string, array{string, string}> */ public static function tokenProvider(): array { return [ 'main' => ['main', 'com.example.token'], 'myToken' => ['myToken', 'com.example.token#myToken'], 'anotherToken' => ['anotherToken', 'com.example.token#anotherToken'], ]; }
public function testHandlesOpenAndClosedUnions(): void { $schema = self::unionRecord(); $known = [ '$type' => 'com.example.union', 'unionOpen' => [ '$type' => 'com.example.kitchenSink#object', 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', ], 'unionClosed' => [ '$type' => 'com.example.kitchenSink#subobject', 'boolean' => TRUE, ], ]; $openUnknown = [ '$type' => 'com.example.union', 'unionOpen' => ['$type' => 'com.example.other'], 'unionClosed' => [ '$type' => 'com.example.kitchenSink#subobject', 'boolean' => TRUE, ], ];
static::assertTrue($schema->safeParse($known)->isValid()); static::assertTrue($schema->safeParse($openUnknown)->isValid());
$missingTypes = $schema->safeParse([ '$type' => 'com.example.union', 'unionOpen' => [], 'unionClosed' => [], ]); static::assertFalse($missingTypes->isValid()); static::assertContains('$.unionOpen', \array_column($missingTypes->violations, 'path')); static::assertContains('missing_type', \array_column($missingTypes->violations, 'code'));
$closedUnknown = $schema->safeParse([ '$type' => 'com.example.union', 'unionOpen' => ['$type' => 'com.example.other'], 'unionClosed' => [ '$type' => 'com.example.other', 'boolean' => TRUE, ], ]); static::assertFalse($closedUnknown->isValid()); static::assertContains('$.unionClosed', \array_column($closedUnknown->violations, 'path')); static::assertContains('invalid_union_type', \array_column($closedUnknown->violations, 'code')); }
public function testHandlesUnknownObjects(): void { $schema = self::record([ 'lexicon' => 1, 'id' => 'com.example.unknown', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['unknown'], 'properties' => [ 'unknown' => ['type' => 'unknown'], 'optUnknown' => ['type' => 'unknown'], ], ], ], ], ]);
static::assertTrue( $schema->safeParse([ '$type' => 'com.example.unknown', 'unknown' => ['foo' => 'bar'], ])->isValid(), ); $missing = $schema->safeParse(['$type' => 'com.example.unknown']); static::assertFalse($missing->isValid()); static::assertContains('$.unknown', \array_column($missing->violations, 'path')); static::assertContains('missing_required', \array_column($missing->violations, 'code')); }
public function testCompilerAccessesDefinitionsWithUnsafeCharacters(): void { $compiler = self::unsafeCompiler(); $schema = $compiler->compileRef( 'com.example.4-2.unsafeDefs#ob-je-c$t', 'com.example.4-2.unsafeDefs', ); static::assertInstanceOf(SchemaInterface::class, $schema); $value = [ '$type' => 'com.example.4-2.unsafeDefs#ob-je-c$t', 'foo' => 'bar', ];
$result = $schema->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); }
public function testCompilerAccessesDefinitionsNamedWithReservedGeneratedIdentifiers(): void { $compiler = self::unsafeCompiler(); $schema = $compiler->compileRef('com.example.4-2.unsafeDefs#9', 'com.example.4-2.unsafeDefs'); static::assertInstanceOf(SchemaInterface::class, $schema);
$result = $schema->safeParse(9);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame(9, $result->value); }
private static function tokenSchema(string $definition): TokenSchema { $document = [ 'lexicon' => 1, 'id' => 'com.example.token', 'defs' => [ 'main' => ['type' => 'token', 'description' => 'Main token definition'], 'myToken' => ['type' => 'token', 'description' => 'A custom token'], 'anotherToken' => ['type' => 'token', 'description' => 'Another custom token'], ], ]; $compiler = self::compiler([$document]); $schema = $compiler->compileRef('com.example.token#' . $definition, 'com.example.token'); static::assertInstanceOf(TokenSchema::class, $schema); return $schema; }
private static function unionRecord(): RecordSchema { $union = [ 'lexicon' => 1, 'id' => 'com.example.union', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['unionOpen', 'unionClosed'], 'properties' => [ 'unionOpen' => [ 'type' => 'union', 'refs' => [ 'com.example.kitchenSink#object', 'com.example.kitchenSink#subobject', ], ], 'unionClosed' => [ 'type' => 'union', 'closed' => TRUE, 'refs' => [ 'com.example.kitchenSink#object', 'com.example.kitchenSink#subobject', ], ], ], ], ], ], ]; $compiler = self::compiler([self::kitchenSinkDocument(), $union]); $schema = $compiler->compileRecord('com.example.union'); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; }
private static function unsafeCompiler(): SchemaCompiler { return self::compiler([[ 'lexicon' => 1, 'id' => 'com.example.4-2.unsafeDefs', 'defs' => [ 'ob-je-c$t' => [ 'type' => 'object', 'properties' => [], ], 'ff-ri-n.g' => ['type' => 'string'], '9' => ['type' => 'integer', 'enum' => [9]], ], ]]); }
/** @param array<string, mixed> $document */ private static function record(array $document): RecordSchema { $compiler = self::compiler([$document]); $id = $document['id'] ?? NULL; static::assertIsString($id); $schema = $compiler->compileRecord($id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; }
/** * @param list<array<string, mixed>> $documents */ private static function compiler(array $documents): SchemaCompiler { $repository = new InMemoryLexiconRepository(); foreach ($documents as $raw) { $result = new LexiconParser()->parse($raw); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository->add($result->value); } return new SchemaCompiler($repository); }
/** @return array<string, mixed> */ private static function kitchenSinkDocument(): array { return [ 'lexicon' => 1, 'id' => 'com.example.kitchenSink', 'defs' => [ 'main' => [ 'type' => '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'], ], ], 'subobject' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean'], ], ], ], ]; }
}