[Server] Reorganize server handlers and transport structure for better organization (#86)
* refactor: reorganize server handlers and transport structure * feat: move the test files to corresponding directories * fix: linter issues * fix: correct PHPDoc formatting and streamline handler imports * refactor: update import statement for ServerBuilder to Builder
This commit is contained in:
parent
3dcd2f717a
commit
41486bfaa4
7 changed files with 65 additions and 67 deletions
297
tests/Unit/Server/Handler/Request/CallToolHandlerTest.php
Normal file
297
tests/Unit/Server/Handler/Request/CallToolHandlerTest.php
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Tool\ToolCallerInterface;
|
||||
use Mcp\Exception\ToolCallException;
|
||||
use Mcp\Exception\ToolNotFoundException;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\CallToolRequest;
|
||||
use Mcp\Schema\Result\CallToolResult;
|
||||
use Mcp\Server\Handler\Request\CallToolHandler;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class CallToolHandlerTest extends TestCase
|
||||
{
|
||||
private CallToolHandler $handler;
|
||||
private ToolCallerInterface|MockObject $toolCaller;
|
||||
private LoggerInterface|MockObject $logger;
|
||||
private SessionInterface|MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->toolCaller = $this->createMock(ToolCallerInterface::class);
|
||||
$this->logger = $this->createMock(LoggerInterface::class);
|
||||
$this->session = $this->createMock(SessionInterface::class);
|
||||
|
||||
$this->handler = new CallToolHandler(
|
||||
$this->toolCaller,
|
||||
$this->logger,
|
||||
);
|
||||
}
|
||||
|
||||
public function testSupportsCallToolRequest(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('test_tool', ['param' => 'value']);
|
||||
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
}
|
||||
|
||||
public function testHandleSuccessfulToolCall(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('greet_user', ['name' => 'John']);
|
||||
$expectedResult = new CallToolResult([new TextContent('Hello, John!')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$this->logger
|
||||
->expects($this->never())
|
||||
->method('error');
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleToolCallWithEmptyArguments(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('simple_tool', []);
|
||||
$expectedResult = new CallToolResult([new TextContent('Simple result')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleToolCallWithComplexArguments(): void
|
||||
{
|
||||
$arguments = [
|
||||
'string_param' => 'value',
|
||||
'int_param' => 42,
|
||||
'bool_param' => true,
|
||||
'array_param' => ['nested' => 'data'],
|
||||
'null_param' => null,
|
||||
];
|
||||
$request = $this->createCallToolRequest('complex_tool', $arguments);
|
||||
$expectedResult = new CallToolResult([new TextContent('Complex result')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleToolNotFoundExceptionReturnsError(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('nonexistent_tool', ['param' => 'value']);
|
||||
$exception = new ToolNotFoundException($request);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$this->logger
|
||||
->expects($this->once())
|
||||
->method('error')
|
||||
->with(
|
||||
'Error while executing tool "nonexistent_tool": "Tool not found for call: "nonexistent_tool".".',
|
||||
[
|
||||
'tool' => 'nonexistent_tool',
|
||||
'arguments' => ['param' => 'value'],
|
||||
],
|
||||
);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while executing tool', $response->message);
|
||||
}
|
||||
|
||||
public function testHandleToolExecutionExceptionReturnsError(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('failing_tool', ['param' => 'value']);
|
||||
$exception = new ToolCallException($request, new \RuntimeException('Tool execution failed'));
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$this->logger
|
||||
->expects($this->once())
|
||||
->method('error')
|
||||
->with(
|
||||
'Error while executing tool "failing_tool": "Tool call "failing_tool" failed with error: "Tool execution failed".".',
|
||||
[
|
||||
'tool' => 'failing_tool',
|
||||
'arguments' => ['param' => 'value'],
|
||||
],
|
||||
);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while executing tool', $response->message);
|
||||
}
|
||||
|
||||
public function testHandleWithNullResult(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('null_tool', []);
|
||||
$expectedResult = new CallToolResult([]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleWithErrorResult(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('error_tool', []);
|
||||
$expectedResult = CallToolResult::error([new TextContent('Tool error occurred')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertTrue($response->result->isError);
|
||||
}
|
||||
|
||||
public function testConstructorWithDefaultLogger(): void
|
||||
{
|
||||
$handler = new CallToolHandler($this->toolCaller);
|
||||
|
||||
$this->assertInstanceOf(CallToolHandler::class, $handler);
|
||||
}
|
||||
|
||||
public function testHandleLogsErrorWithCorrectParameters(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('test_tool', ['key1' => 'value1', 'key2' => 42]);
|
||||
$exception = new ToolCallException($request, new \RuntimeException('Custom error message'));
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->willThrowException($exception);
|
||||
|
||||
$this->logger
|
||||
->expects($this->once())
|
||||
->method('error')
|
||||
->with(
|
||||
'Error while executing tool "test_tool": "Tool call "test_tool" failed with error: "Custom error message".".',
|
||||
[
|
||||
'tool' => 'test_tool',
|
||||
'arguments' => ['key1' => 'value1', 'key2' => 42],
|
||||
],
|
||||
);
|
||||
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
public function testHandleWithSpecialCharactersInToolName(): void
|
||||
{
|
||||
$request = $this->createCallToolRequest('tool-with_special.chars', []);
|
||||
$expectedResult = new CallToolResult([new TextContent('Special tool result')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleWithSpecialCharactersInArguments(): void
|
||||
{
|
||||
$arguments = [
|
||||
'special_chars' => 'äöü ñ 中文 🚀',
|
||||
'unicode' => '\\u{1F600}',
|
||||
'quotes' => 'text with "quotes" and \'single quotes\'',
|
||||
];
|
||||
$request = $this->createCallToolRequest('unicode_tool', $arguments);
|
||||
$expectedResult = new CallToolResult([new TextContent('Unicode handled')]);
|
||||
|
||||
$this->toolCaller
|
||||
->expects($this->once())
|
||||
->method('call')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $arguments
|
||||
*/
|
||||
private function createCallToolRequest(string $name, array $arguments): CallToolRequest
|
||||
{
|
||||
return CallToolRequest::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => CallToolRequest::getMethod(),
|
||||
'id' => 'test-request-'.uniqid(),
|
||||
'params' => [
|
||||
'name' => $name,
|
||||
'arguments' => $arguments,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
345
tests/Unit/Server/Handler/Request/GetPromptHandlerTest.php
Normal file
345
tests/Unit/Server/Handler/Request/GetPromptHandlerTest.php
Normal file
|
|
@ -0,0 +1,345 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Prompt\PromptGetterInterface;
|
||||
use Mcp\Exception\PromptGetException;
|
||||
use Mcp\Exception\PromptNotFoundException;
|
||||
use Mcp\Schema\Content\PromptMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\GetPromptRequest;
|
||||
use Mcp\Schema\Result\GetPromptResult;
|
||||
use Mcp\Server\Handler\Request\GetPromptHandler;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GetPromptHandlerTest extends TestCase
|
||||
{
|
||||
private GetPromptHandler $handler;
|
||||
private PromptGetterInterface|MockObject $promptGetter;
|
||||
private SessionInterface|MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->promptGetter = $this->createMock(PromptGetterInterface::class);
|
||||
$this->session = $this->createMock(SessionInterface::class);
|
||||
|
||||
$this->handler = new GetPromptHandler($this->promptGetter);
|
||||
}
|
||||
|
||||
public function testSupportsGetPromptRequest(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('test_prompt');
|
||||
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
}
|
||||
|
||||
public function testHandleSuccessfulPromptGet(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('greeting_prompt');
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Hello, how can I help you?')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A greeting prompt',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithArguments(): void
|
||||
{
|
||||
$arguments = [
|
||||
'name' => 'John',
|
||||
'context' => 'business meeting',
|
||||
'formality' => 'formal',
|
||||
];
|
||||
$request = $this->createGetPromptRequest('personalized_prompt', $arguments);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(
|
||||
Role::User,
|
||||
new TextContent('Good morning, John. How may I assist you in your business meeting?'),
|
||||
),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A personalized greeting prompt',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithNullArguments(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('simple_prompt', null);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::Assistant, new TextContent('I am ready to help.')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A simple prompt',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithEmptyArguments(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('empty_args_prompt', []);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Default message')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A prompt with empty arguments',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithMultipleMessages(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('conversation_prompt');
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Hello')),
|
||||
new PromptMessage(Role::Assistant, new TextContent('Hi there! How can I help you today?')),
|
||||
new PromptMessage(Role::User, new TextContent('I need assistance with my project')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A conversation prompt',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertCount(3, $response->result->messages);
|
||||
}
|
||||
|
||||
public function testHandlePromptNotFoundExceptionReturnsError(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('nonexistent_prompt');
|
||||
$exception = new PromptNotFoundException($request);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while handling prompt', $response->message);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetExceptionReturnsError(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('failing_prompt');
|
||||
$exception = new PromptGetException($request, new \RuntimeException('Failed to get prompt'));
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while handling prompt', $response->message);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithComplexArguments(): void
|
||||
{
|
||||
$arguments = [
|
||||
'user_data' => [
|
||||
'name' => 'Alice',
|
||||
'preferences' => ['formal', 'concise'],
|
||||
'history' => [
|
||||
'last_interaction' => '2025-01-15',
|
||||
'topics' => ['technology', 'business'],
|
||||
],
|
||||
],
|
||||
'context' => 'technical consultation',
|
||||
'metadata' => [
|
||||
'session_id' => 'sess_123456',
|
||||
'timestamp' => 1705392000,
|
||||
],
|
||||
];
|
||||
$request = $this->createGetPromptRequest('complex_prompt', $arguments);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Complex prompt generated with all parameters')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A complex prompt with nested arguments',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithSpecialCharacters(): void
|
||||
{
|
||||
$arguments = [
|
||||
'message' => 'Hello 世界! How are you? 😊',
|
||||
'special' => 'äöü ñ ß',
|
||||
'quotes' => 'Text with "double" and \'single\' quotes',
|
||||
];
|
||||
$request = $this->createGetPromptRequest('unicode_prompt', $arguments);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Unicode message processed')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A prompt handling unicode characters',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetReturnsEmptyMessages(): void
|
||||
{
|
||||
$request = $this->createGetPromptRequest('empty_prompt');
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'An empty prompt',
|
||||
messages: [],
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertCount(0, $response->result->messages);
|
||||
}
|
||||
|
||||
public function testHandlePromptGetWithLargeNumberOfArguments(): void
|
||||
{
|
||||
$arguments = [];
|
||||
for ($i = 0; $i < 100; ++$i) {
|
||||
$arguments["arg_{$i}"] = "value_{$i}";
|
||||
}
|
||||
|
||||
$request = $this->createGetPromptRequest('many_args_prompt', $arguments);
|
||||
$expectedMessages = [
|
||||
new PromptMessage(Role::User, new TextContent('Processed 100 arguments')),
|
||||
];
|
||||
$expectedResult = new GetPromptResult(
|
||||
description: 'A prompt with many arguments',
|
||||
messages: $expectedMessages,
|
||||
);
|
||||
|
||||
$this->promptGetter
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed>|null $arguments
|
||||
*/
|
||||
private function createGetPromptRequest(string $name, ?array $arguments = null): GetPromptRequest
|
||||
{
|
||||
return GetPromptRequest::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => GetPromptRequest::getMethod(),
|
||||
'id' => 'test-request-'.uniqid(),
|
||||
'params' => [
|
||||
'name' => $name,
|
||||
'arguments' => $arguments,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
247
tests/Unit/Server/Handler/Request/ListPromptsHandlerTest.php
Normal file
247
tests/Unit/Server/Handler/Request/ListPromptsHandlerTest.php
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\InvalidCursorException;
|
||||
use Mcp\Schema\Prompt;
|
||||
use Mcp\Schema\Request\ListPromptsRequest;
|
||||
use Mcp\Schema\Result\ListPromptsResult;
|
||||
use Mcp\Server\Handler\Request\ListPromptsHandler;
|
||||
use Mcp\Server\Session\InMemorySessionStore;
|
||||
use Mcp\Server\Session\Session;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ListPromptsHandlerTest extends TestCase
|
||||
{
|
||||
private Registry $registry;
|
||||
private ListPromptsHandler $handler;
|
||||
private SessionInterface $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->registry = new Registry();
|
||||
$this->handler = new ListPromptsHandler($this->registry, pageSize: 3); // Use small page size for testing
|
||||
$this->session = new Session(new InMemorySessionStore());
|
||||
}
|
||||
|
||||
#[TestDox('Returns first page when no cursor provided')]
|
||||
public function testReturnsFirstPageWhenNoCursorProvided(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(5);
|
||||
$request = $this->createListPromptsRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(3, $result->prompts);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('prompt_0', $result->prompts[0]->name);
|
||||
$this->assertEquals('prompt_1', $result->prompts[1]->name);
|
||||
$this->assertEquals('prompt_2', $result->prompts[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns paginated prompts with cursor')]
|
||||
public function testReturnsPaginatedPromptsWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(10);
|
||||
$request = $this->createListPromptsRequest(cursor: null);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(3, $result->prompts);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('prompt_0', $result->prompts[0]->name);
|
||||
$this->assertEquals('prompt_1', $result->prompts[1]->name);
|
||||
$this->assertEquals('prompt_2', $result->prompts[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns second page with cursor')]
|
||||
public function testReturnsSecondPageWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(10);
|
||||
$firstPageRequest = $this->createListPromptsRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListPromptsResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListPromptsRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(3, $result->prompts);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('prompt_3', $result->prompts[0]->name);
|
||||
$this->assertEquals('prompt_4', $result->prompts[1]->name);
|
||||
$this->assertEquals('prompt_5', $result->prompts[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns last page with null cursor')]
|
||||
public function testReturnsLastPageWithNullCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(5);
|
||||
$firstPageRequest = $this->createListPromptsRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListPromptsResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListPromptsRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(2, $result->prompts);
|
||||
$this->assertNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('prompt_3', $result->prompts[0]->name);
|
||||
$this->assertEquals('prompt_4', $result->prompts[1]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Handles empty registry')]
|
||||
public function testHandlesEmptyRegistry(): void
|
||||
{
|
||||
// Arrange
|
||||
$request = $this->createListPromptsRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(0, $result->prompts);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for invalid cursor')]
|
||||
public function testThrowsExceptionForInvalidCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(5);
|
||||
$request = $this->createListPromptsRequest(cursor: 'invalid-cursor');
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for cursor beyond bounds')]
|
||||
public function testThrowsExceptionForCursorBeyondBounds(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(5);
|
||||
$outOfBoundsCursor = base64_encode('1000');
|
||||
$request = $this->createListPromptsRequest(cursor: $outOfBoundsCursor);
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Handles cursor at exact boundary')]
|
||||
public function testHandlesCursorAtExactBoundary(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(6);
|
||||
$exactBoundaryCursor = base64_encode('6'); // Exactly at the end
|
||||
$request = $this->createListPromptsRequest(cursor: $exactBoundaryCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $result);
|
||||
$this->assertCount(0, $result->prompts);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Maintains stable cursors across calls')]
|
||||
public function testMaintainsStableCursorsAcrossCalls(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addPromptsToRegistry(10);
|
||||
|
||||
// Act
|
||||
$request = $this->createListPromptsRequest();
|
||||
$response1 = $this->handler->handle($request, $this->session);
|
||||
$response2 = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListPromptsResult $result1 */
|
||||
$result1 = $response1->result;
|
||||
/** @var ListPromptsResult $result2 */
|
||||
$result2 = $response2->result;
|
||||
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
|
||||
$this->assertEquals($result1->prompts, $result2->prompts);
|
||||
}
|
||||
|
||||
private function addPromptsToRegistry(int $count): void
|
||||
{
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$prompt = new Prompt(
|
||||
name: "prompt_$i",
|
||||
description: "Test prompt $i"
|
||||
);
|
||||
|
||||
$this->registry->registerPrompt($prompt, fn () => null);
|
||||
}
|
||||
}
|
||||
|
||||
private function createListPromptsRequest(?string $cursor = null): ListPromptsRequest
|
||||
{
|
||||
$data = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'test-request-id',
|
||||
'method' => 'prompts/list',
|
||||
];
|
||||
|
||||
if (null !== $cursor) {
|
||||
$data['params'] = ['cursor' => $cursor];
|
||||
}
|
||||
|
||||
return ListPromptsRequest::fromArray($data);
|
||||
}
|
||||
}
|
||||
248
tests/Unit/Server/Handler/Request/ListResourcesHandlerTest.php
Normal file
248
tests/Unit/Server/Handler/Request/ListResourcesHandlerTest.php
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\InvalidCursorException;
|
||||
use Mcp\Schema\Request\ListResourcesRequest;
|
||||
use Mcp\Schema\Resource;
|
||||
use Mcp\Schema\Result\ListResourcesResult;
|
||||
use Mcp\Server\Handler\Request\ListResourcesHandler;
|
||||
use Mcp\Server\Session\InMemorySessionStore;
|
||||
use Mcp\Server\Session\Session;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ListResourcesHandlerTest extends TestCase
|
||||
{
|
||||
private Registry $registry;
|
||||
private ListResourcesHandler $handler;
|
||||
private SessionInterface $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->registry = new Registry();
|
||||
$this->handler = new ListResourcesHandler($this->registry, pageSize: 3); // Use small page size for testing
|
||||
$this->session = new Session(new InMemorySessionStore());
|
||||
}
|
||||
|
||||
#[TestDox('Returns first page when no cursor provided')]
|
||||
public function testReturnsFirstPageWhenNoCursorProvided(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(5);
|
||||
$request = $this->createListResourcesRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(3, $result->resources);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('resource://test/resource_0', $result->resources[0]->uri);
|
||||
$this->assertEquals('resource://test/resource_1', $result->resources[1]->uri);
|
||||
$this->assertEquals('resource://test/resource_2', $result->resources[2]->uri);
|
||||
}
|
||||
|
||||
#[TestDox('Returns paginated resources with cursor')]
|
||||
public function testReturnsPaginatedResourcesWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(10);
|
||||
$request = $this->createListResourcesRequest(cursor: null);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(3, $result->resources);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('resource://test/resource_0', $result->resources[0]->uri);
|
||||
$this->assertEquals('resource://test/resource_1', $result->resources[1]->uri);
|
||||
$this->assertEquals('resource://test/resource_2', $result->resources[2]->uri);
|
||||
}
|
||||
|
||||
#[TestDox('Returns second page with cursor')]
|
||||
public function testReturnsSecondPageWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(10);
|
||||
$firstPageRequest = $this->createListResourcesRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListResourcesResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListResourcesRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(3, $result->resources);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('resource://test/resource_3', $result->resources[0]->uri);
|
||||
$this->assertEquals('resource://test/resource_4', $result->resources[1]->uri);
|
||||
$this->assertEquals('resource://test/resource_5', $result->resources[2]->uri);
|
||||
}
|
||||
|
||||
#[TestDox('Returns last page with null cursor')]
|
||||
public function testReturnsLastPageWithNullCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(5);
|
||||
$firstPageRequest = $this->createListResourcesRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListResourcesResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListResourcesRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(2, $result->resources);
|
||||
$this->assertNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('resource://test/resource_3', $result->resources[0]->uri);
|
||||
$this->assertEquals('resource://test/resource_4', $result->resources[1]->uri);
|
||||
}
|
||||
|
||||
#[TestDox('Handles empty registry')]
|
||||
public function testHandlesEmptyRegistry(): void
|
||||
{
|
||||
// Arrange
|
||||
$request = $this->createListResourcesRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(0, $result->resources);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for invalid cursor')]
|
||||
public function testThrowsExceptionForInvalidCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(5);
|
||||
$request = $this->createListResourcesRequest(cursor: 'invalid-cursor');
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for cursor beyond bounds')]
|
||||
public function testThrowsExceptionForCursorBeyondBounds(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(5);
|
||||
$outOfBoundsCursor = base64_encode('100');
|
||||
$request = $this->createListResourcesRequest(cursor: $outOfBoundsCursor);
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Handles cursor at exact boundary')]
|
||||
public function testHandlesCursorAtExactBoundary(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(6);
|
||||
$exactBoundaryCursor = base64_encode('6');
|
||||
$request = $this->createListResourcesRequest(cursor: $exactBoundaryCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $result);
|
||||
$this->assertCount(0, $result->resources);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Maintains stable cursors across calls')]
|
||||
public function testMaintainsStableCursorsAcrossCalls(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addResourcesToRegistry(10);
|
||||
|
||||
// Act
|
||||
$request = $this->createListResourcesRequest();
|
||||
$response1 = $this->handler->handle($request, $this->session);
|
||||
$response2 = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListResourcesResult $result1 */
|
||||
$result1 = $response1->result;
|
||||
/** @var ListResourcesResult $result2 */
|
||||
$result2 = $response2->result;
|
||||
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
|
||||
$this->assertEquals($result1->resources, $result2->resources);
|
||||
}
|
||||
|
||||
private function addResourcesToRegistry(int $count): void
|
||||
{
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$resource = new Resource(
|
||||
uri: "resource://test/resource_$i",
|
||||
name: "resource_$i",
|
||||
description: "Test resource $i"
|
||||
);
|
||||
// Use a simple callable as handler
|
||||
$this->registry->registerResource($resource, fn () => null);
|
||||
}
|
||||
}
|
||||
|
||||
private function createListResourcesRequest(?string $cursor = null): ListResourcesRequest
|
||||
{
|
||||
$data = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'test-request-id',
|
||||
'method' => 'resources/list',
|
||||
];
|
||||
|
||||
if (null !== $cursor) {
|
||||
$data['params'] = ['cursor' => $cursor];
|
||||
}
|
||||
|
||||
return ListResourcesRequest::fromArray($data);
|
||||
}
|
||||
}
|
||||
319
tests/Unit/Server/Handler/Request/ListToolsHandlerTest.php
Normal file
319
tests/Unit/Server/Handler/Request/ListToolsHandlerTest.php
Normal file
|
|
@ -0,0 +1,319 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\InvalidCursorException;
|
||||
use Mcp\Schema\Request\ListToolsRequest;
|
||||
use Mcp\Schema\Result\ListToolsResult;
|
||||
use Mcp\Schema\Tool;
|
||||
use Mcp\Server\Handler\Request\ListToolsHandler;
|
||||
use Mcp\Server\Session\InMemorySessionStore;
|
||||
use Mcp\Server\Session\Session;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ListToolsHandlerTest extends TestCase
|
||||
{
|
||||
private Registry $registry;
|
||||
private ListToolsHandler $handler;
|
||||
private SessionInterface $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->registry = new Registry();
|
||||
$this->handler = new ListToolsHandler($this->registry, pageSize: 3); // Use small page size for testing
|
||||
$this->session = new Session(new InMemorySessionStore());
|
||||
}
|
||||
|
||||
#[TestDox('Returns first page when no cursor provided')]
|
||||
public function testReturnsFirstPageWhenNoCursorProvided(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(5);
|
||||
$request = $this->createListToolsRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(3, $result->tools);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('tool_0', $result->tools[0]->name);
|
||||
$this->assertEquals('tool_1', $result->tools[1]->name);
|
||||
$this->assertEquals('tool_2', $result->tools[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns paginated tools with cursor')]
|
||||
public function testReturnsPaginatedToolsWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(10);
|
||||
$request = $this->createListToolsRequest(cursor: null);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(3, $result->tools);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('tool_0', $result->tools[0]->name);
|
||||
$this->assertEquals('tool_1', $result->tools[1]->name);
|
||||
$this->assertEquals('tool_2', $result->tools[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns second page with cursor')]
|
||||
public function testReturnsSecondPageWithCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(10);
|
||||
$firstPageRequest = $this->createListToolsRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListToolsResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListToolsRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(3, $result->tools);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('tool_3', $result->tools[0]->name);
|
||||
$this->assertEquals('tool_4', $result->tools[1]->name);
|
||||
$this->assertEquals('tool_5', $result->tools[2]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns last page with null cursor')]
|
||||
public function testReturnsLastPageWithNullCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(5);
|
||||
$firstPageRequest = $this->createListToolsRequest();
|
||||
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
|
||||
|
||||
/** @var ListToolsResult $firstPageResult */
|
||||
$firstPageResult = $firstPageResponse->result;
|
||||
$secondPageRequest = $this->createListToolsRequest(cursor: $firstPageResult->nextCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($secondPageRequest, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(2, $result->tools);
|
||||
$this->assertNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('tool_3', $result->tools[0]->name);
|
||||
$this->assertEquals('tool_4', $result->tools[1]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Returns all tools when count is less than page size')]
|
||||
public function testReturnsAllToolsWhenCountIsLessThanPageSize(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(2); // Less than page size 3
|
||||
$request = $this->createListToolsRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(2, $result->tools);
|
||||
$this->assertNull($result->nextCursor);
|
||||
|
||||
$this->assertEquals('tool_0', $result->tools[0]->name);
|
||||
$this->assertEquals('tool_1', $result->tools[1]->name);
|
||||
}
|
||||
|
||||
#[TestDox('Handles empty registry')]
|
||||
public function testHandlesEmptyRegistry(): void
|
||||
{
|
||||
// Arrange
|
||||
$request = $this->createListToolsRequest();
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(0, $result->tools);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for invalid cursor')]
|
||||
public function testThrowsExceptionForInvalidCursor(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(5);
|
||||
$request = $this->createListToolsRequest(cursor: 'invalid-cursor');
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Throws exception for cursor beyond bounds')]
|
||||
public function testThrowsExceptionForCursorBeyondBounds(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(5);
|
||||
$outOfBoundsCursor = base64_encode('100');
|
||||
$request = $this->createListToolsRequest(cursor: $outOfBoundsCursor);
|
||||
|
||||
// Assert
|
||||
$this->expectException(InvalidCursorException::class);
|
||||
|
||||
// Act
|
||||
$this->handler->handle($request, $this->session);
|
||||
}
|
||||
|
||||
#[TestDox('Handles cursor at exact boundary')]
|
||||
public function testHandlesCursorAtExactBoundary(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(6);
|
||||
$exactBoundaryCursor = base64_encode('6');
|
||||
$request = $this->createListToolsRequest(cursor: $exactBoundaryCursor);
|
||||
|
||||
// Act
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount(0, $result->tools);
|
||||
$this->assertNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Maintains stable cursors across calls')]
|
||||
public function testMaintainsStableCursorsAcrossCalls(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(10);
|
||||
|
||||
// Act
|
||||
$request = $this->createListToolsRequest();
|
||||
$response1 = $this->handler->handle($request, $this->session);
|
||||
$response2 = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result1 */
|
||||
$result1 = $response1->result;
|
||||
/** @var ListToolsResult $result2 */
|
||||
$result2 = $response2->result;
|
||||
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
|
||||
$this->assertEquals($result1->tools, $result2->tools);
|
||||
}
|
||||
|
||||
#[TestDox('Uses custom page size when provided')]
|
||||
public function testUsesCustomPageSizeWhenProvided(): void
|
||||
{
|
||||
// Arrange
|
||||
$customPageSize = 5;
|
||||
$customHandler = new ListToolsHandler($this->registry, pageSize: $customPageSize);
|
||||
$this->addToolsToRegistry(10);
|
||||
$request = $this->createListToolsRequest();
|
||||
|
||||
// Act
|
||||
$response = $customHandler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $result */
|
||||
$result = $response->result;
|
||||
$this->assertInstanceOf(ListToolsResult::class, $result);
|
||||
$this->assertCount($customPageSize, $result->tools);
|
||||
$this->assertNotNull($result->nextCursor);
|
||||
}
|
||||
|
||||
#[TestDox('Different page sizes produce different pagination results')]
|
||||
public function testDifferentPageSizesProduceDifferentPaginationResults(): void
|
||||
{
|
||||
// Arrange
|
||||
$this->addToolsToRegistry(10);
|
||||
$smallPageHandler = new ListToolsHandler($this->registry, pageSize: 2);
|
||||
$largePageHandler = new ListToolsHandler($this->registry, pageSize: 7);
|
||||
$request = $this->createListToolsRequest();
|
||||
|
||||
// Act
|
||||
$smallPageResponse = $smallPageHandler->handle($request, $this->session);
|
||||
$largePageResponse = $largePageHandler->handle($request, $this->session);
|
||||
|
||||
// Assert
|
||||
/** @var ListToolsResult $smallResult */
|
||||
$smallResult = $smallPageResponse->result;
|
||||
/** @var ListToolsResult $largeResult */
|
||||
$largeResult = $largePageResponse->result;
|
||||
|
||||
$this->assertCount(2, $smallResult->tools);
|
||||
$this->assertCount(7, $largeResult->tools);
|
||||
$this->assertNotNull($smallResult->nextCursor);
|
||||
$this->assertNotNull($largeResult->nextCursor);
|
||||
}
|
||||
|
||||
private function addToolsToRegistry(int $count): void
|
||||
{
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$tool = new Tool(
|
||||
name: "tool_$i",
|
||||
inputSchema: [
|
||||
'type' => 'object',
|
||||
'properties' => [],
|
||||
'required' => [],
|
||||
],
|
||||
description: "Test tool $i",
|
||||
annotations: null
|
||||
);
|
||||
|
||||
$this->registry->registerTool($tool, fn () => null);
|
||||
}
|
||||
}
|
||||
|
||||
private function createListToolsRequest(?string $cursor = null): ListToolsRequest
|
||||
{
|
||||
$data = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'test-request-id',
|
||||
'method' => 'tools/list',
|
||||
];
|
||||
|
||||
if (null !== $cursor) {
|
||||
$data['params'] = ['cursor' => $cursor];
|
||||
}
|
||||
|
||||
return ListToolsRequest::fromArray($data);
|
||||
}
|
||||
}
|
||||
150
tests/Unit/Server/Handler/Request/PingHandlerTest.php
Normal file
150
tests/Unit/Server/Handler/Request/PingHandlerTest.php
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Schema\JsonRpc\Request;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\PingRequest;
|
||||
use Mcp\Schema\Result\EmptyResult;
|
||||
use Mcp\Server\Handler\Request\PingHandler;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PingHandlerTest extends TestCase
|
||||
{
|
||||
private PingHandler $handler;
|
||||
private SessionInterface $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->session = $this->createMock(SessionInterface::class);
|
||||
$this->handler = new PingHandler();
|
||||
}
|
||||
|
||||
public function testSupportsPingRequest(): void
|
||||
{
|
||||
$request = $this->createPingRequest();
|
||||
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
}
|
||||
|
||||
public function testHandlePingRequest(): void
|
||||
{
|
||||
$request = $this->createPingRequest();
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertInstanceOf(EmptyResult::class, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleMultiplePingRequests(): void
|
||||
{
|
||||
$request1 = $this->createPingRequest();
|
||||
$request2 = $this->createPingRequest();
|
||||
|
||||
$response1 = $this->handler->handle($request1, $this->session);
|
||||
$response2 = $this->handler->handle($request2, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response1);
|
||||
$this->assertInstanceOf(Response::class, $response2);
|
||||
$this->assertInstanceOf(EmptyResult::class, $response1->result);
|
||||
$this->assertInstanceOf(EmptyResult::class, $response2->result);
|
||||
$this->assertEquals($request1->getId(), $response1->id);
|
||||
$this->assertEquals($request2->getId(), $response2->id);
|
||||
}
|
||||
|
||||
public function testHandlerHasNoSideEffects(): void
|
||||
{
|
||||
$request = $this->createPingRequest();
|
||||
|
||||
// Handle same request multiple times
|
||||
$response1 = $this->handler->handle($request, $this->session);
|
||||
$response2 = $this->handler->handle($request, $this->session);
|
||||
|
||||
// Both responses should be identical
|
||||
$this->assertEquals($response1->id, $response2->id);
|
||||
$this->assertEquals(
|
||||
\get_class($response1->result),
|
||||
\get_class($response2->result),
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyResultIsCorrectType(): void
|
||||
{
|
||||
$request = $this->createPingRequest();
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(EmptyResult::class, $response->result);
|
||||
|
||||
// Verify EmptyResult serializes to empty object
|
||||
$serialized = json_encode($response->result);
|
||||
$this->assertEquals('{}', $serialized);
|
||||
}
|
||||
|
||||
public function testHandlerIsStateless(): void
|
||||
{
|
||||
$handler1 = new PingHandler();
|
||||
$handler2 = new PingHandler();
|
||||
|
||||
$request = $this->createPingRequest();
|
||||
|
||||
$response1 = $handler1->handle($request, $this->session);
|
||||
$response2 = $handler2->handle($request, $this->session);
|
||||
|
||||
// Both handlers should produce equivalent results
|
||||
$this->assertEquals($response1->id, $response2->id);
|
||||
$this->assertEquals(
|
||||
\get_class($response1->result),
|
||||
\get_class($response2->result),
|
||||
);
|
||||
}
|
||||
|
||||
public function testSupportsMethodIsConsistent(): void
|
||||
{
|
||||
$request = $this->createPingRequest();
|
||||
|
||||
// Multiple calls to supports should return same result
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
}
|
||||
|
||||
public function testHandlerCanBeReused(): void
|
||||
{
|
||||
$requests = [];
|
||||
$responses = [];
|
||||
|
||||
// Create multiple ping requests
|
||||
for ($i = 0; $i < 5; ++$i) {
|
||||
$requests[$i] = $this->createPingRequest();
|
||||
$responses[$i] = $this->handler->handle($requests[$i], $this->session);
|
||||
}
|
||||
|
||||
// All responses should be valid
|
||||
foreach ($responses as $i => $response) {
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals($requests[$i]->getId(), $response->id);
|
||||
$this->assertInstanceOf(EmptyResult::class, $response->result);
|
||||
}
|
||||
}
|
||||
|
||||
private function createPingRequest(): Request
|
||||
{
|
||||
return PingRequest::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => PingRequest::getMethod(),
|
||||
'id' => 'test-request-'.uniqid(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
354
tests/Unit/Server/Handler/Request/ReadResourceHandlerTest.php
Normal file
354
tests/Unit/Server/Handler/Request/ReadResourceHandlerTest.php
Normal file
|
|
@ -0,0 +1,354 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Tests\Unit\Server\Handler\Request;
|
||||
|
||||
use Mcp\Capability\Resource\ResourceReaderInterface;
|
||||
use Mcp\Exception\ResourceNotFoundException;
|
||||
use Mcp\Exception\ResourceReadException;
|
||||
use Mcp\Schema\Content\BlobResourceContents;
|
||||
use Mcp\Schema\Content\TextResourceContents;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\ReadResourceRequest;
|
||||
use Mcp\Schema\Result\ReadResourceResult;
|
||||
use Mcp\Server\Handler\Request\ReadResourceHandler;
|
||||
use Mcp\Server\Session\SessionInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ReadResourceHandlerTest extends TestCase
|
||||
{
|
||||
private ReadResourceHandler $handler;
|
||||
private ResourceReaderInterface|MockObject $resourceReader;
|
||||
private SessionInterface|MockObject $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->resourceReader = $this->createMock(ResourceReaderInterface::class);
|
||||
$this->session = $this->createMock(SessionInterface::class);
|
||||
|
||||
$this->handler = new ReadResourceHandler($this->resourceReader);
|
||||
}
|
||||
|
||||
public function testSupportsReadResourceRequest(): void
|
||||
{
|
||||
$request = $this->createReadResourceRequest('file://test.txt');
|
||||
|
||||
$this->assertTrue($this->handler->supports($request));
|
||||
}
|
||||
|
||||
public function testHandleSuccessfulResourceRead(): void
|
||||
{
|
||||
$uri = 'file://documents/readme.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'text/plain',
|
||||
text: 'This is the content of the readme file.',
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithBlobContent(): void
|
||||
{
|
||||
$uri = 'file://images/logo.png';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedContent = new BlobResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'image/png',
|
||||
blob: base64_encode('fake-image-data'),
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithMultipleContents(): void
|
||||
{
|
||||
$uri = 'app://data/mixed-content';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$textContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'text/plain',
|
||||
text: 'Text part of the resource',
|
||||
);
|
||||
$blobContent = new BlobResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'application/octet-stream',
|
||||
blob: base64_encode('binary-data'),
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$textContent, $blobContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertCount(2, $response->result->contents);
|
||||
}
|
||||
|
||||
public function testHandleResourceNotFoundExceptionReturnsSpecificError(): void
|
||||
{
|
||||
$uri = 'file://nonexistent/file.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$exception = new ResourceNotFoundException($request);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::RESOURCE_NOT_FOUND, $response->code);
|
||||
$this->assertEquals('Resource not found for uri: "'.$uri.'".', $response->message);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadExceptionReturnsGenericError(): void
|
||||
{
|
||||
$uri = 'file://corrupted/file.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$exception = new ResourceReadException(
|
||||
$request,
|
||||
new \RuntimeException('Failed to read resource: corrupted data'),
|
||||
);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals($request->getId(), $response->id);
|
||||
$this->assertEquals(Error::INTERNAL_ERROR, $response->code);
|
||||
$this->assertEquals('Error while reading resource', $response->message);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithDifferentUriSchemes(): void
|
||||
{
|
||||
$uriSchemes = [
|
||||
'file://local/path/file.txt',
|
||||
'http://example.com/resource',
|
||||
'https://secure.example.com/api/data',
|
||||
'ftp://files.example.com/document.pdf',
|
||||
'app://internal/resource/123',
|
||||
'custom-scheme://special/resource',
|
||||
];
|
||||
|
||||
foreach ($uriSchemes as $uri) {
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'text/plain',
|
||||
text: "Content for {$uri}",
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
|
||||
// Reset the mock for next iteration
|
||||
$this->resourceReader = $this->createMock(ResourceReaderInterface::class);
|
||||
$this->handler = new ReadResourceHandler($this->resourceReader);
|
||||
}
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithSpecialCharactersInUri(): void
|
||||
{
|
||||
$uri = 'file://path/with spaces/äöü-file-ñ.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'text/plain',
|
||||
text: 'Content with unicode characters: äöü ñ 世界 🚀',
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithEmptyContent(): void
|
||||
{
|
||||
$uri = 'file://empty/file.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: 'text/plain',
|
||||
text: '',
|
||||
);
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertInstanceOf(TextResourceContents::class, $response->result->contents[0]);
|
||||
$this->assertEquals('', $response->result->contents[0]->text);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithDifferentMimeTypes(): void
|
||||
{
|
||||
$mimeTypes = [
|
||||
'text/plain',
|
||||
'text/html',
|
||||
'application/json',
|
||||
'application/xml',
|
||||
'image/png',
|
||||
'image/jpeg',
|
||||
'application/pdf',
|
||||
'video/mp4',
|
||||
'audio/mpeg',
|
||||
'application/octet-stream',
|
||||
];
|
||||
|
||||
foreach ($mimeTypes as $i => $mimeType) {
|
||||
$uri = "file://test/file{$i}";
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
|
||||
if (str_starts_with($mimeType, 'text/') || str_starts_with($mimeType, 'application/json')) {
|
||||
$expectedContent = new TextResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: $mimeType,
|
||||
text: "Content for {$mimeType}",
|
||||
);
|
||||
} else {
|
||||
$expectedContent = new BlobResourceContents(
|
||||
uri: $uri,
|
||||
mimeType: $mimeType,
|
||||
blob: base64_encode("binary-content-for-{$mimeType}"),
|
||||
);
|
||||
}
|
||||
$expectedResult = new ReadResourceResult([$expectedContent]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertEquals($mimeType, $response->result->contents[0]->mimeType);
|
||||
|
||||
// Reset the mock for next iteration
|
||||
$this->resourceReader = $this->createMock(ResourceReaderInterface::class);
|
||||
$this->handler = new ReadResourceHandler($this->resourceReader);
|
||||
}
|
||||
}
|
||||
|
||||
public function testHandleResourceNotFoundWithCustomMessage(): void
|
||||
{
|
||||
$uri = 'file://custom/missing.txt';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$exception = new ResourceNotFoundException($request);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willThrowException($exception);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Error::class, $response);
|
||||
$this->assertEquals(Error::RESOURCE_NOT_FOUND, $response->code);
|
||||
$this->assertEquals('Resource not found for uri: "'.$uri.'".', $response->message);
|
||||
}
|
||||
|
||||
public function testHandleResourceReadWithEmptyResult(): void
|
||||
{
|
||||
$uri = 'file://empty/resource';
|
||||
$request = $this->createReadResourceRequest($uri);
|
||||
$expectedResult = new ReadResourceResult([]);
|
||||
|
||||
$this->resourceReader
|
||||
->expects($this->once())
|
||||
->method('read')
|
||||
->with($request)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$response = $this->handler->handle($request, $this->session);
|
||||
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertSame($expectedResult, $response->result);
|
||||
$this->assertCount(0, $response->result->contents);
|
||||
}
|
||||
|
||||
private function createReadResourceRequest(string $uri): ReadResourceRequest
|
||||
{
|
||||
return ReadResourceRequest::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => ReadResourceRequest::getMethod(),
|
||||
'id' => 'test-request-'.uniqid(),
|
||||
'params' => [
|
||||
'uri' => $uri,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue