Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250<?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\UnionSchema;use Wotnak\Atproto\Lexicon\SchemaCompiler;
final class UnionSchemaTest extends TestCase {
public function testClosedUnionValidatesEachKnownVariant(): void { $schema = self::typedUnion(TRUE);
static::assertTrue( $schema->safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', 'age' => 30, ])->isValid(), ); static::assertTrue( $schema->safeValidate([ '$type' => 'app.bsky.feed.post', 'text' => 'Hello world', 'createdAt' => '2023-01-01T00:00:00Z', ])->isValid(), ); }
public function testKnownVariantMustSatisfyItsReferencedSchema(): void { $schema = self::typedUnion(TRUE);
$wrongType = $schema->safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', 'age' => 'thirty', ]); static::assertFalse($wrongType->isValid()); static::assertSame(['$.age'], \array_column($wrongType->violations, 'path'));
$missing = $schema->safeValidate([ '$type' => 'app.bsky.actor.person', 'name' => 'Alice', ]); static::assertFalse($missing->isValid()); static::assertSame(['$.age'], \array_column($missing->violations, 'path')); }
public function testOpenAndClosedUnionsHandleUnknownVariantsDifferently(): void { $value = [ '$type' => 'app.bsky.feed.like', 'subject' => 'some-uri', 'createdAt' => '2023-01-01T00:00:00Z', ];
static::assertFalse(self::typedUnion(TRUE)->safeValidate($value)->isValid()); static::assertTrue(self::typedUnion(FALSE)->safeValidate($value)->isValid()); }
#[\PHPUnit\Framework\Attributes\DataProvider('missingTypeProvider')] public function testRequiresStringTypeDiscriminator(mixed $value): void { $result = self::typedUnion(FALSE)->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame(['missing_type'], \array_column($result->violations, 'code')); }
/** @return array<string, array{mixed}> */ public static function missingTypeProvider(): array { return [ 'missing type' => [['name' => 'Alice', 'age' => 30]], 'null type' => [['$type' => NULL]], 'numeric type' => [['$type' => 123]], 'non-object' => ['not an object'], 'null' => [NULL], 'list' => [[]], 'number' => [42], 'boolean' => [TRUE], ]; }
public function testValidatesClosedUnionRefs(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'embed' => ['$type' => 'com.example.unknown'], ], self::schemaWithUnion(), );
static::assertFalse($result->isValid()); static::assertSame(['$.embed'], \array_column($result->violations, 'path')); }
public function testOpenUnionUnknownVariantStillFollowsTheDataModel(): void { $result = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'embed' => [ '$type' => 'com.example.futureEmbed', 'score' => 1.5, ], ], self::schemaWithProperty('embed', [ 'type' => 'union', 'refs' => [], ]), );
static::assertFalse($result->isValid()); static::assertSame(['$.embed.score'], \array_column($result->violations, 'path')); static::assertSame(['invalid_lex_value'], \array_column($result->violations, 'code')); }
public function testMainDefinitionRequiresBareNsidDiscriminator(): void { $repository = new InMemoryLexiconRepository(); $repository->add(new LexiconDocument( 'com.example.variant', [ 'main' => [ 'type' => 'object', 'required' => ['value'], 'properties' => [ 'value' => ['type' => 'string'], ], ], ], [], )); $schema = new UnionSchema( new SchemaCompiler($repository), ['com.example.variant'], 'com.example.container', TRUE, );
$valid = $schema->safeValidate([ '$type' => 'com.example.variant', 'value' => 'valid', ]); $invalid = $schema->safeValidate([ '$type' => 'com.example.variant#main', 'value' => 'invalid', ]);
static::assertTrue($valid->isValid()); static::assertFalse($invalid->isValid()); static::assertSame(['invalid_union_type'], \array_column($invalid->violations, 'code')); }
/** * @param array<string, mixed> $property * @return array<string, mixed> */ private static function schemaWithProperty(string $name, array $property): array { return [ 'type' => 'object', 'required' => [$name], 'properties' => [$name => $property], ]; }
/** * @return array<string, mixed> */ private static function schemaWithUnion(): array { return [ 'lexicon' => 1, 'id' => 'com.example.post', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'tid', 'record' => [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], 'embed' => [ 'type' => 'union', 'closed' => TRUE, 'refs' => ['#knownEmbed'], ], ], ], ], 'knownEmbed' => [ 'type' => 'object', 'required' => ['name'], 'properties' => [ 'name' => ['type' => 'string'], ], ], ], ]; }
private static function typedUnion(bool $closed): UnionSchema { $repository = new InMemoryLexiconRepository(); $repository->add( new LexiconDocument( 'app.bsky.actor.person', [ 'main' => [ 'type' => 'object', 'required' => ['name', 'age'], 'properties' => [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'], ], ], ], [], ), ); $repository->add( new LexiconDocument( 'app.bsky.feed.post', [ 'main' => [ 'type' => 'object', 'required' => ['text', 'createdAt'], 'properties' => [ 'text' => ['type' => 'string'], 'createdAt' => ['type' => 'string'], ], ], ], [], ), );
return new UnionSchema( new SchemaCompiler($repository), ['app.bsky.actor.person', 'app.bsky.feed.post'], 'app.bsky.feed.container', $closed, ); }
}