|
| 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