Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\InMemoryLexiconRepository;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\Schema\IntegerSchema;use Wotnak\Atproto\Lexicon\Schema\ObjectSchema;use Wotnak\Atproto\Lexicon\Schema\RecordSchema;use Wotnak\Atproto\Lexicon\SchemaCompiler;
final class SchemaCompilerPrimitiveIntegrationTest extends TestCase {
public function testAppliesArrayLengthConstraints(): void { $schema = self::record('com.example.arrayLength', [ 'array' => [ 'type' => 'array', 'minLength' => 2, 'maxLength' => 4, 'items' => ['type' => 'integer'], ], ]);
static::assertTrue($schema->safeParse(self::value('com.example.arrayLength', 'array', [1, 2, 3]))->isValid()); self::assertViolation($schema, self::value('com.example.arrayLength', 'array', [1]), '$.array', 'min_length'); self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, 2, 3, 4, 5]), '$.array', 'max_length', ); }
public function testAppliesArrayItemConstraints(): void { $schema = self::record('com.example.arrayLength', [ 'array' => [ 'type' => 'array', 'minLength' => 2, 'maxLength' => 4, 'items' => ['type' => 'integer'], ], ]);
self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, '2', 3]), '$.array[1]', 'invalid_type', ); self::assertViolation( $schema, self::value('com.example.arrayLength', 'array', [1, NULL, 3]), '$.array[1]', 'invalid_type', ); }
public function testAppliesBooleanConstConstraint(): void { $schema = self::record('com.example.boolConst', [ 'boolean' => ['type' => 'boolean', 'const' => FALSE], ]);
static::assertTrue($schema->safeParse(self::value('com.example.boolConst', 'boolean', FALSE))->isValid()); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', TRUE), '$.boolean', 'invalid_const', ); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', 'true'), '$.boolean', 'invalid_type', ); self::assertViolation( $schema, self::value('com.example.boolConst', 'boolean', 1), '$.boolean', 'invalid_type', ); }
public function testAppliesBytesLengthConstraints(): void { $schema = self::record('com.example.byteLength', [ 'bytes' => ['type' => 'bytes', 'minLength' => 2, 'maxLength' => 4], ]);
static::assertTrue( $schema->safeParse(self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQID']))->isValid(), ); self::assertViolation( $schema, self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQ==']), '$.bytes', 'min_length', ); self::assertViolation( $schema, self::value('com.example.byteLength', 'bytes', ['$bytes' => 'AQIDBAU=']), '$.bytes', 'max_length', ); }
public function testGeneratesDefaultProperties(): void { $document = [ 'lexicon' => 1, 'id' => 'com.example.default', 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'required' => ['boolean'], 'properties' => [ 'boolean' => ['type' => 'boolean', 'default' => FALSE], 'integer' => ['type' => 'integer', 'default' => 0], 'string' => ['type' => 'string', 'default' => ''], 'object' => ['type' => 'ref', 'ref' => '#object'], ], ], ], 'object' => [ 'type' => 'object', 'properties' => [ 'boolean' => ['type' => 'boolean', 'default' => TRUE], 'integer' => ['type' => 'integer', 'default' => 1], 'string' => ['type' => 'string', 'default' => 'x'], ], ], ], ]; $schema = self::compiledRecord($document);
$result = $schema->safeParse([ '$type' => 'com.example.default', 'object' => [], ]);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertIsArray($result->value); static::assertEquals( [ '$type' => 'com.example.default', 'boolean' => FALSE, 'integer' => 0, 'string' => '', 'object' => [ 'boolean' => TRUE, 'integer' => 1, 'string' => 'x', ], ], $result->value, ); static::assertArrayNotHasKey('datetime', $result->value); }
public function testPrimitiveAndOptionalDefaultsRespectParseMode(): void { $integer = new IntegerSchema(); static::assertFalse($integer->safeParse(NULL)->isValid()); static::assertFalse($integer->safeValidate(NULL)->isValid());
$optionalInteger = new ObjectSchema(['value' => $integer]); static::assertSame([], $optionalInteger->safeParse([])->value); static::assertSame([], $optionalInteger->safeValidate([])->value);
$defaultInteger = new IntegerSchema(default: 42); $parsedDefault = $defaultInteger->safeParse(NULL); static::assertTrue($parsedDefault->isValid()); static::assertSame(42, $parsedDefault->value); static::assertFalse($defaultInteger->safeValidate(NULL)->isValid());
$optionalDefault = new ObjectSchema(['value' => $defaultInteger]); static::assertSame(['value' => 42], $optionalDefault->safeParse([])->value); static::assertSame([], $optionalDefault->safeValidate([])->value);
$object = new ObjectSchema([ 'foo' => new \Wotnak\Atproto\Lexicon\Schema\StringSchema(default: 'aze'), ]); static::assertSame(['foo' => 'aze'], $object->safeParse([])->value); static::assertSame([], $object->safeValidate([])->value); }
public function testAppliesIntegerRangeConstraint(): void { $schema = self::record('com.example.integerRange', [ 'integer' => ['type' => 'integer', 'minimum' => 2, 'maximum' => 4], ]);
static::assertTrue($schema->safeParse(self::value('com.example.integerRange', 'integer', 2))->isValid()); self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 1), '$.integer', 'minimum', ); self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 5), '$.integer', 'maximum', ); }
public function testAppliesIntegerEnumConstraint(): void { $schema = self::record('com.example.integerEnum', [ 'integer' => ['type' => 'integer', 'enum' => [1, 2]], ]);
static::assertTrue($schema->safeParse(self::value('com.example.integerEnum', 'integer', 2))->isValid()); self::assertViolation( $schema, self::value('com.example.integerEnum', 'integer', 0), '$.integer', 'invalid_enum', ); }
public function testAppliesIntegerConstConstraint(): void { $schema = self::record('com.example.integerConst', [ 'integer' => ['type' => 'integer', 'const' => 0], ]);
static::assertTrue($schema->safeParse(self::value('com.example.integerConst', 'integer', 0))->isValid()); self::assertViolation( $schema, self::value('com.example.integerConst', 'integer', 1), '$.integer', 'invalid_const', ); }
public function testRejectsNonWholeNumbersAsIntegers(): void { $schema = self::record('com.example.integerRange', [ 'integer' => ['type' => 'integer', 'minimum' => 2, 'maximum' => 4], ]);
self::assertViolation( $schema, self::value('com.example.integerRange', 'integer', 2.5), '$.integer', 'invalid_type', ); }
/** * @param array<string, mixed> $properties * @param list<string> $required */ private static function record(string $id, array $properties, array $required = []): RecordSchema { return self::compiledRecord([ 'lexicon' => 1, 'id' => $id, 'defs' => [ 'main' => [ 'type' => 'record', 'key' => 'any', 'record' => [ 'type' => 'object', 'required' => $required, 'properties' => $properties, ], ], ], ]); }
/** @param array<string, mixed> $document */ private static function compiledRecord(array $document): RecordSchema { $result = new LexiconParser()->parse($document); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository = new InMemoryLexiconRepository(); $repository->add($result->value); $schema = new SchemaCompiler($repository)->compileRecord($result->value->id); static::assertInstanceOf(RecordSchema::class, $schema); return $schema; }
/** @return array<string, mixed> */ private static function value(string $type, string $field, mixed $value): array { return ['$type' => $type, $field => $value]; }
/** @param array<string, mixed> $value */ private static function assertViolation( RecordSchema $schema, array $value, string $path, string $code, ): void { $result = $schema->safeParse($value); static::assertFalse($result->isValid()); static::assertContains($path, \array_column($result->violations, 'path')); static::assertContains($code, \array_column($result->violations, 'code')); }
}