From 2c496a9f9f13c410b36d910051747ad3ead334cb Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 12 Feb 2026 12:45:42 +0100 Subject: [PATCH] feat: improve invalid resource messages --- src/Schema/Resource.php | 4 +- tests/Unit/Schema/ResourceTest.php | 88 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Schema/ResourceTest.php diff --git a/src/Schema/Resource.php b/src/Schema/Resource.php index ac33ed4d..049e19e7 100644 --- a/src/Schema/Resource.php +++ b/src/Schema/Resource.php @@ -68,10 +68,10 @@ public function __construct( public readonly ?array $meta = null, ) { if (!preg_match(self::RESOURCE_NAME_PATTERN, $name)) { - throw new InvalidArgumentException('Invalid resource name: must contain only alphanumeric characters, underscores, and hyphens.'); + throw new InvalidArgumentException(\sprintf('Invalid resource name "%s": must contain only alphanumeric characters, underscores, and hyphens.', $name)); } if (!preg_match(self::URI_PATTERN, $uri)) { - throw new InvalidArgumentException('Invalid resource URI: must be a valid URI with a scheme and optional path.'); + throw new InvalidArgumentException(\sprintf('Invalid resource URI: "%s" must be a valid URI with a scheme and optional path.', $uri)); } } diff --git a/tests/Unit/Schema/ResourceTest.php b/tests/Unit/Schema/ResourceTest.php new file mode 100644 index 00000000..316f48fc --- /dev/null +++ b/tests/Unit/Schema/ResourceTest.php @@ -0,0 +1,88 @@ +assertInstanceOf(Resource::class, $resource); + $this->assertSame($uri, $resource->uri); + } + + public function testConstructorInvalid(): void + { + $uri = '/list-books'; + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid resource URI: "/list-books" must be a valid URI with a scheme and optional path.'); + + $resource = new Resource( + uri: $uri, + name: 'list-books', + ); + } + + public function testFromArrayValid(): void + { + $resource = Resource::fromArray([ + 'uri' => self::VALID_URI, + 'name' => 'list-books', + ]); + + $this->assertInstanceOf(Resource::class, $resource); + $this->assertSame(self::VALID_URI, $resource->uri); + $this->assertSame('list-books', $resource->name); + $this->assertNull($resource->description); + $this->assertNull($resource->meta); + } + + #[DataProvider('provideInvalidResources')] + public function testFromArrayInvalid(array $input, string $expectedExceptionMessage): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage($expectedExceptionMessage); + + Resource::fromArray($input); + } + + public static function provideInvalidResources(): iterable + { + yield 'missing uri' => [[], 'Invalid or missing "uri" in Resource data.']; + yield 'missing name' => [ + ['uri' => self::VALID_URI], + 'Invalid or missing "name" in Resource data.', + ]; + yield 'meta' => [ + [ + 'uri' => self::VALID_URI, + 'name' => 'list-books', + '_meta' => 'foo', + ], + 'Invalid "_meta" in Resource data.', + ]; + } +}