Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\RecordValidator;
final class RefSchemaTest extends TestCase {
public function testAcceptsValuesThroughLocalRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'meta' => ['label' => 'example'], ], self::schemaWithRefs(), );
static::assertTrue($result->isValid()); }
public function testValidatesLocalRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'meta' => ['label' => 123], ], self::schemaWithRefs(), );
static::assertFalse($result->isValid()); static::assertSame(['$.meta.label'], \array_column($result->violations, 'path')); }
public function testValidatesExternalRefs(): void { $validator = new RecordValidator(static fn (string $nsid): ?array => ( $nsid === 'com.example.profile' ? [ 'lexicon' => 1, 'id' => 'com.example.profile', 'defs' => [ 'main' => [ 'type' => 'object', 'required' => ['handle'], 'properties' => [ 'handle' => ['type' => 'string'], ], ], ], ] : NULL ));
$result = $validator->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'author' => [], ], self::schemaWithRefs(), );
static::assertFalse($result->isValid()); static::assertSame(['$.author.handle'], \array_column($result->violations, 'path')); }
public function testAcceptsValuesThroughExternalRefs(): void { $validator = new RecordValidator(static fn (string $nsid): ?array => $nsid === 'com.example.profile' ? self::profileLexicon() : NULL);
$result = $validator->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'author' => ['handle' => 'alice.test.example.com'], ], self::schemaWithRefs(), );
static::assertTrue($result->isValid()); }
public function testReportsUnresolvedExternalRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello world', 'author' => ['handle' => 'alice.test.example.com'], ], self::schemaWithRefs(), );
static::assertFalse($result->isValid()); static::assertSame(['$.author'], \array_column($result->violations, 'path')); static::assertSame(['unresolved_ref'], \array_column($result->violations, 'code')); }
/** * @return array<string, mixed> */ private static function schemaWithRefs(): array { return [ 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], 'meta' => ['ref' => '#metadata'], 'author' => ['ref' => 'com.example.profile'], ], ], ], 'metadata' => [ 'type' => 'object', 'required' => ['label'], 'properties' => [ 'label' => ['type' => 'string'], ], ], ], ]; }
/** @return array<string, mixed> */ private static function profileLexicon(): array { return [ 'lexicon' => 1, 'id' => 'com.example.profile', 'defs' => [ 'main' => [ 'type' => 'object', 'required' => ['handle'], 'properties' => [ 'handle' => ['type' => 'string', 'format' => 'handle'], ], ], ], ]; }
}