Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\InMemoryLexiconRepository;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\RecordValidator;use Wotnak\Atproto\Lexicon\Schema\ObjectSchema;use Wotnak\Atproto\Lexicon\Schema\RecordSchema;use Wotnak\Atproto\Lexicon\Schema\StringSchema;
final class RecordSchemaTest extends TestCase {
public function testValidRecordPasses(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'count' => 5, 'tags' => [], ], self::schema(), );
static::assertTrue($result->isValid()); }
public function testReportsMissingRequiredAndInvalidTypes(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.like', 'text' => 123, 'count' => 20, 'tags' => ['ok', 99], ], self::schema(), );
static::assertFalse($result->isValid()); static::assertSame( [ '$type', '$.text', '$.count', '$.tags[1]', ], \array_map(static fn ($violation): string => $violation->path, $result->violations), ); }
public function testRequiresRecordType(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', ['text' => 'Hello world'], self::schema(), );
static::assertFalse($result->isValid()); static::assertSame(['$type'], \array_column($result->violations, 'path')); }
public function testUnknownFieldsMustStillFollowTheDataModel(): void { $result = new RecordValidator()->validateRecord( 'app.bsky.feed.post', [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'futureField' => 1.5, ], self::schema(), );
static::assertFalse($result->isValid()); static::assertSame(['$.futureField'], \array_column($result->violations, 'path')); static::assertSame(['invalid_lex_value'], \array_column($result->violations, 'code')); }
public function testBuildAddsAndOverwritesTypeWithoutMutatingInput(): void { $schema = self::compiledRecordSchema(); $input = ['$type' => 'app.bsky.feed.like', 'text' => 'Hello world'];
$built = $schema->build($input);
static::assertSame( [ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ], $built, ); static::assertSame('app.bsky.feed.like', $input['$type']); static::assertTrue($schema->safeValidate($built)->isValid()); }
public function testExposesConfiguredRecordKeyType(): void { static::assertSame('tid', self::compiledRecordSchema()->key()); static::assertSame( 'any', new RecordSchema('app.bsky.feed.post', 'any', new ObjectSchema([]))->key(), ); static::assertSame( 'literal:self', new RecordSchema('app.bsky.feed.post', 'literal:self', new ObjectSchema([]))->key(), ); }
public function testValidatesAnyRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'any', new ObjectSchema([]));
static::assertTrue($schema->validateKey('anyStringKey')->isValid()); static::assertTrue($schema->validateKey('key123')->isValid()); static::assertTrue($schema->validateKey('key-with-dashes')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); static::assertFalse($schema->validateKey('invalid/key')->isValid()); static::assertFalse($schema->validateKey(123)->isValid()); }
public function testValidatesTidRecordKeys(): void { $schema = self::compiledRecordSchema();
static::assertTrue($schema->keySchema()->safeValidate('3jzfcijpj2z2a')->isValid()); static::assertFalse($schema->validateKey('not-a-tid')->isValid()); static::assertFalse($schema->validateKey('3jzfcijpj2z2!')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); static::assertFalse($schema->validateKey('regularString')->isValid()); }
public function testValidatesNsidRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'nsid', new ObjectSchema([]));
static::assertTrue($schema->validateKey('app.bsky.feed.post')->isValid()); static::assertTrue($schema->validateKey('com.example.app.feature.action')->isValid()); static::assertFalse($schema->validateKey('not-an-nsid')->isValid()); static::assertFalse($schema->validateKey('app.bsky.feed!')->isValid()); static::assertFalse($schema->validateKey('')->isValid()); }
public function testValidatesSelfLiteralRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'literal:self', new ObjectSchema([]));
static::assertTrue($schema->keySchema()->safeParse('self')->isValid()); static::assertFalse($schema->keySchema()->safeParse('other')->isValid()); static::assertFalse($schema->keySchema()->safeParse('Self')->isValid()); static::assertFalse($schema->keySchema()->safeParse('')->isValid()); }
public function testValidatesLiteralRecordKeys(): void { $schema = new RecordSchema('app.bsky.feed.post', 'literal:customKey', new ObjectSchema([]));
static::assertTrue($schema->validateKey('customKey')->isValid()); static::assertFalse($schema->validateKey('otherKey')->isValid()); static::assertFalse($schema->validateKey('custom')->isValid()); static::assertSame('customKey', $schema->keySchema()->safeParse(NULL)->value); }
public function testValidatesRecordTypesWithHashFragments(): void { $schema = new RecordSchema( 'app.bsky.feed.post#main', 'any', new ObjectSchema(['text' => new StringSchema()], required: ['text']), );
$valid = $schema->safeValidate([ '$type' => 'app.bsky.feed.post#main', 'text' => 'Hello world', ]); $withoutHash = $schema->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', ]); $differentHash = $schema->safeValidate([ '$type' => 'app.bsky.feed.post#other', 'text' => 'Hello world', ]);
static::assertTrue($valid->isValid()); static::assertFalse($withoutHash->isValid()); static::assertFalse($differentHash->isValid()); }
public function testRejectsUnsupportedRecordKeyTypes(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unsupported record key type');
new RecordSchema('app.bsky.feed.post', 'literal:invalid/key', new ObjectSchema([])); }
public function testRecordValidatorValidatesOptionalRecordKey(): void { $repository = new InMemoryLexiconRepository(); $repository->add(new LexiconDocument( 'app.bsky.feed.post', [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => ['text' => ['type' => 'string']], ], ], ], [], )); $validator = new RecordValidator($repository); $record = ['$type' => 'app.bsky.feed.post', 'text' => 'Hello world'];
$valid = $validator->validateRepositoryRecord( 'app.bsky.feed.post', $record, recordKey: '3jzfcijpj2z2a', ); $invalid = $validator->validateRepositoryRecord( 'app.bsky.feed.post', $record, recordKey: 'not-a-tid', );
static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame(['$rkey'], \array_column($invalid->violations, 'path')); static::assertSame(['invalid_format'], \array_column($invalid->violations, 'code')); }
public function testRejectsNonStringNullAndNonNormalizedTypes(): void { $schema = self::compiledRecordSchema();
foreach ([NULL, 42, TRUE, [], 'APP.BSKY.FEED.POST', 'app.bsky.feed.post#main'] as $type) { $result = $schema->safeValidate(['$type' => $type, 'text' => 'Hello world']); static::assertFalse($result->isValid()); static::assertContains('$type', \array_column($result->violations, 'path')); } }
private static function compiledRecordSchema(): RecordSchema { return new RecordSchema( 'app.bsky.feed.post', 'tid', new ObjectSchema(['text' => new StringSchema()], required: ['text']), ); }
/** * @return array<string, mixed> */ private static function schema(): array { return [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => [ 'type' => 'string', 'maxLength' => 100, ], 'count' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 10, ], 'tags' => [ 'type' => 'array', 'items' => ['type' => 'string'], ], ], ]; }
}