forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceSubscribeTest.php
More file actions
150 lines (127 loc) · 5.73 KB
/
Copy pathResourceSubscribeTest.php
File metadata and controls
150 lines (127 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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\Capability\Registry\ResourceReference;
use Mcp\Capability\RegistryInterface;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Exception\ResourceNotFoundException;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\Request\ResourceSubscribeRequest;
use Mcp\Schema\Resource;
use Mcp\Schema\Result\EmptyResult;
use Mcp\Server\Handler\Request\ResourceSubscribeHandler;
use Mcp\Server\Resource\SubscriptionManagerInterface;
use Mcp\Server\Session\SessionInterface;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
class ResourceSubscribeTest extends TestCase
{
private ResourceSubscribeHandler $handler;
private RegistryInterface&MockObject $registry;
private SessionInterface&MockObject $session;
private SubscriptionManagerInterface&MockObject $subscriptionManager;
private LoggerInterface&MockObject $logger;
protected function setUp(): void
{
$this->registry = $this->createMock(RegistryInterface::class);
$this->subscriptionManager = $this->createMock(SubscriptionManagerInterface::class);
$this->session = $this->createMock(SessionInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->handler = new ResourceSubscribeHandler($this->registry, $this->subscriptionManager, $this->logger);
}
#[TestDox('Client can successfully subscribe to a resource')]
public function testClientCanSuccessfulSubscribeToAResource(): void
{
$uri = 'file://documents/readme.txt';
$request = $this->createResourceSubscribeRequest($uri);
$resourceReference = $this->getMockBuilder(ResourceReference::class)
->setConstructorArgs([new Resource($uri, 'test', mimeType: 'text/plain'), []])
->getMock();
$this->registry
->expects($this->once())
->method('getResource')
->with($uri)
->willReturn($resourceReference);
$this->subscriptionManager->expects($this->once())
->method('subscribe')
->with($this->session, $uri);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($request->getId(), $response->id);
$this->assertInstanceOf(EmptyResult::class, $response->result);
}
#[TestDox('Gracefully handle duplicate subscription to a resource')]
public function testDuplicateSubscriptionIsGracefullyHandled(): void
{
$uri = 'file://documents/readme.txt';
$request = $this->createResourceSubscribeRequest($uri);
$resourceReference = $this->getMockBuilder(ResourceReference::class)
->setConstructorArgs([new Resource($uri, 'test', mimeType: 'text/plain'), []])
->getMock();
$this->registry
->expects($this->exactly(2))
->method('getResource')
->with($uri)
->willReturn($resourceReference);
$this->subscriptionManager
->expects($this->exactly(2))
->method('subscribe')
->with($this->session, $uri);
$response1 = $this->handler->handle($request, $this->session);
$response2 = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Response::class, $response1);
$this->assertInstanceOf(Response::class, $response2);
$this->assertEquals($request->getId(), $response1->id);
$this->assertEquals($request->getId(), $response2->id);
$this->assertInstanceOf(EmptyResult::class, $response1->result);
$this->assertInstanceOf(EmptyResult::class, $response2->result);
}
#[TestDox('Subscription to a resource with an empty uri throws InvalidArgumentException')]
public function testSubscribeWithEmptyUriThrowsError(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "uri" parameter for resources/subscribe.');
$this->createResourceSubscribeRequest('');
}
#[TestDox('Subscription to a resource with an invalid uri throws ResourceNotException')]
public function testHandleSubscribeResourceNotFoundException(): void
{
$uri = 'file://missing/file.txt';
$request = $this->createResourceSubscribeRequest($uri);
$exception = new ResourceNotFoundException($uri);
$this->registry
->expects($this->once())
->method('getResource')
->with($uri)
->willThrowException($exception);
$this->logger
->expects($this->once())
->method('error')
->with('Resource not found', ['uri' => $uri, 'exception' => $exception]);
$response = $this->handler->handle($request, $this->session);
$this->assertInstanceOf(Error::class, $response);
$this->assertEquals(Error::RESOURCE_NOT_FOUND, $response->code);
$this->assertEquals(\sprintf('Resource not found for uri: "%s".', $uri), $response->message);
}
private function createResourceSubscribeRequest(string $uri): ResourceSubscribeRequest
{
return ResourceSubscribeRequest::fromArray([
'jsonrpc' => '2.0',
'method' => ResourceSubscribeRequest::getMethod(),
'id' => 'test-request-'.uniqid(),
'params' => [
'uri' => $uri,
],
]);
}
}