Adopt Schema component in server handlers
This commit is contained in:
parent
6025f890f9
commit
09707b4798
12 changed files with 1516 additions and 2424 deletions
|
|
@ -1,50 +0,0 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Mcp\Server\TransportInterface;
|
||||
|
||||
class InMemoryTransport implements TransportInterface
|
||||
{
|
||||
private bool $connected = true;
|
||||
|
||||
/**
|
||||
* @param list<string> $messages
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly array $messages = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function isConnected(): bool
|
||||
{
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
public function receive(): \Generator
|
||||
{
|
||||
yield from $this->messages;
|
||||
$this->connected = false;
|
||||
}
|
||||
|
||||
public function send(string $data): void
|
||||
{
|
||||
}
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
84
tests/JsonRpc/HandlerTest.php
Normal file
84
tests/JsonRpc/HandlerTest.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?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\JsonRpc;
|
||||
|
||||
use Mcp\JsonRpc\Handler;
|
||||
use Mcp\JsonRpc\MessageFactory;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HandlerTest extends TestCase
|
||||
{
|
||||
#[TestDox('Make sure a single notification can be handled by multiple handlers.')]
|
||||
public function testHandleMultipleNotifications()
|
||||
{
|
||||
$handlerA = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerA->method('supports')->willReturn(true);
|
||||
$handlerA->expects($this->once())->method('handle');
|
||||
|
||||
$handlerB = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerB->method('supports')->willReturn(false);
|
||||
$handlerB->expects($this->never())->method('handle');
|
||||
|
||||
$handlerC = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerC->method('supports')->willReturn(true);
|
||||
$handlerC->expects($this->once())->method('handle');
|
||||
|
||||
$jsonRpc = new Handler(MessageFactory::make(), [$handlerA, $handlerB, $handlerC]);
|
||||
$result = $jsonRpc->process(
|
||||
'{"jsonrpc": "2.0", "method": "notifications/initialized"}'
|
||||
);
|
||||
iterator_to_array($result);
|
||||
}
|
||||
|
||||
#[TestDox('Make sure a single request can NOT be handled by multiple handlers.')]
|
||||
public function testHandleMultipleRequests()
|
||||
{
|
||||
$handlerA = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerA->method('supports')->willReturn(true);
|
||||
$handlerA->expects($this->once())->method('handle')->willReturn(new Response(1, ['result' => 'success']));
|
||||
|
||||
$handlerB = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerB->method('supports')->willReturn(false);
|
||||
$handlerB->expects($this->never())->method('handle');
|
||||
|
||||
$handlerC = $this->getMockBuilder(MethodHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerC->method('supports')->willReturn(true);
|
||||
$handlerC->expects($this->never())->method('handle');
|
||||
|
||||
$jsonRpc = new Handler(MessageFactory::make(), [$handlerA, $handlerB, $handlerC]);
|
||||
$result = $jsonRpc->process(
|
||||
'{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
|
||||
);
|
||||
iterator_to_array($result);
|
||||
}
|
||||
}
|
||||
95
tests/JsonRpc/MessageFactoryTest.php
Normal file
95
tests/JsonRpc/MessageFactoryTest.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?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\JsonRpc;
|
||||
|
||||
use Mcp\Exception\InvalidInputMessageException;
|
||||
use Mcp\JsonRpc\MessageFactory;
|
||||
use Mcp\Schema\Notification\CancelledNotification;
|
||||
use Mcp\Schema\Notification\InitializedNotification;
|
||||
use Mcp\Schema\Request\GetPromptRequest;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MessageFactoryTest extends TestCase
|
||||
{
|
||||
private MessageFactory $factory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->factory = new MessageFactory([
|
||||
CancelledNotification::class,
|
||||
InitializedNotification::class,
|
||||
GetPromptRequest::class,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testCreateRequest()
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "prompts/get", "params": {"name": "create_story"}, "id": 123}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(GetPromptRequest::class, $result);
|
||||
$this->assertSame('prompts/get', $result::getMethod());
|
||||
$this->assertSame('create_story', $result->name);
|
||||
$this->assertSame(123, $result->getId());
|
||||
}
|
||||
|
||||
public function testCreateNotification()
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 12345}}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(CancelledNotification::class, $result);
|
||||
$this->assertSame('notifications/cancelled', $result::getMethod());
|
||||
$this->assertSame(12345, $result->requestId);
|
||||
}
|
||||
|
||||
public function testInvalidJson()
|
||||
{
|
||||
$this->expectException(\JsonException::class);
|
||||
|
||||
$this->first($this->factory->create('invalid json'));
|
||||
}
|
||||
|
||||
public function testMissingMethod()
|
||||
{
|
||||
$result = $this->first($this->factory->create('{"jsonrpc": "2.0", "params": {}, "id": 1}'));
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing valid "method".', $result->getMessage());
|
||||
}
|
||||
|
||||
public function testBatchMissingMethod()
|
||||
{
|
||||
$results = $this->factory->create('[{"jsonrpc": "2.0", "params": {}, "id": 1}, {"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}]');
|
||||
|
||||
$results = iterator_to_array($results);
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing valid "method".', $result->getMessage());
|
||||
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(InitializedNotification::class, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<mixed> $items
|
||||
*/
|
||||
private function first(iterable $items): mixed
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Message\Error;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Error::class)]
|
||||
final class ErrorTest extends TestCase
|
||||
{
|
||||
public function testWithIntegerId(): void
|
||||
{
|
||||
$error = new Error(1, -32602, 'Another error occurred');
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'error' => [
|
||||
'code' => -32602,
|
||||
'message' => 'Another error occurred',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $error->jsonSerialize());
|
||||
}
|
||||
|
||||
public function testWithStringId(): void
|
||||
{
|
||||
$error = new Error('abc', -32602, 'Another error occurred');
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'abc',
|
||||
'error' => [
|
||||
'code' => -32602,
|
||||
'message' => 'Another error occurred',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $error->jsonSerialize());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Exception\InvalidInputMessageException;
|
||||
use Mcp\Message\Factory;
|
||||
use Mcp\Message\Notification;
|
||||
use Mcp\Message\Request;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Factory::class)]
|
||||
final class FactoryTest extends TestCase
|
||||
{
|
||||
private Factory $factory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->factory = new Factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<mixed> $items
|
||||
*/
|
||||
private function first(iterable $items): mixed
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function testCreateRequest(): void
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "test_method", "params": {"foo": "bar"}, "id": 123}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(Request::class, $result);
|
||||
$this->assertSame('test_method', $result->method);
|
||||
$this->assertSame(['foo' => 'bar'], $result->params);
|
||||
$this->assertSame(123, $result->id);
|
||||
}
|
||||
|
||||
public function testCreateNotification(): void
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "notifications/test_event", "params": {"foo": "bar"}}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(Notification::class, $result);
|
||||
$this->assertSame('notifications/test_event', $result->method);
|
||||
$this->assertSame(['foo' => 'bar'], $result->params);
|
||||
}
|
||||
|
||||
public function testInvalidJson(): void
|
||||
{
|
||||
$this->expectException(\JsonException::class);
|
||||
|
||||
$this->first($this->factory->create('invalid json'));
|
||||
}
|
||||
|
||||
public function testMissingMethod(): void
|
||||
{
|
||||
$result = $this->first($this->factory->create('{"jsonrpc": "2.0", "params": {}, "id": 1}'));
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
|
||||
}
|
||||
|
||||
public function testBatchMissingMethod(): void
|
||||
{
|
||||
$results = $this->factory->create('[{"jsonrpc": "2.0", "params": {}, "id": 1}, {"jsonrpc": "2.0", "method": "notifications/test_event", "params": {}, "id": 2}]');
|
||||
|
||||
$results = iterator_to_array($results);
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
|
||||
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(Notification::class, $result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Message\Response;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Response::class)]
|
||||
final class ResponseTest extends TestCase
|
||||
{
|
||||
public function testWithIntegerId(): void
|
||||
{
|
||||
$response = new Response(1, ['foo' => 'bar']);
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'result' => ['foo' => 'bar'],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $response->jsonSerialize());
|
||||
}
|
||||
|
||||
public function testWithStringId(): void
|
||||
{
|
||||
$response = new Response('abc', ['foo' => 'bar']);
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'abc',
|
||||
'result' => ['foo' => 'bar'],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $response->jsonSerialize());
|
||||
}
|
||||
}
|
||||
56
tests/Schema/JsonRpc/NotificationTest.php
Normal file
56
tests/Schema/JsonRpc/NotificationTest.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?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\Schema\JsonRpc;
|
||||
|
||||
use Mcp\Schema\JsonRpc\Notification;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class NotificationTest extends TestCase
|
||||
{
|
||||
public function testMetaIsLoopedThrough()
|
||||
{
|
||||
$notificationImplementation = new class extends Notification {
|
||||
public static function getMethod(): string
|
||||
{
|
||||
return 'notifications/dummy';
|
||||
}
|
||||
|
||||
public static function fromParams(?array $params): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
protected function getParams(): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
$notification = $notificationImplementation::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'notifications/dummy',
|
||||
'params' => [
|
||||
'_meta' => ['key' => 'value'],
|
||||
],
|
||||
]);
|
||||
|
||||
$expectedMeta = [
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'notifications/dummy',
|
||||
'params' => [
|
||||
'_meta' => ['key' => 'value'],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expectedMeta, $notification->jsonSerialize());
|
||||
}
|
||||
}
|
||||
58
tests/Schema/JsonRpc/RequestTest.php
Normal file
58
tests/Schema/JsonRpc/RequestTest.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?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\Schema\JsonRpc;
|
||||
|
||||
use Mcp\Schema\JsonRpc\Request;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RequestTest extends TestCase
|
||||
{
|
||||
public function testMetaAndIdAreLoopedThrough()
|
||||
{
|
||||
$requestImplementation = new class extends Request {
|
||||
public static function getMethod(): string
|
||||
{
|
||||
return 'foo/bar';
|
||||
}
|
||||
|
||||
public static function fromParams(?array $params): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
protected function getParams(): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
$notification = $requestImplementation::fromArray([
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => '12345',
|
||||
'method' => 'foo/bar',
|
||||
'params' => [
|
||||
'_meta' => ['key' => 'value'],
|
||||
],
|
||||
]);
|
||||
|
||||
$expectedMeta = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => '12345',
|
||||
'method' => 'foo/bar',
|
||||
'params' => [
|
||||
'_meta' => ['key' => 'value'],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expectedMeta, $notification->jsonSerialize());
|
||||
}
|
||||
}
|
||||
|
|
@ -13,43 +13,50 @@ namespace Mcp\Tests\Server\RequestHandler;
|
|||
|
||||
use Mcp\Capability\Prompt\MetadataInterface;
|
||||
use Mcp\Capability\PromptChain;
|
||||
use Mcp\Message\Request;
|
||||
use Mcp\Server\RequestHandler\PromptListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use Mcp\Schema\Request\ListPromptsRequest;
|
||||
use Mcp\Schema\Result\ListPromptsResult;
|
||||
use Mcp\Server\RequestHandler\ListPromptsHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(PromptListHandler::class)]
|
||||
class PromptListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$handler = new PromptListHandler(new PromptChain([]));
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['prompts' => []], $response->result);
|
||||
$handler = new ListPromptsHandler(new PromptChain([]));
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertEquals(1, $response->getId());
|
||||
$this->assertEquals([], $response->result->prompts);
|
||||
}
|
||||
|
||||
public function testHandleReturnAll(): void
|
||||
public function testHandleReturnAll()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new PromptListHandler(new PromptChain([$item]));
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['prompts']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
$handler = new ListPromptsHandler(new PromptChain([$item]));
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->prompts);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
public function testHandlePagination(): void
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new PromptListHandler(new PromptChain([$item, $item]), 2);
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['prompts']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
$handler = new ListPromptsHandler(new PromptChain([$item, $item]), 2);
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->prompts);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
|
|
|
|||
|
|
@ -13,18 +13,16 @@ namespace Mcp\Tests\Server\RequestHandler;
|
|||
|
||||
use Mcp\Capability\Resource\CollectionInterface;
|
||||
use Mcp\Capability\Resource\MetadataInterface;
|
||||
use Mcp\Message\Request;
|
||||
use Mcp\Server\RequestHandler\ResourceListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Mcp\Schema\Request\ListResourcesRequest;
|
||||
use Mcp\Schema\Result\ListResourcesResult;
|
||||
use Mcp\Server\RequestHandler\ListResourcesHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(ResourceListHandler::class)]
|
||||
class ResourceListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
|
|
@ -32,29 +30,36 @@ class ResourceListHandlerTest extends TestCase
|
|||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ResourceListHandler($collection);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$handler = new ListResourcesHandler($collection);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['resources' => []], $response->result);
|
||||
$this->assertEquals([], $response->result->resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList): void
|
||||
public function testHandleReturnAll(iterable $metadataList)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$handler = new ResourceListHandler($collection);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['resources']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
|
||||
$handler = new ListResourcesHandler($collection);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->resources);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -70,7 +75,7 @@ class ResourceListHandlerTest extends TestCase
|
|||
];
|
||||
}
|
||||
|
||||
public function testHandlePagination(): void
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
|
|
@ -78,11 +83,15 @@ class ResourceListHandlerTest extends TestCase
|
|||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$handler = new ResourceListHandler($collection, 2);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['resources']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
|
||||
$handler = new ListResourcesHandler($collection, 2);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->resources);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
|
|
@ -95,7 +104,7 @@ class ResourceListHandlerTest extends TestCase
|
|||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'src/SomeFile.php';
|
||||
return 'SomeFile';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
|
|
|
|||
|
|
@ -13,19 +13,16 @@ namespace Mcp\Tests\Server\RequestHandler;
|
|||
|
||||
use Mcp\Capability\Tool\CollectionInterface;
|
||||
use Mcp\Capability\Tool\MetadataInterface;
|
||||
use Mcp\Message\Request;
|
||||
use Mcp\Server\RequestHandler\ToolListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Mcp\Schema\Request\ListToolsRequest;
|
||||
use Mcp\Schema\Result\ListToolsResult;
|
||||
use Mcp\Server\RequestHandler\ListToolsHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(ToolListHandler::class)]
|
||||
class ToolListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
|
|
@ -33,29 +30,35 @@ class ToolListHandlerTest extends TestCase
|
|||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ToolListHandler($collection);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['tools' => []], $response->result);
|
||||
$handler = new ListToolsHandler($collection);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertSame(1, $response->id);
|
||||
$this->assertSame([], $response->result->tools);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList): void
|
||||
public function testHandleReturnAll(iterable $metadataList)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$handler = new ToolListHandler($collection);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['tools']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
$handler = new ListToolsHandler($collection);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->tools);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -71,8 +74,7 @@ class ToolListHandlerTest extends TestCase
|
|||
];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handlePagination(): void
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
|
|
@ -80,11 +82,14 @@ class ToolListHandlerTest extends TestCase
|
|||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$handler = new ToolListHandler($collection, 2);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['tools']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
$handler = new ListToolsHandler($collection, 2);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->tools);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
|
|
|
|||
|
|
@ -11,20 +11,16 @@
|
|||
|
||||
namespace Mcp\Tests;
|
||||
|
||||
use Mcp\JsonRpc\Handler;
|
||||
use Mcp\Server;
|
||||
use Mcp\Server\JsonRpcHandler;
|
||||
use Mcp\Tests\Fixtures\InMemoryTransport;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use Mcp\Server\Transport\InMemoryTransport;
|
||||
use PHPUnit\Framework\MockObject\Stub\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Server::class)]
|
||||
class ServerTest extends TestCase
|
||||
{
|
||||
public function testJsonExceptions(): void
|
||||
public function testJsonExceptions()
|
||||
{
|
||||
$logger = $this->getMockBuilder(NullLogger::class)
|
||||
->disableOriginalConstructor()
|
||||
|
|
@ -32,7 +28,7 @@ class ServerTest extends TestCase
|
|||
->getMock();
|
||||
$logger->expects($this->once())->method('error');
|
||||
|
||||
$handler = $this->getMockBuilder(JsonRpcHandler::class)
|
||||
$handler = $this->getMockBuilder(Handler::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['process'])
|
||||
->getMock();
|
||||
|
|
|
|||
Loading…
Reference in a new issue