-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathEnumSchemaDefinitionTest.php
More file actions
165 lines (138 loc) · 5.42 KB
/
Copy pathEnumSchemaDefinitionTest.php
File metadata and controls
165 lines (138 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?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\EnumSchemaDefinition;
use PHPUnit\Framework\TestCase;
final class EnumSchemaDefinitionTest extends TestCase
{
public function testConstructorWithMinimalParams(): void
{
$schema = new EnumSchemaDefinition('Rating', ['1', '2', '3', '4', '5']);
$this->assertSame('Rating', $schema->title);
$this->assertSame(['1', '2', '3', '4', '5'], $schema->enum);
$this->assertNull($schema->description);
$this->assertNull($schema->default);
$this->assertNull($schema->enumNames);
}
public function testConstructorWithAllParams(): void
{
$schema = new EnumSchemaDefinition(
title: 'Satisfaction',
enum: ['poor', 'fair', 'good', 'excellent'],
description: 'Rate your satisfaction',
default: 'good',
enumNames: ['Poor', 'Fair', 'Good', 'Excellent'],
);
$this->assertSame('Satisfaction', $schema->title);
$this->assertSame(['poor', 'fair', 'good', 'excellent'], $schema->enum);
$this->assertSame('Rate your satisfaction', $schema->description);
$this->assertSame('good', $schema->default);
$this->assertSame(['Poor', 'Fair', 'Good', 'Excellent'], $schema->enumNames);
}
public function testConstructorWithEmptyEnum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('enum array must not be empty');
new EnumSchemaDefinition('Test', []);
}
public function testConstructorWithNonStringEnumValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('All enum values must be strings');
/* @phpstan-ignore argument.type */
new EnumSchemaDefinition('Test', ['a', 1, 'b']);
}
public function testConstructorWithEnumNamesMismatch(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('enumNames length must match enum length');
new EnumSchemaDefinition(
title: 'Test',
enum: ['a', 'b', 'c'],
enumNames: ['A', 'B'],
);
}
public function testConstructorWithInvalidDefault(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Default value "invalid" is not in the enum array');
new EnumSchemaDefinition(
title: 'Test',
enum: ['a', 'b', 'c'],
default: 'invalid',
);
}
public function testFromArrayWithMinimalParams(): void
{
$schema = EnumSchemaDefinition::fromArray([
'title' => 'Rating',
'enum' => ['1', '2', '3'],
]);
$this->assertSame('Rating', $schema->title);
$this->assertSame(['1', '2', '3'], $schema->enum);
}
public function testFromArrayWithAllParams(): void
{
$schema = EnumSchemaDefinition::fromArray([
'title' => 'Satisfaction',
'enum' => ['poor', 'fair', 'good'],
'description' => 'Rate your satisfaction',
'default' => 'fair',
'enumNames' => ['Poor', 'Fair', 'Good'],
]);
$this->assertSame('Satisfaction', $schema->title);
$this->assertSame(['poor', 'fair', 'good'], $schema->enum);
$this->assertSame('Rate your satisfaction', $schema->description);
$this->assertSame('fair', $schema->default);
$this->assertSame(['Poor', 'Fair', 'Good'], $schema->enumNames);
}
public function testFromArrayWithMissingTitle(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "title"');
/* @phpstan-ignore argument.type */
EnumSchemaDefinition::fromArray(['enum' => ['a', 'b']]);
}
public function testFromArrayWithMissingEnum(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "enum"');
/* @phpstan-ignore argument.type */
EnumSchemaDefinition::fromArray(['title' => 'Test']);
}
public function testJsonSerializeWithMinimalParams(): void
{
$schema = new EnumSchemaDefinition('Rating', ['1', '2', '3']);
$this->assertSame([
'type' => 'string',
'title' => 'Rating',
'enum' => ['1', '2', '3'],
], $schema->jsonSerialize());
}
public function testJsonSerializeWithAllParams(): void
{
$schema = new EnumSchemaDefinition(
title: 'Satisfaction',
enum: ['poor', 'fair', 'good'],
description: 'Rate your satisfaction',
default: 'fair',
enumNames: ['Poor', 'Fair', 'Good'],
);
$this->assertSame([
'type' => 'string',
'title' => 'Satisfaction',
'enum' => ['poor', 'fair', 'good'],
'description' => 'Rate your satisfaction',
'default' => 'fair',
'enumNames' => ['Poor', 'Fair', 'Good'],
], $schema->jsonSerialize());
}
}