Skip to content

Commit e5cdce6

Browse files
feat: improve invalid resource messages
1 parent 8f8af90 commit e5cdce6

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

src/Schema/Resource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public function __construct(
6868
public readonly ?array $meta = null,
6969
) {
7070
if (!preg_match(self::RESOURCE_NAME_PATTERN, $name)) {
71-
throw new InvalidArgumentException('Invalid resource name: must contain only alphanumeric characters, underscores, and hyphens.');
71+
throw new InvalidArgumentException('Invalid resource name "'.$name.'": must contain only alphanumeric characters, underscores, and hyphens.');
7272
}
7373
if (!preg_match(self::URI_PATTERN, $uri)) {
74-
throw new InvalidArgumentException('Invalid resource URI: must be a valid URI with a scheme and optional path.');
74+
throw new InvalidArgumentException('Invalid resource URI: "'.$uri.'" must be a valid URI with a scheme and optional path.');
7575
}
7676
}
7777

tests/Unit/Schema/ResourceTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Tests\Unit\Schema;
13+
14+
use Mcp\Exception\InvalidArgumentException;
15+
use Mcp\Schema\Resource;
16+
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ResourceTest extends TestCase
20+
{
21+
private const VALID_URI = 'https://example.com/list-books';
22+
23+
public function testConstructorValid(): void
24+
{
25+
$uri = self::VALID_URI;
26+
27+
$resource = new Resource(
28+
uri: $uri,
29+
name: 'list-books',
30+
);
31+
32+
$this->assertInstanceOf(Resource::class, $resource);
33+
$this->assertSame($uri, $resource->uri);
34+
}
35+
36+
public function testConstructorInvalid(): void
37+
{
38+
$uri = '/list-books';
39+
40+
$this->expectException(InvalidArgumentException::class);
41+
$this->expectExceptionMessage('Invalid resource URI: "/list-books" must be a valid URI with a scheme and optional path.');
42+
43+
$resource = new Resource(
44+
uri: $uri,
45+
name: 'list-books',
46+
);
47+
}
48+
49+
public function testFromArrayValid(): void
50+
{
51+
$resource = Resource::fromArray([
52+
'uri' => self::VALID_URI,
53+
'name' => 'list-books',
54+
]);
55+
56+
$this->assertInstanceOf(Resource::class, $resource);
57+
$this->assertSame(self::VALID_URI, $resource->uri);
58+
$this->assertSame('list-books', $resource->name);
59+
$this->assertNull($resource->description);
60+
$this->assertNull($resource->meta);
61+
}
62+
63+
#[DataProvider('provideInvalidResources')]
64+
public function testFromArrayInvalid(array $input, string $expectedExceptionMessage): void
65+
{
66+
$this->expectException(InvalidArgumentException::class);
67+
$this->expectExceptionMessage($expectedExceptionMessage);
68+
69+
Resource::fromArray($input);
70+
}
71+
72+
public static function provideInvalidResources(): iterable
73+
{
74+
yield 'missing uri' => [[], 'Invalid or missing "uri" in Resource data.'];
75+
yield 'missing name' => [
76+
['uri' => self::VALID_URI],
77+
'Invalid or missing "name" in Resource data.',
78+
];
79+
yield 'meta' => [
80+
[
81+
'uri' => self::VALID_URI,
82+
'name' => 'list-books',
83+
'_meta' => 'foo',
84+
],
85+
'Invalid "_meta" in Resource data.',
86+
];
87+
}
88+
}

0 commit comments

Comments
 (0)