Extend elicitation enum schema compliance (#261)

This commit is contained in:
Christopher Hertel 2026-03-22 18:46:40 +01:00 • committed by GitHub
commit e8f5a8ba22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1252 additions and 178 deletions

View file

@ -17,8 +17,11 @@ use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Elicitation\BooleanSchemaDefinition;
use Mcp\Schema\Elicitation\ElicitationSchema;
use Mcp\Schema\Elicitation\EnumSchemaDefinition;
use Mcp\Schema\Elicitation\MultiSelectEnumSchemaDefinition;
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
use Mcp\Schema\Elicitation\StringSchemaDefinition;
use Mcp\Schema\Elicitation\TitledEnumSchemaDefinition;
use Mcp\Schema\Elicitation\TitledMultiSelectEnumSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class ElicitationSchemaTest extends TestCase
@ -161,6 +164,101 @@ final class ElicitationSchemaTest extends TestCase
$this->assertInstanceOf(EnumSchemaDefinition::class, $schema->properties['rating']);
}
public function testFromArrayWithMissingPropertyType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "type"');
/* @phpstan-ignore argument.type */
ElicitationSchema::fromArray([
'properties' => [
'name' => ['title' => 'Name'],
],
]);
}
public function testFromArrayWithUnsupportedPropertyType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported type "object"');
ElicitationSchema::fromArray([
'properties' => [
'name' => ['type' => 'object', 'title' => 'Name'],
],
]);
}
public function testFromArrayWithArrayTypeMissingItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Array type must have "items" with either "enum" or "anyOf"');
ElicitationSchema::fromArray([
'properties' => [
'tags' => ['type' => 'array', 'title' => 'Tags'],
],
]);
}
public function testFromArrayWithNewEnumTypes(): void
{
$schema = ElicitationSchema::fromArray([
'properties' => [
'titledSingle' => [
'type' => 'string',
'title' => 'Titled Single',
'oneOf' => [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
],
],
'multiSelect' => [
'type' => 'array',
'title' => 'Multi Select',
'items' => ['type' => 'string', 'enum' => ['x', 'y']],
],
'titledMulti' => [
'type' => 'array',
'title' => 'Titled Multi',
'items' => [
'anyOf' => [
['const' => 'c', 'title' => 'Option C'],
['const' => 'd', 'title' => 'Option D'],
],
],
],
],
]);
$this->assertInstanceOf(TitledEnumSchemaDefinition::class, $schema->properties['titledSingle']);
$this->assertInstanceOf(MultiSelectEnumSchemaDefinition::class, $schema->properties['multiSelect']);
$this->assertInstanceOf(TitledMultiSelectEnumSchemaDefinition::class, $schema->properties['titledMulti']);
}
public function testFromArrayJsonSerializeRoundTripWithAllTypes(): void
{
$schema = new ElicitationSchema(
[
'name' => new StringSchemaDefinition('Name'),
'rating' => new EnumSchemaDefinition('Rating', ['1', '2', '3']),
'titledSingle' => new TitledEnumSchemaDefinition('Titled', [
['const' => 'a', 'title' => 'A'],
]),
'tags' => new MultiSelectEnumSchemaDefinition('Tags', ['x', 'y']),
'titledMulti' => new TitledMultiSelectEnumSchemaDefinition('Multi', [
['const' => 'c', 'title' => 'C'],
]),
],
['name'],
);
$serialized = $schema->jsonSerialize();
$restored = ElicitationSchema::fromArray($serialized);
$this->assertSame($serialized, $restored->jsonSerialize());
}
public function testJsonSerializeWithMinimalParams(): void
{
$schema = new ElicitationSchema([

View file

@ -0,0 +1,218 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Elicitation\MultiSelectEnumSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class MultiSelectEnumSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$schema = new MultiSelectEnumSchemaDefinition('Tags', ['php', 'js', 'go']);
$this->assertSame('Tags', $schema->title);
$this->assertSame(['php', 'js', 'go'], $schema->enum);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->minItems);
$this->assertNull($schema->maxItems);
}
public function testConstructorWithAllParams(): void
{
$schema = new MultiSelectEnumSchemaDefinition(
title: 'Tags',
enum: ['php', 'js', 'go'],
description: 'Select languages',
default: ['php'],
minItems: 1,
maxItems: 3,
);
$this->assertSame('Tags', $schema->title);
$this->assertSame(['php', 'js', 'go'], $schema->enum);
$this->assertSame('Select languages', $schema->description);
$this->assertSame(['php'], $schema->default);
$this->assertSame(1, $schema->minItems);
$this->assertSame(3, $schema->maxItems);
}
public function testConstructorWithEmptyEnum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('enum array must not be empty');
new MultiSelectEnumSchemaDefinition('Test', []);
}
public function testConstructorWithNonStringEnumValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('All enum values must be strings');
/* @phpstan-ignore argument.type */
new MultiSelectEnumSchemaDefinition('Test', ['a', 1, 'b']);
}
public function testConstructorWithNegativeMinItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minItems must be non-negative');
new MultiSelectEnumSchemaDefinition('Test', ['a'], minItems: -1);
}
public function testConstructorWithNegativeMaxItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('maxItems must be non-negative');
new MultiSelectEnumSchemaDefinition('Test', ['a'], maxItems: -1);
}
public function testConstructorWithMinItemsGreaterThanMaxItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minItems cannot be greater than maxItems');
new MultiSelectEnumSchemaDefinition('Test', ['a', 'b'], minItems: 3, maxItems: 1);
}
public function testConstructorWithInvalidDefault(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Default value "invalid" is not in the enum array');
new MultiSelectEnumSchemaDefinition('Test', ['a', 'b'], default: ['invalid']);
}
public function testFromArrayWithMinimalParams(): void
{
$schema = MultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Tags',
'items' => [
'type' => 'string',
'enum' => ['php', 'js', 'go'],
],
]);
$this->assertSame('Tags', $schema->title);
$this->assertSame(['php', 'js', 'go'], $schema->enum);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->minItems);
$this->assertNull($schema->maxItems);
}
public function testFromArrayWithAllParams(): void
{
$schema = MultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Tags',
'description' => 'Select languages',
'default' => ['php'],
'minItems' => 1,
'maxItems' => 3,
'items' => [
'type' => 'string',
'enum' => ['php', 'js', 'go'],
],
]);
$this->assertSame('Tags', $schema->title);
$this->assertSame(['php', 'js', 'go'], $schema->enum);
$this->assertSame('Select languages', $schema->description);
$this->assertSame(['php'], $schema->default);
$this->assertSame(1, $schema->minItems);
$this->assertSame(3, $schema->maxItems);
}
public function testFromArrayWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
/* @phpstan-ignore argument.type */
MultiSelectEnumSchemaDefinition::fromArray([
'items' => ['type' => 'string', 'enum' => ['a']],
]);
}
public function testFromArrayWithMissingItemsEnum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "items.enum"');
/* @phpstan-ignore argument.type */
MultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Test',
'items' => ['type' => 'string'],
]);
}
public function testJsonSerializeWithMinimalParams(): void
{
$schema = new MultiSelectEnumSchemaDefinition('Tags', ['php', 'js', 'go']);
$this->assertSame([
'type' => 'array',
'title' => 'Tags',
'items' => [
'type' => 'string',
'enum' => ['php', 'js', 'go'],
],
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$schema = new MultiSelectEnumSchemaDefinition(
title: 'Tags',
enum: ['php', 'js', 'go'],
description: 'Select languages',
default: ['php'],
minItems: 1,
maxItems: 3,
);
$this->assertSame([
'type' => 'array',
'title' => 'Tags',
'description' => 'Select languages',
'items' => [
'type' => 'string',
'enum' => ['php', 'js', 'go'],
],
'default' => ['php'],
'minItems' => 1,
'maxItems' => 3,
], $schema->jsonSerialize());
}
public function testFromArrayJsonSerializeRoundTrip(): void
{
$original = new MultiSelectEnumSchemaDefinition(
title: 'Tags',
enum: ['php', 'js', 'go'],
description: 'Select languages',
default: ['php', 'go'],
minItems: 1,
maxItems: 3,
);
$serialized = $original->jsonSerialize();
$restored = MultiSelectEnumSchemaDefinition::fromArray($serialized);
$this->assertSame($serialized, $restored->jsonSerialize());
}
}

View file

@ -1,115 +0,0 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Elicitation\BooleanSchemaDefinition;
use Mcp\Schema\Elicitation\EnumSchemaDefinition;
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
use Mcp\Schema\Elicitation\PrimitiveSchemaDefinition;
use Mcp\Schema\Elicitation\StringSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class PrimitiveSchemaDefinitionTest extends TestCase
{
public function testFromArrayCreatesStringSchema(): void
{
$schema = PrimitiveSchemaDefinition::fromArray([
'type' => 'string',
'title' => 'Name',
]);
$this->assertInstanceOf(StringSchemaDefinition::class, $schema);
$this->assertSame('Name', $schema->title);
}
public function testFromArrayCreatesEnumSchemaForStringWithEnum(): void
{
$schema = PrimitiveSchemaDefinition::fromArray([
'type' => 'string',
'title' => 'Rating',
'enum' => ['1', '2', '3'],
]);
$this->assertInstanceOf(EnumSchemaDefinition::class, $schema);
$this->assertSame('Rating', $schema->title);
$this->assertSame(['1', '2', '3'], $schema->enum);
}
public function testFromArrayCreatesIntegerSchema(): void
{
$schema = PrimitiveSchemaDefinition::fromArray([
'type' => 'integer',
'title' => 'Age',
]);
$this->assertInstanceOf(NumberSchemaDefinition::class, $schema);
$this->assertSame('Age', $schema->title);
$this->assertTrue($schema->integerOnly);
}
public function testFromArrayCreatesNumberSchema(): void
{
$schema = PrimitiveSchemaDefinition::fromArray([
'type' => 'number',
'title' => 'Price',
]);
$this->assertInstanceOf(NumberSchemaDefinition::class, $schema);
$this->assertSame('Price', $schema->title);
$this->assertFalse($schema->integerOnly);
}
public function testFromArrayCreatesBooleanSchema(): void
{
$schema = PrimitiveSchemaDefinition::fromArray([
'type' => 'boolean',
'title' => 'Confirm',
]);
$this->assertInstanceOf(BooleanSchemaDefinition::class, $schema);
$this->assertSame('Confirm', $schema->title);
}
public function testFromArrayWithMissingType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "type"');
/* @phpstan-ignore argument.type */
PrimitiveSchemaDefinition::fromArray(['title' => 'Test']);
}
public function testFromArrayWithUnsupportedType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported primitive type "object"');
PrimitiveSchemaDefinition::fromArray([
'type' => 'object',
'title' => 'Test',
]);
}
public function testFromArrayWithArrayType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported primitive type "array"');
PrimitiveSchemaDefinition::fromArray([
'type' => 'array',
'title' => 'Test',
]);
}
}

View file

@ -0,0 +1,200 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Elicitation\TitledEnumSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class TitledEnumSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledEnumSchemaDefinition('Pick one', $oneOf);
$this->assertSame('Pick one', $schema->title);
$this->assertSame($oneOf, $schema->oneOf);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
}
public function testConstructorWithAllParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledEnumSchemaDefinition(
title: 'Pick one',
oneOf: $oneOf,
description: 'Choose wisely',
default: 'b',
);
$this->assertSame('Pick one', $schema->title);
$this->assertSame($oneOf, $schema->oneOf);
$this->assertSame('Choose wisely', $schema->description);
$this->assertSame('b', $schema->default);
}
public function testConstructorWithEmptyOneOf(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('oneOf array must not be empty');
new TitledEnumSchemaDefinition('Test', []);
}
public function testConstructorWithMissingConst(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Each oneOf item must have a string "const" property');
/* @phpstan-ignore argument.type */
new TitledEnumSchemaDefinition('Test', [['title' => 'A']]);
}
public function testConstructorWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Each oneOf item must have a string "title" property');
/* @phpstan-ignore argument.type */
new TitledEnumSchemaDefinition('Test', [['const' => 'a']]);
}
public function testConstructorWithInvalidDefault(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Default value "invalid" is not in the oneOf const values');
new TitledEnumSchemaDefinition(
title: 'Test',
oneOf: [['const' => 'a', 'title' => 'A']],
default: 'invalid',
);
}
public function testFromArrayWithMinimalParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = TitledEnumSchemaDefinition::fromArray([
'title' => 'Pick one',
'oneOf' => $oneOf,
]);
$this->assertSame('Pick one', $schema->title);
$this->assertSame($oneOf, $schema->oneOf);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
}
public function testFromArrayWithAllParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = TitledEnumSchemaDefinition::fromArray([
'title' => 'Pick one',
'oneOf' => $oneOf,
'description' => 'Choose wisely',
'default' => 'b',
]);
$this->assertSame('Pick one', $schema->title);
$this->assertSame($oneOf, $schema->oneOf);
$this->assertSame('Choose wisely', $schema->description);
$this->assertSame('b', $schema->default);
}
public function testFromArrayWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
/* @phpstan-ignore argument.type */
TitledEnumSchemaDefinition::fromArray(['oneOf' => [['const' => 'a', 'title' => 'A']]]);
}
public function testFromArrayWithMissingOneOf(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "oneOf"');
/* @phpstan-ignore argument.type */
TitledEnumSchemaDefinition::fromArray(['title' => 'Test']);
}
public function testJsonSerializeWithMinimalParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledEnumSchemaDefinition('Pick one', $oneOf);
$this->assertSame([
'type' => 'string',
'title' => 'Pick one',
'oneOf' => $oneOf,
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledEnumSchemaDefinition(
title: 'Pick one',
oneOf: $oneOf,
description: 'Choose wisely',
default: 'b',
);
$this->assertSame([
'type' => 'string',
'title' => 'Pick one',
'description' => 'Choose wisely',
'oneOf' => $oneOf,
'default' => 'b',
], $schema->jsonSerialize());
}
public function testFromArrayJsonSerializeRoundTrip(): void
{
$oneOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$original = new TitledEnumSchemaDefinition(
title: 'Pick one',
oneOf: $oneOf,
description: 'Choose wisely',
default: 'b',
);
$serialized = $original->jsonSerialize();
$restored = TitledEnumSchemaDefinition::fromArray($serialized);
$this->assertSame($serialized, $restored->jsonSerialize());
}
}

View file

@ -0,0 +1,253 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Schema\Elicitation;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Elicitation\TitledMultiSelectEnumSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class TitledMultiSelectEnumSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledMultiSelectEnumSchemaDefinition('Pick many', $anyOf);
$this->assertSame('Pick many', $schema->title);
$this->assertSame($anyOf, $schema->anyOf);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->minItems);
$this->assertNull($schema->maxItems);
}
public function testConstructorWithAllParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
['const' => 'c', 'title' => 'Option C'],
];
$schema = new TitledMultiSelectEnumSchemaDefinition(
title: 'Pick many',
anyOf: $anyOf,
description: 'Select all that apply',
default: ['a', 'c'],
minItems: 1,
maxItems: 3,
);
$this->assertSame('Pick many', $schema->title);
$this->assertSame($anyOf, $schema->anyOf);
$this->assertSame('Select all that apply', $schema->description);
$this->assertSame(['a', 'c'], $schema->default);
$this->assertSame(1, $schema->minItems);
$this->assertSame(3, $schema->maxItems);
}
public function testConstructorWithEmptyAnyOf(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('anyOf array must not be empty');
new TitledMultiSelectEnumSchemaDefinition('Test', []);
}
public function testConstructorWithMissingConst(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Each anyOf item must have a string "const" property');
/* @phpstan-ignore argument.type */
new TitledMultiSelectEnumSchemaDefinition('Test', [['title' => 'A']]);
}
public function testConstructorWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Each anyOf item must have a string "title" property');
/* @phpstan-ignore argument.type */
new TitledMultiSelectEnumSchemaDefinition('Test', [['const' => 'a']]);
}
public function testConstructorWithNegativeMinItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minItems must be non-negative');
new TitledMultiSelectEnumSchemaDefinition('Test', [['const' => 'a', 'title' => 'A']], minItems: -1);
}
public function testConstructorWithNegativeMaxItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('maxItems must be non-negative');
new TitledMultiSelectEnumSchemaDefinition('Test', [['const' => 'a', 'title' => 'A']], maxItems: -1);
}
public function testConstructorWithMinItemsGreaterThanMaxItems(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minItems cannot be greater than maxItems');
new TitledMultiSelectEnumSchemaDefinition(
'Test',
[['const' => 'a', 'title' => 'A'], ['const' => 'b', 'title' => 'B']],
minItems: 3,
maxItems: 1,
);
}
public function testConstructorWithInvalidDefault(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Default value "invalid" is not in the anyOf const values');
new TitledMultiSelectEnumSchemaDefinition(
'Test',
[['const' => 'a', 'title' => 'A']],
default: ['invalid'],
);
}
public function testFromArrayWithMinimalParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = TitledMultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Pick many',
'items' => ['anyOf' => $anyOf],
]);
$this->assertSame('Pick many', $schema->title);
$this->assertSame($anyOf, $schema->anyOf);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->minItems);
$this->assertNull($schema->maxItems);
}
public function testFromArrayWithAllParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = TitledMultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Pick many',
'description' => 'Select all that apply',
'default' => ['a'],
'minItems' => 1,
'maxItems' => 2,
'items' => ['anyOf' => $anyOf],
]);
$this->assertSame('Pick many', $schema->title);
$this->assertSame($anyOf, $schema->anyOf);
$this->assertSame('Select all that apply', $schema->description);
$this->assertSame(['a'], $schema->default);
$this->assertSame(1, $schema->minItems);
$this->assertSame(2, $schema->maxItems);
}
public function testFromArrayWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
/* @phpstan-ignore argument.type */
TitledMultiSelectEnumSchemaDefinition::fromArray([
'items' => ['anyOf' => [['const' => 'a', 'title' => 'A']]],
]);
}
public function testFromArrayWithMissingItemsAnyOf(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "items.anyOf"');
/* @phpstan-ignore argument.type */
TitledMultiSelectEnumSchemaDefinition::fromArray([
'title' => 'Test',
'items' => [],
]);
}
public function testJsonSerializeWithMinimalParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledMultiSelectEnumSchemaDefinition('Pick many', $anyOf);
$this->assertSame([
'type' => 'array',
'title' => 'Pick many',
'items' => ['anyOf' => $anyOf],
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$schema = new TitledMultiSelectEnumSchemaDefinition(
title: 'Pick many',
anyOf: $anyOf,
description: 'Select all that apply',
default: ['a'],
minItems: 1,
maxItems: 2,
);
$this->assertSame([
'type' => 'array',
'title' => 'Pick many',
'description' => 'Select all that apply',
'items' => ['anyOf' => $anyOf],
'default' => ['a'],
'minItems' => 1,
'maxItems' => 2,
], $schema->jsonSerialize());
}
public function testFromArrayJsonSerializeRoundTrip(): void
{
$anyOf = [
['const' => 'a', 'title' => 'Option A'],
['const' => 'b', 'title' => 'Option B'],
];
$original = new TitledMultiSelectEnumSchemaDefinition(
title: 'Pick many',
anyOf: $anyOf,
description: 'Select all that apply',
default: ['a', 'b'],
minItems: 1,
maxItems: 2,
);
$serialized = $original->jsonSerialize();
$restored = TitledMultiSelectEnumSchemaDefinition::fromArray($serialized);
$this->assertSame($serialized, $restored->jsonSerialize());
}
}