forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceUnsubscribeTest.php
More file actions
157 lines (133 loc) · 5.86 KB
/
Copy pathResourceUnsubscribeTest.php
File metadata and controls
157 lines (133 loc) · 5.86 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
151
152
153
154
155
156
157
<?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\ResourceUnsubscribeRequest;
use Mcp\Schema\Resource;
use Mcp\Schema\Result\EmptyResult;
use Mcp\Server\Handler\Request\ResourceUnsubscribeHandler;
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 ResourceUnsubscribeTest extends TestCase
{
private ResourceUnsubscribeHandler $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 ResourceUnsubscribeHandler($this->registry, $this->subscriptionManager, $this->logger);
}
#[TestDox('Client can unsubscribe from a resource')]
public function testClientCanUnsubscribeFromAResource(): void
{
// Arrange
$uri = 'file://documents/readme.txt';
$request = $this->createResourceUnsubscribeRequest($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('unsubscribe')
->with($this->session, $uri);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($request->getId(), $response->id);
$this->assertInstanceOf(EmptyResult::class, $response->result);
}
#[TestDox('Gracefully handle duplicate unsubscription from a resource')]
public function testDuplicateUnSubscriptionIsGracefullyHandled(): void
{
// Arrange
$uri = 'file://documents/readme.txt';
$request = $this->createResourceUnsubscribeRequest($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('unsubscribe')
->with($this->session, $uri);
// Act
$response1 = $this->handler->handle($request, $this->session);
$response2 = $this->handler->handle($request, $this->session);
// Assert
$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('Unsubscription from a resource with an invalid uri throws ResourceNotException')]
public function testHandleUnsubscribeResourceNotFoundException(): void
{
$uri = 'file://missing/file.txt';
$request = $this->createResourceUnsubscribeRequest($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);
}
#[TestDox('Unsubscription from a resource with an empty uri throws InvalidArgumentException')]
public function testUnsubscribeWithEmptyUriThrowsError(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Missing or invalid "uri" parameter for resources/unsubscribe.');
$this->createResourceUnsubscribeRequest('');
}
private function createResourceUnsubscribeRequest(string $uri): ResourceUnsubscribeRequest
{
return ResourceUnsubscribeRequest::fromArray([
'jsonrpc' => '2.0',
'method' => ResourceUnsubscribeRequest::getMethod(),
'id' => 'test-request-'.uniqid(),
'params' => [
'uri' => $uri,
],
]);
}
}