|
| 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\Capability\Registry; |
| 13 | + |
| 14 | +use Mcp\Capability\Registry\ElementReference; |
| 15 | +use Mcp\Capability\Registry\ReferenceHandler; |
| 16 | +use Mcp\Schema\Metadata; |
| 17 | +use Mcp\Server\Session\SessionInterface; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +final class ReferenceHandlerTest extends TestCase |
| 22 | +{ |
| 23 | + private ReferenceHandler $handler; |
| 24 | + private SessionInterface&MockObject $session; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->handler = new ReferenceHandler(); |
| 29 | + $this->session = $this->createMock(SessionInterface::class); |
| 30 | + } |
| 31 | + |
| 32 | + public function testInjectsMetadataIntoTypedParameter(): void |
| 33 | + { |
| 34 | + $fn = function (Metadata $meta): string { |
| 35 | + return (string) $meta->get('securitySchema'); |
| 36 | + }; |
| 37 | + |
| 38 | + $ref = new ElementReference($fn); |
| 39 | + |
| 40 | + $result = $this->handler->handle($ref, [ |
| 41 | + '_session' => $this->session, |
| 42 | + '_meta' => ['securitySchema' => 'secure-123'], |
| 43 | + ]); |
| 44 | + |
| 45 | + $this->assertSame('secure-123', $result); |
| 46 | + } |
| 47 | + |
| 48 | + public function testNullableMetadataReceivesNullWhenNotProvided(): void |
| 49 | + { |
| 50 | + $fn = function (?Metadata $meta): string { |
| 51 | + return null === $meta ? 'no-meta' : 'has-meta'; |
| 52 | + }; |
| 53 | + |
| 54 | + $ref = new ElementReference($fn); |
| 55 | + |
| 56 | + $result = $this->handler->handle($ref, [ |
| 57 | + '_session' => $this->session, |
| 58 | + ]); |
| 59 | + |
| 60 | + $this->assertSame('no-meta', $result); |
| 61 | + } |
| 62 | + |
| 63 | + public function testRequiredMetadataThrowsInternalErrorWhenNotProvided(): void |
| 64 | + { |
| 65 | + $fn = function (Metadata $meta): array { |
| 66 | + return $meta->all(); |
| 67 | + }; |
| 68 | + |
| 69 | + $ref = new ElementReference($fn); |
| 70 | + |
| 71 | + $this->expectException(\Mcp\Exception\RegistryException::class); |
| 72 | + $this->expectExceptionMessage('Missing required request metadata'); |
| 73 | + |
| 74 | + $this->handler->handle($ref, [ |
| 75 | + '_session' => $this->session, |
| 76 | + ]); |
| 77 | + } |
| 78 | +} |
0 commit comments