Adopt server examples
This commit is contained in:
parent
da0d5a87a0
commit
2ef051a355
9 changed files with 3639 additions and 756 deletions
|
|
@ -15,10 +15,10 @@ use Mcp\Capability\Discovery\Discoverer;
|
|||
use Mcp\Capability\Prompt\Completion\EnumCompletionProvider;
|
||||
use Mcp\Capability\Prompt\Completion\ListCompletionProvider;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Capability\Registry\RegisteredPrompt;
|
||||
use Mcp\Capability\Registry\RegisteredResource;
|
||||
use Mcp\Capability\Registry\RegisteredResourceTemplate;
|
||||
use Mcp\Capability\Registry\RegisteredTool;
|
||||
use Mcp\Capability\Registry\PromptReference;
|
||||
use Mcp\Capability\Registry\ResourceReference;
|
||||
use Mcp\Capability\Registry\ResourceTemplateReference;
|
||||
use Mcp\Capability\Registry\ToolReference;
|
||||
use Mcp\Tests\Capability\Attribute\CompletionProviderFixture;
|
||||
use Mcp\Tests\Capability\Discovery\Fixtures\DiscoverableToolHandler;
|
||||
use Mcp\Tests\Capability\Discovery\Fixtures\InvocablePromptFixture;
|
||||
|
|
@ -46,7 +46,7 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertCount(4, $tools);
|
||||
|
||||
$greetUserTool = $this->registry->getTool('greet_user');
|
||||
$this->assertInstanceOf(RegisteredTool::class, $greetUserTool);
|
||||
$this->assertInstanceOf(ToolReference::class, $greetUserTool);
|
||||
$this->assertFalse($greetUserTool->isManual);
|
||||
$this->assertEquals('greet_user', $greetUserTool->tool->name);
|
||||
$this->assertEquals('Greets a user by name.', $greetUserTool->tool->description);
|
||||
|
|
@ -54,13 +54,13 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertArrayHasKey('name', $greetUserTool->tool->inputSchema['properties'] ?? []);
|
||||
|
||||
$repeatActionTool = $this->registry->getTool('repeatAction');
|
||||
$this->assertInstanceOf(RegisteredTool::class, $repeatActionTool);
|
||||
$this->assertInstanceOf(ToolReference::class, $repeatActionTool);
|
||||
$this->assertEquals('A tool with more complex parameters and inferred name/description.', $repeatActionTool->tool->description);
|
||||
$this->assertTrue($repeatActionTool->tool->annotations->readOnlyHint);
|
||||
$this->assertEquals(['count', 'loudly', 'mode'], array_keys($repeatActionTool->tool->inputSchema['properties'] ?? []));
|
||||
|
||||
$invokableCalcTool = $this->registry->getTool('InvokableCalculator');
|
||||
$this->assertInstanceOf(RegisteredTool::class, $invokableCalcTool);
|
||||
$this->assertInstanceOf(ToolReference::class, $invokableCalcTool);
|
||||
$this->assertFalse($invokableCalcTool->isManual);
|
||||
$this->assertEquals([InvocableToolFixture::class, '__invoke'], $invokableCalcTool->handler);
|
||||
|
||||
|
|
@ -72,13 +72,13 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertCount(3, $resources);
|
||||
|
||||
$appVersionRes = $this->registry->getResource('app://info/version');
|
||||
$this->assertInstanceOf(RegisteredResource::class, $appVersionRes);
|
||||
$this->assertInstanceOf(ResourceReference::class, $appVersionRes);
|
||||
$this->assertFalse($appVersionRes->isManual);
|
||||
$this->assertEquals('app_version', $appVersionRes->schema->name);
|
||||
$this->assertEquals('text/plain', $appVersionRes->schema->mimeType);
|
||||
|
||||
$invokableStatusRes = $this->registry->getResource('invokable://config/status');
|
||||
$this->assertInstanceOf(RegisteredResource::class, $invokableStatusRes);
|
||||
$this->assertInstanceOf(ResourceReference::class, $invokableStatusRes);
|
||||
$this->assertFalse($invokableStatusRes->isManual);
|
||||
$this->assertEquals([InvocableResourceFixture::class, '__invoke'], $invokableStatusRes->handler);
|
||||
|
||||
|
|
@ -86,22 +86,22 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertCount(4, $prompts);
|
||||
|
||||
$storyPrompt = $this->registry->getPrompt('creative_story_prompt');
|
||||
$this->assertInstanceOf(RegisteredPrompt::class, $storyPrompt);
|
||||
$this->assertInstanceOf(PromptReference::class, $storyPrompt);
|
||||
$this->assertFalse($storyPrompt->isManual);
|
||||
$this->assertCount(2, $storyPrompt->prompt->arguments);
|
||||
$this->assertEquals(CompletionProviderFixture::class, $storyPrompt->completionProviders['genre']);
|
||||
|
||||
$simplePrompt = $this->registry->getPrompt('simpleQuestionPrompt');
|
||||
$this->assertInstanceOf(RegisteredPrompt::class, $simplePrompt);
|
||||
$this->assertInstanceOf(PromptReference::class, $simplePrompt);
|
||||
$this->assertFalse($simplePrompt->isManual);
|
||||
|
||||
$invokableGreeter = $this->registry->getPrompt('InvokableGreeterPrompt');
|
||||
$this->assertInstanceOf(RegisteredPrompt::class, $invokableGreeter);
|
||||
$this->assertInstanceOf(PromptReference::class, $invokableGreeter);
|
||||
$this->assertFalse($invokableGreeter->isManual);
|
||||
$this->assertEquals([InvocablePromptFixture::class, '__invoke'], $invokableGreeter->handler);
|
||||
|
||||
$contentCreatorPrompt = $this->registry->getPrompt('content_creator');
|
||||
$this->assertInstanceOf(RegisteredPrompt::class, $contentCreatorPrompt);
|
||||
$this->assertInstanceOf(PromptReference::class, $contentCreatorPrompt);
|
||||
$this->assertFalse($contentCreatorPrompt->isManual);
|
||||
$this->assertCount(3, $contentCreatorPrompt->completionProviders);
|
||||
|
||||
|
|
@ -109,14 +109,14 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertCount(4, $templates);
|
||||
|
||||
$productTemplate = $this->registry->getResourceTemplate('product://{region}/details/{productId}');
|
||||
$this->assertInstanceOf(RegisteredResourceTemplate::class, $productTemplate);
|
||||
$this->assertInstanceOf(ResourceTemplateReference::class, $productTemplate);
|
||||
$this->assertFalse($productTemplate->isManual);
|
||||
$this->assertEquals('product_details_template', $productTemplate->resourceTemplate->name);
|
||||
$this->assertEquals(CompletionProviderFixture::class, $productTemplate->completionProviders['region']);
|
||||
$this->assertEqualsCanonicalizing(['region', 'productId'], $productTemplate->getVariableNames());
|
||||
|
||||
$invokableUserTemplate = $this->registry->getResourceTemplate('invokable://user-profile/{userId}');
|
||||
$this->assertInstanceOf(RegisteredResourceTemplate::class, $invokableUserTemplate);
|
||||
$this->assertInstanceOf(ResourceTemplateReference::class, $invokableUserTemplate);
|
||||
$this->assertFalse($invokableUserTemplate->isManual);
|
||||
$this->assertEquals([InvocableResourceTemplateFixture::class, '__invoke'], $invokableUserTemplate->handler);
|
||||
}
|
||||
|
|
@ -124,7 +124,7 @@ class DiscoveryTest extends TestCase
|
|||
public function testDoesNotDiscoverElementsFromExcludedDirectories()
|
||||
{
|
||||
$this->discoverer->discover(__DIR__, ['Fixtures']);
|
||||
$this->assertInstanceOf(RegisteredTool::class, $this->registry->getTool('hidden_subdir_tool'));
|
||||
$this->assertInstanceOf(ToolReference::class, $this->registry->getTool('hidden_subdir_tool'));
|
||||
|
||||
$this->registry->clear();
|
||||
|
||||
|
|
@ -160,7 +160,7 @@ class DiscoveryTest extends TestCase
|
|||
$this->discoverer->discover(__DIR__, ['Fixtures']);
|
||||
|
||||
$contentPrompt = $this->registry->getPrompt('content_creator');
|
||||
$this->assertInstanceOf(RegisteredPrompt::class, $contentPrompt);
|
||||
$this->assertInstanceOf(PromptReference::class, $contentPrompt);
|
||||
$this->assertCount(3, $contentPrompt->completionProviders);
|
||||
|
||||
$typeProvider = $contentPrompt->completionProviders['type'];
|
||||
|
|
@ -173,7 +173,7 @@ class DiscoveryTest extends TestCase
|
|||
$this->assertInstanceOf(EnumCompletionProvider::class, $priorityProvider);
|
||||
|
||||
$contentTemplate = $this->registry->getResourceTemplate('content://{category}/{slug}');
|
||||
$this->assertInstanceOf(RegisteredResourceTemplate::class, $contentTemplate);
|
||||
$this->assertInstanceOf(ResourceTemplateReference::class, $contentTemplate);
|
||||
$this->assertCount(1, $contentTemplate->completionProviders);
|
||||
|
||||
$categoryProvider = $contentTemplate->completionProviders['category'];
|
||||
|
|
|
|||
68
tests/Example/InspectorSnapshotTestCase.php
Normal file
68
tests/Example/InspectorSnapshotTestCase.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?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\Example;
|
||||
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
abstract class InspectorSnapshotTestCase extends TestCase
|
||||
{
|
||||
#[DataProvider('provideMethods')]
|
||||
public function testResourcesListOutputMatchesSnapshot(string $method): void
|
||||
{
|
||||
$process = Process::fromShellCommandline(
|
||||
\sprintf('npx @modelcontextprotocol/inspector --cli php %s --method %s', $this->getServerScript(), $method)
|
||||
)->mustRun();
|
||||
|
||||
$output = $process->getOutput();
|
||||
$snapshotFile = $this->getSnapshotFilePath($method);
|
||||
|
||||
if (!file_exists($snapshotFile)) {
|
||||
file_put_contents($snapshotFile, $output.\PHP_EOL);
|
||||
$this->markTestIncomplete("Snapshot created at $snapshotFile, please re-run tests.");
|
||||
}
|
||||
|
||||
$expected = file_get_contents($snapshotFile);
|
||||
|
||||
$this->assertJsonStringEqualsJsonString($expected, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* List of methods to test.
|
||||
*
|
||||
* @return array<string, array{method: string}>
|
||||
*/
|
||||
abstract public static function provideMethods(): array;
|
||||
|
||||
abstract protected function getServerScript(): string;
|
||||
|
||||
/**
|
||||
* @return array<string, array{method: string}>
|
||||
*/
|
||||
protected static function provideListMethods(): array
|
||||
{
|
||||
return [
|
||||
'Prompt Listing' => ['method' => 'prompts/list'],
|
||||
'Resource Listing' => ['method' => 'resources/list'],
|
||||
// 'Resource Template Listing' => ['method' => 'resources/templates/list'],
|
||||
'Tool Listing' => ['method' => 'tools/list'],
|
||||
];
|
||||
}
|
||||
|
||||
private function getSnapshotFilePath(string $method): string
|
||||
{
|
||||
$className = substr(static::class, strrpos(static::class, '\\') + 1);
|
||||
|
||||
return __DIR__.'/snapshots/'.$className.'-'.str_replace('/', '_', $method).'.json';
|
||||
}
|
||||
}
|
||||
30
tests/Example/StdioCalculatorExampleTest.php
Normal file
30
tests/Example/StdioCalculatorExampleTest.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?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\Example;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
|
||||
#[Group('inspector')]
|
||||
final class StdioCalculatorExampleTest extends InspectorSnapshotTestCase
|
||||
{
|
||||
public static function provideMethods(): array
|
||||
{
|
||||
return [
|
||||
...parent::provideListMethods(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getServerScript(): string
|
||||
{
|
||||
return \dirname(__DIR__, 2).'/examples/01-discovery-stdio-calculator/server.php';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"prompts": []
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"resources": [
|
||||
{
|
||||
"name": "calculator_config",
|
||||
"uri": "config://calculator/settings",
|
||||
"description": "Current settings for the calculator tool (precision, allow_negative).",
|
||||
"mimeType": "application/json"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
{
|
||||
"tools": [
|
||||
{
|
||||
"name": "calculate",
|
||||
"description": "Performs a calculation based on the operation.",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "number",
|
||||
"description": "the first operand"
|
||||
},
|
||||
"b": {
|
||||
"type": "number",
|
||||
"description": "the second operand"
|
||||
},
|
||||
"operation": {
|
||||
"type": "string",
|
||||
"description": "the operation ('add', 'subtract', 'multiply', 'divide')"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a",
|
||||
"b",
|
||||
"operation"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "update_setting",
|
||||
"description": "Updates a specific configuration setting.",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"setting": {
|
||||
"type": "string",
|
||||
"description": "the setting key ('precision' or 'allow_negative')"
|
||||
},
|
||||
"value": {
|
||||
"description": "the new value (int for precision, bool for allow_negative)"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"setting",
|
||||
"value"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -1,87 +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\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Prompt\MetadataInterface;
|
||||
use Mcp\Capability\PromptChain;
|
||||
use Mcp\Schema\Request\ListPromptsRequest;
|
||||
use Mcp\Schema\Result\ListPromptsResult;
|
||||
use Mcp\Server\RequestHandler\ListPromptsHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PromptListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$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()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$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()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$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
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getName(): string
|
||||
{
|
||||
return 'greet';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Greet a person with a nice message';
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'name' => 'first name',
|
||||
'description' => 'The name of the person to greet',
|
||||
'required' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,126 +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\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Resource\CollectionInterface;
|
||||
use Mcp\Capability\Resource\MetadataInterface;
|
||||
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\TestCase;
|
||||
|
||||
class ResourceListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$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([], $response->result->resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, iterable<MetadataInterface>>
|
||||
*/
|
||||
public static function metadataProvider(): array
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
|
||||
return [
|
||||
'array' => [[$item]],
|
||||
'generator' => [(function () use ($item) { yield $item; })()],
|
||||
];
|
||||
}
|
||||
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
|
||||
$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
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getUri(): string
|
||||
{
|
||||
return 'file:///src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'SomeFile';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'File src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return 'text/plain';
|
||||
}
|
||||
|
||||
public function getSize(): int
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,114 +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\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Tool\CollectionInterface;
|
||||
use Mcp\Capability\Tool\MetadataInterface;
|
||||
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\TestCase;
|
||||
|
||||
class ToolListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$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)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, iterable<MetadataInterface>>
|
||||
*/
|
||||
public static function metadataProvider(): array
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
|
||||
return [
|
||||
'array' => [[$item]],
|
||||
'generator' => [(function () use ($item) { yield $item; })()],
|
||||
];
|
||||
}
|
||||
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$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
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getName(): string
|
||||
{
|
||||
return 'test_tool';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'A test tool';
|
||||
}
|
||||
|
||||
public function getInputSchema(): array
|
||||
{
|
||||
return ['type' => 'object'];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue