php-sdk/tests/Unit/Schema/Elicitation/NumberSchemaDefinitionTest.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

201 lines
6.1 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\NumberSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class NumberSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$schema = new NumberSchemaDefinition('Age');
$this->assertSame('Age', $schema->title);
$this->assertFalse($schema->integerOnly);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->minimum);
$this->assertNull($schema->maximum);
}
public function testConstructorWithAllParams(): void
{
$schema = new NumberSchemaDefinition(
title: 'Party Size',
integerOnly: true,
description: 'Number of guests',
default: 2,
minimum: 1,
maximum: 10,
);
$this->assertSame('Party Size', $schema->title);
$this->assertTrue($schema->integerOnly);
$this->assertSame('Number of guests', $schema->description);
$this->assertSame(2, $schema->default);
$this->assertSame(1, $schema->minimum);
$this->assertSame(10, $schema->maximum);
}
public function testConstructorWithFloatValues(): void
{
$schema = new NumberSchemaDefinition(
title: 'Temperature',
default: 36.5,
minimum: 35.0,
maximum: 42.0,
);
$this->assertSame(36.5, $schema->default);
$this->assertSame(35.0, $schema->minimum);
$this->assertSame(42.0, $schema->maximum);
}
public function testConstructorWithMinimumGreaterThanMaximum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('minimum cannot be greater than maximum');
new NumberSchemaDefinition('Test', minimum: 10, maximum: 5);
}
public function testConstructorWithDefaultBelowMinimum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('default value cannot be less than minimum');
new NumberSchemaDefinition('Test', default: 5, minimum: 10);
}
public function testConstructorWithDefaultAboveMaximum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('default value cannot be greater than maximum');
new NumberSchemaDefinition('Test', default: 15, maximum: 10);
}
public function testConstructorWithNonIntegerDefaultWhenIntegerOnly(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('default value must be an integer when integerOnly is true');
new NumberSchemaDefinition('Test', integerOnly: true, default: 5.5);
}
public function testFromArrayWithIntegerType(): void
{
$schema = NumberSchemaDefinition::fromArray([
'type' => 'integer',
'title' => 'Count',
]);
$this->assertTrue($schema->integerOnly);
}
public function testFromArrayWithNumberType(): void
{
$schema = NumberSchemaDefinition::fromArray([
'type' => 'number',
'title' => 'Price',
]);
$this->assertFalse($schema->integerOnly);
}
public function testFromArrayWithAllParams(): void
{
$schema = NumberSchemaDefinition::fromArray([
'type' => 'integer',
'title' => 'Party Size',
'description' => 'Number of guests',
'default' => 2,
'minimum' => 1,
'maximum' => 10,
]);
$this->assertSame('Party Size', $schema->title);
$this->assertTrue($schema->integerOnly);
$this->assertSame('Number of guests', $schema->description);
$this->assertSame(2, $schema->default);
$this->assertSame(1, $schema->minimum);
$this->assertSame(10, $schema->maximum);
}
public function testFromArrayWithoutTitleIsAccepted(): void
{
// `title` is optional in the specification, so a server that omits it
// must still be readable.
$schema = NumberSchemaDefinition::fromArray(['type' => 'integer']);
$this->assertNull($schema->title);
$this->assertArrayNotHasKey('title', $schema->jsonSerialize());
}
public function testFromArrayRejectsNonStringTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid "title" for number schema definition.');
/* @phpstan-ignore argument.type */
NumberSchemaDefinition::fromArray(['title' => 42]);
}
public function testJsonSerializeAsInteger(): void
{
$schema = new NumberSchemaDefinition(
title: 'Count',
integerOnly: true,
);
$this->assertSame([
'type' => 'integer',
'title' => 'Count',
], $schema->jsonSerialize());
}
public function testJsonSerializeAsNumber(): void
{
$schema = new NumberSchemaDefinition(
title: 'Price',
integerOnly: false,
);
$this->assertSame([
'type' => 'number',
'title' => 'Price',
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$schema = new NumberSchemaDefinition(
title: 'Party Size',
integerOnly: true,
description: 'Number of guests',
default: 2,
minimum: 1,
maximum: 10,
);
$this->assertSame([
'type' => 'integer',
'title' => 'Party Size',
'description' => 'Number of guests',
'default' => 2,
'minimum' => 1,
'maximum' => 10,
], $schema->jsonSerialize());
}
}