Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\CompiledXrpcMethod;use Wotnak\Atproto\Lexicon\InMemoryLexiconRepository;use Wotnak\Atproto\Lexicon\LexiconDocument;use Wotnak\Atproto\Lexicon\LexiconParser;use Wotnak\Atproto\Lexicon\SchemaCompiler;use Wotnak\Atproto\Lexicon\ValidationResult;
final class SchemaCompilerXrpcIntegrationTest extends TestCase {
public function testProcedurePassesValidParameters(): void { $method = self::procedure(); static::assertNotNull($method->parameters); $value = [ 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'array' => ['x', 'y'], 'def' => 1, ];
$result = $method->parameters->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value, $result->value); }
public function testProcedurePassesValidInput(): void { $method = self::procedure(); static::assertNotNull($method->input);
$result = $method->input->safeParse(self::validBody());
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
public function testProcedureValidatesInputPropertyType(): void { $method = self::procedure(); static::assertNotNull($method->input); $value = self::validBody(); $value['object'] = ['boolean' => 'string'];
self::assertViolation($method->input->safeParse($value), '$.object.boolean', 'invalid_type'); }
public function testProcedureRejectsMissingInputProperties(): void { $method = self::procedure(); static::assertNotNull($method->input);
self::assertViolation($method->input->safeParse([]), '$.object', 'missing_required'); }
public function testProcedurePassesValidOutput(): void { $method = self::procedure(); static::assertNotNull($method->output);
$result = $method->output->safeParse(self::validBody());
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
public function testProcedureRejectsInvalidOutput(): void { $method = self::procedure(); static::assertNotNull($method->output);
self::assertViolation($method->output->safeParse([]), '$.object', 'missing_required'); }
public function testQueryPassesValidParametersAndAppliesDefault(): void { $method = self::query(); static::assertNotNull($method->parameters); $value = [ 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'array' => ['x', 'y'], ];
$result = $method->parameters->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value + ['def' => 0], $result->value); }
public function testQueryPreservesUnknownParameters(): void { $method = self::query(); static::assertNotNull($method->parameters); $value = [ 'boolean' => TRUE, 'integer' => 123, 'unknown' => 'property', ];
$result = $method->parameters->safeParse($value);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertSame($value + ['def' => 0], $result->value); }
public function testQueryPassesRequiredParameters(): void { $method = self::query(); static::assertNotNull($method->parameters);
$result = $method->parameters->safeParse(['boolean' => TRUE, 'integer' => 123]);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
public function testQueryRejectsMissingParameters(): void { $method = self::query(); static::assertNotNull($method->parameters);
self::assertViolation($method->parameters->safeParse(['boolean' => TRUE]), '$.integer', 'missing_required'); }
public function testQueryRejectsUndefinedEquivalentParameter(): void { $method = self::query(); static::assertNotNull($method->parameters);
self::assertViolation( $method->parameters->safeParse(['boolean' => TRUE, 'integer' => NULL]), '$.integer', 'invalid_type', ); }
public function testQueryRejectsInvalidParameterValue(): void { $method = self::query(); static::assertNotNull($method->parameters);
self::assertViolation( $method->parameters->safeParse([ 'boolean' => 'string', 'integer' => 123, 'string' => 'string', ]), '$.boolean', 'invalid_type', ); }
public function testQueryRejectsInvalidParameterTypes(): void { $method = self::query(); static::assertNotNull($method->parameters); $cases = [ 'unknown float' => [ ['boolean' => TRUE, 'integer' => 123, 'float' => 123.45], '$.float', 'invalid_parameter', ], 'string instead of array' => [ ['boolean' => TRUE, 'integer' => 123, 'array' => 'x'], '$.array', 'invalid_type', ], 'integer instead of array' => [ ['boolean' => TRUE, 'integer' => 123, 'array' => 3], '$.array', 'invalid_type', ], 'NaN instead of array' => [ ['boolean' => TRUE, 'integer' => 123, 'array' => NAN], '$.array', 'invalid_type', ], ];
foreach ($cases as $name => [$value, $path, $code]) { $result = $method->parameters->safeParse($value); static::assertFalse($result->isValid(), $name); static::assertContains($path, \array_column($result->violations, 'path'), $name); static::assertContains($code, \array_column($result->violations, 'code'), $name); } }
public function testQueryDefaultIsPresentInParsedParameters(): void { $method = self::query(); static::assertNotNull($method->parameters);
$result = $method->parameters->safeParse([ 'boolean' => TRUE, 'integer' => 123, 'string' => 'string', 'array' => ['x', 'y'], ]);
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertIsArray($result->value); static::assertSame(0, $result->value['def'] ?? NULL); }
public function testQueryPassesValidOutput(): void { $method = self::query(); static::assertNotNull($method->output);
$result = $method->output->safeParse(self::validBody());
static::assertTrue($result->isValid(), \implode('; ', $result->messages())); }
public function testQueryRejectsInvalidOutput(): void { $method = self::query(); static::assertNotNull($method->output); $value = self::validBody(); $value['object'] = ['boolean' => 'string'];
self::assertViolation($method->output->safeParse($value), '$.object.boolean', 'invalid_type'); }
private static function procedure(): CompiledXrpcMethod { return self::method([ 'lexicon' => 1, 'id' => 'com.example.procedure', 'defs' => [ 'main' => [ 'type' => 'procedure', 'parameters' => self::parameters(), 'input' => [ 'encoding' => 'application/json', 'schema' => ['type' => 'ref', 'ref' => 'com.example.kitchenSink#object'], ], 'output' => [ 'encoding' => 'application/json', 'schema' => ['type' => 'ref', 'ref' => 'com.example.kitchenSink#object'], ], ], ], ]); }
private static function query(): CompiledXrpcMethod { $parameters = self::parameters(); $properties = $parameters['properties'] ?? NULL; static::assertIsArray($properties); $properties['def'] = ['type' => 'integer', 'default' => 0]; $parameters['properties'] = $properties; return self::method([ 'lexicon' => 1, 'id' => 'com.example.query', 'defs' => [ 'main' => [ 'type' => 'query', 'parameters' => $parameters, 'output' => [ 'encoding' => 'application/json', 'schema' => ['type' => 'ref', 'ref' => 'com.example.kitchenSink#object'], ], ], ], ]); }
/** @return array<string, mixed> */ private static function parameters(): array { return [ 'type' => 'params', 'required' => ['boolean', 'integer'], 'properties' => [ 'boolean' => ['type' => 'boolean'], 'integer' => ['type' => 'integer'], 'string' => ['type' => 'string'], 'array' => ['type' => 'array', 'items' => ['type' => 'string']], ], ]; }
/** @return array<string, mixed> */ private static function validBody(): array { return [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'float' => 123.45, 'integer' => 123, 'string' => 'string', ]; }
/** @param array<string, mixed> $document */ private static function method(array $document): CompiledXrpcMethod { $repository = new InMemoryLexiconRepository(); foreach ([self::kitchenSinkDocument(), $document] as $raw) { $result = new LexiconParser()->parse($raw); static::assertTrue($result->isValid(), \implode('; ', $result->messages())); static::assertInstanceOf(LexiconDocument::class, $result->value); $repository->add($result->value); } $id = $document['id'] ?? NULL; static::assertIsString($id); $method = new SchemaCompiler($repository)->compileXrpc($id); static::assertInstanceOf(CompiledXrpcMethod::class, $method); return $method; }
/** @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'], ], ], ], ]; }
private static function assertViolation(ValidationResult $result, string $path, string $code): void { static::assertFalse($result->isValid()); static::assertContains($path, \array_column($result->violations, 'path')); static::assertContains($code, \array_column($result->violations, 'code')); }
}