|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 Symfony\AI\McpSdk\Tests\Server; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\TestDox; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\Log\NullLogger; |
| 19 | +use Symfony\AI\McpSdk\Message\Factory; |
| 20 | +use Symfony\AI\McpSdk\Message\Response; |
| 21 | +use Symfony\AI\McpSdk\Server\JsonRpcHandler; |
| 22 | +use Symfony\AI\McpSdk\Server\NotificationHandlerInterface; |
| 23 | +use Symfony\AI\McpSdk\Server\RequestHandlerInterface; |
| 24 | + |
| 25 | +#[Small] |
| 26 | +#[CoversClass(JsonRpcHandler::class)] |
| 27 | +class JsonRpcHandlerTest extends TestCase |
| 28 | +{ |
| 29 | + #[TestDox('Make sure a single notification can be handled by multiple handlers.')] |
| 30 | + public function testHandleMultipleNotifications(): void |
| 31 | + { |
| 32 | + $handlerA = $this->getMockBuilder(NotificationHandlerInterface::class) |
| 33 | + ->disableOriginalConstructor() |
| 34 | + ->onlyMethods(['supports', 'handle']) |
| 35 | + ->getMock(); |
| 36 | + $handlerA->method('supports')->willReturn(true); |
| 37 | + $handlerA->expects($this->once())->method('handle'); |
| 38 | + |
| 39 | + $handlerB = $this->getMockBuilder(NotificationHandlerInterface::class) |
| 40 | + ->disableOriginalConstructor() |
| 41 | + ->onlyMethods(['supports', 'handle']) |
| 42 | + ->getMock(); |
| 43 | + $handlerB->method('supports')->willReturn(false); |
| 44 | + $handlerB->expects($this->never())->method('handle'); |
| 45 | + |
| 46 | + $handlerC = $this->getMockBuilder(NotificationHandlerInterface::class) |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->onlyMethods(['supports', 'handle']) |
| 49 | + ->getMock(); |
| 50 | + $handlerC->method('supports')->willReturn(true); |
| 51 | + $handlerC->expects($this->once())->method('handle'); |
| 52 | + |
| 53 | + $jsonRpc = new JsonRpcHandler(new Factory(), [], [$handlerA, $handlerB, $handlerC], new NullLogger()); |
| 54 | + $jsonRpc->process( |
| 55 | + '{"jsonrpc": "2.0", "id": 1, "method": "notifications/foobar"}' |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + #[TestDox('Make sure a single request can NOT be handled by multiple handlers.')] |
| 60 | + public function testHandleMultipleRequests(): void |
| 61 | + { |
| 62 | + $handlerA = $this->getMockBuilder(RequestHandlerInterface::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->onlyMethods(['supports', 'createResponse']) |
| 65 | + ->getMock(); |
| 66 | + $handlerA->method('supports')->willReturn(true); |
| 67 | + $handlerA->expects($this->once())->method('createResponse')->willReturn(new Response(1)); |
| 68 | + |
| 69 | + $handlerB = $this->getMockBuilder(RequestHandlerInterface::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->onlyMethods(['supports', 'createResponse']) |
| 72 | + ->getMock(); |
| 73 | + $handlerB->method('supports')->willReturn(false); |
| 74 | + $handlerB->expects($this->never())->method('createResponse'); |
| 75 | + |
| 76 | + $handlerC = $this->getMockBuilder(RequestHandlerInterface::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->onlyMethods(['supports', 'createResponse']) |
| 79 | + ->getMock(); |
| 80 | + $handlerC->method('supports')->willReturn(true); |
| 81 | + $handlerC->expects($this->never())->method('createResponse'); |
| 82 | + |
| 83 | + $jsonRpc = new JsonRpcHandler(new Factory(), [$handlerA, $handlerB, $handlerC], [], new NullLogger()); |
| 84 | + $jsonRpc->process( |
| 85 | + '{"jsonrpc": "2.0", "id": 1, "method": "request/foobar"}' |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments