Skip to content

Commit f1e9cbc

Browse files
committed
Test Registry
1 parent e104db0 commit f1e9cbc

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/JsonRpc/MessageFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function createByType(string $messageType, array $data): HasMethodInterfa
123123
if (\in_array($messageType, $this->registeredMessages, true)) {
124124
$data['jsonrpc'] = MessageInterface::JSONRPC_VERSION;
125125
$data['method'] = $messageType::getMethod();
126+
126127
return $messageType::fromArray($data);
127128
}
128129

tests/Capability/RegistryTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the official PHP MCP SDK.
7+
*
8+
* A collaboration between Symfony and the PHP Foundation.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Mcp\Tests\Capability;
15+
16+
use Mcp\Capability\Registry;
17+
use Mcp\Schema\Notification\PromptListChangedNotification;
18+
use Mcp\Schema\Notification\ResourceListChangedNotification;
19+
use Mcp\Schema\Notification\ToolListChangedNotification;
20+
use Mcp\Schema\Prompt;
21+
use Mcp\Schema\Resource;
22+
use Mcp\Schema\Tool;
23+
use Mcp\Server\NotificationPublisher;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class RegistryTest extends TestCase
27+
{
28+
public function testToolRegistration(): void
29+
{
30+
$tool = new Tool(
31+
'the-best-tool-name-ever',
32+
[
33+
'type' => 'object',
34+
'properties' => [
35+
'param1' => ['type' => 'string'],
36+
],
37+
'required' => [
38+
'param1',
39+
],
40+
],
41+
null,
42+
null
43+
);
44+
45+
$expected = [$tool->name => $tool];
46+
47+
$notificationPublisher = $this->createMock(NotificationPublisher::class);
48+
$notificationPublisher->expects($this->once())
49+
->method('enqueue')
50+
->with(ToolListChangedNotification::class);
51+
52+
$registry = new Registry($notificationPublisher);
53+
$registry->registerTool($tool, fn () => null);
54+
55+
$this->assertSame($expected, $registry->getTools());
56+
}
57+
58+
public function testResourceRegistration(): void
59+
{
60+
$resource = new Resource(
61+
'config://the-best-resource-uri-ever',
62+
'the-best-resource-name-ever',
63+
);
64+
65+
$expected = [$resource->uri => $resource];
66+
67+
$notificationPublisher = $this->createMock(NotificationPublisher::class);
68+
$notificationPublisher->expects($this->once())
69+
->method('enqueue')
70+
->with(ResourceListChangedNotification::class);
71+
72+
$registry = new Registry($notificationPublisher);
73+
$registry->registerResource($resource, fn () => null);
74+
75+
$this->assertSame($expected, $registry->getResources());
76+
}
77+
78+
public function testPromptRegistration(): void
79+
{
80+
$prompt = new Prompt(
81+
'the-best-prompt-ever',
82+
);
83+
84+
$expected = [$prompt->name => $prompt];
85+
86+
$notificationPublisher = $this->createMock(NotificationPublisher::class);
87+
$notificationPublisher->expects($this->once())
88+
->method('enqueue')
89+
->with(PromptListChangedNotification::class);
90+
91+
$registry = new Registry($notificationPublisher);
92+
$registry->registerPrompt($prompt, fn () => null);
93+
94+
$this->assertSame($expected, $registry->getPrompts());
95+
}
96+
}

0 commit comments

Comments
 (0)