php-sdk/tests/Unit/Schema/Elicitation/StringSchemaDefinitionTest.php
Christopher Hertel cf2c4d4ead
[Schema] Stop rejecting elicitation schemas the specification allows (#437)
* [Schema] Stop rejecting elicitation schemas the specification allows

The specification makes `title` optional on every elicitation schema
definition. AbstractSchemaDefinition required it, so a client reading a
conformant server's elicitation/create refused the request - and a server
building one could not omit it either.

`title` is now nullable on all seven definitions, omitted from the
serialized form when absent, and rejected only when present and not a
string.

* Fix stale title docblocks; allow omitting title where safe

* Give Number's title param a default too

* Apply suggestions from code review

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
2026-08-18 19:49:15 +02:00

167 lines
5.4 KiB
PHP

<?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\StringSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class StringSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$schema = new StringSchemaDefinition('Name');
$this->assertSame('Name', $schema->title);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->format);
$this->assertNull($schema->minLength);
$this->assertNull($schema->maxLength);
}
public function testConstructorWithAllParams(): void
{
$schema = new StringSchemaDefinition(
title: 'Email Address',
description: 'Your primary email',
default: 'user@example.com',
format: 'email',
minLength: 5,
maxLength: 100,
);
$this->assertSame('Email Address', $schema->title);
$this->assertSame('Your primary email', $schema->description);
$this->assertSame('user@example.com', $schema->default);
$this->assertSame('email', $schema->format);
$this->assertSame(5, $schema->minLength);
$this->assertSame(100, $schema->maxLength);
}
public function testConstructorWithValidFormats(): void
{
foreach (['date', 'date-time', 'email', 'uri'] as $format) {
$schema = new StringSchemaDefinition('Test', format: $format);
$this->assertSame($format, $schema->format);
}
}
public function testConstructorWithInvalidFormat(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid format "invalid"');
new StringSchemaDefinition('Test', format: 'invalid');
}
public function testConstructorWithNegativeMinLength(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minLength must be non-negative');
new StringSchemaDefinition('Test', minLength: -1);
}
public function testConstructorWithNegativeMaxLength(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('maxLength must be non-negative');
new StringSchemaDefinition('Test', maxLength: -1);
}
public function testConstructorWithMinLengthGreaterThanMaxLength(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minLength cannot be greater than maxLength');
new StringSchemaDefinition('Test', minLength: 10, maxLength: 5);
}
public function testFromArrayWithMinimalParams(): void
{
$schema = StringSchemaDefinition::fromArray(['title' => 'Name']);
$this->assertSame('Name', $schema->title);
}
public function testFromArrayWithAllParams(): void
{
$schema = StringSchemaDefinition::fromArray([
'title' => 'Email Address',
'description' => 'Your primary email',
'default' => 'user@example.com',
'format' => 'email',
'minLength' => 5,
'maxLength' => 100,
]);
$this->assertSame('Email Address', $schema->title);
$this->assertSame('Your primary email', $schema->description);
$this->assertSame('user@example.com', $schema->default);
$this->assertSame('email', $schema->format);
$this->assertSame(5, $schema->minLength);
$this->assertSame(100, $schema->maxLength);
}
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = StringSchemaDefinition::fromArray([]);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid "title" for string schema definition.');
/* @phpstan-ignore argument.type */
StringSchemaDefinition::fromArray(['title' => 42]);
}
public function testJsonSerializeWithMinimalParams(): void
{
$schema = new StringSchemaDefinition('Name');
$this->assertSame([
'type' => 'string',
'title' => 'Name',
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$schema = new StringSchemaDefinition(
title: 'Email Address',
description: 'Your primary email',
default: 'user@example.com',
format: 'email',
minLength: 5,
maxLength: 100,
);
$this->assertSame([
'type' => 'string',
'title' => 'Email Address',
'description' => 'Your primary email',
'default' => 'user@example.com',
'format' => 'email',
'minLength' => 5,
'maxLength' => 100,
], $schema->jsonSerialize());
}
}