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 */ 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 */ private static function validBody(): array { return [ 'object' => ['boolean' => TRUE], 'array' => ['one', 'two'], 'boolean' => TRUE, 'float' => 123.45, 'integer' => 123, 'string' => 'string', ]; } /** @param array $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 */ 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')); } }