Skip to content

Commit 89fd592

Browse files
author
klapaudius
committed
Add MCPProtocolTest and enhance test structure
Introduce unit tests for MCPProtocol to verify transport behavior. Update .gitignore for PHPUnit coverage and improve SseControllerTest mocks and comments for clarity.
1 parent a52961a commit 89fd592

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ phpstan.neon
3030
testbench.yaml
3131
/docs
3232
/coverage
33+
/phpunit.coverage.xml

tests/Controllers/SseControllerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use KLP\KlpMcpServer\Controllers\SseController;
66
use KLP\KlpMcpServer\Server\MCPServerInterface;
7+
use PHPUnit\Framework\Attributes\Small;
8+
use PHPUnit\Framework\MockObject\MockObject;
79
use PHPUnit\Framework\TestCase;
810
use Symfony\Component\HttpFoundation\Request;
911
use Symfony\Component\HttpFoundation\StreamedResponse;
1012

13+
#[Small]
1114
class SseControllerTest extends TestCase
1215
{
1316
private SseController $controller;
1417

15-
private MCPServerInterface $mockServer;
18+
private MCPServerInterface|MockObject $mockServer;
1619

1720
protected function setUp(): void
1821
{
@@ -44,7 +47,7 @@ public function test_handle_executes_server_connect(): void
4447

4548
$response = $this->controller->handle($request);
4649

47-
// Ensure response stream executes the connect callback
50+
// Ensure the response stream executes the connect callback
4851
ob_start();
4952
$response->sendContent();
5053
ob_end_clean();

tests/Protocol/MCPProtocolTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace KLP\KlpMcpServer\Tests\Protocol;
4+
5+
use Exception;
6+
use KLP\KlpMcpServer\Protocol\MCPProtocol;
7+
use KLP\KlpMcpServer\Transports\TransportInterface;
8+
use PHPUnit\Framework\Attributes\Small;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
12+
#[Small]
13+
class MCPProtocolTest extends TestCase
14+
{
15+
private TransportInterface|MockObject $mockTransport;
16+
private MCPProtocol $mcpProtocol;
17+
18+
protected function setUp(): void
19+
{
20+
$this->mockTransport = $this->createMock(TransportInterface::class);
21+
$this->mcpProtocol = new MCPProtocol($this->mockTransport);
22+
}
23+
24+
/**
25+
* Test that connect method starts the transport
26+
*/
27+
public function testConnectStartsTransport(): void
28+
{
29+
$this->mockTransport
30+
->expects($this->once())
31+
->method('start');
32+
33+
$this->mockTransport
34+
->expects($this->once())
35+
->method('isConnected')
36+
->willReturn(false);
37+
38+
$this->mockTransport
39+
->expects($this->once())
40+
->method('close');
41+
42+
$this->mcpProtocol->connect();
43+
}
44+
45+
/**
46+
* Test connect processes messages when transport is connected
47+
*/
48+
public function testConnectProcessesMessages(): void
49+
{
50+
$messages = ['message1', 'message2'];
51+
52+
$this->mockTransport
53+
->expects($this->once())
54+
->method('start');
55+
56+
$this->mockTransport
57+
->method('isConnected')
58+
->willReturn(true, false);
59+
60+
$this->mockTransport
61+
->method('receive')
62+
->willReturn($messages);
63+
64+
$this->mockTransport
65+
->expects($matcher = $this->exactly(count($messages)))
66+
->method('send')
67+
->with($this->callback(function ($message) use ($messages, $matcher) {
68+
$this->assertEquals($messages[$matcher->numberOfInvocations()-1], $message);
69+
return true;
70+
}));
71+
72+
$this->mockTransport
73+
->expects($this->once())
74+
->method('close');
75+
76+
$this->mcpProtocol->connect();
77+
}
78+
79+
/**
80+
* Test that connect method ignores null messages without sending
81+
*/
82+
public function testConnectIgnoresNullMessages(): void
83+
{
84+
$messages = [null, 'validMessage', null];
85+
86+
$this->mockTransport
87+
->expects($this->once())
88+
->method('start');
89+
90+
$this->mockTransport
91+
->method('isConnected')
92+
->willReturn(true, false);
93+
94+
$this->mockTransport
95+
->method('receive')
96+
->willReturn($messages);
97+
98+
$this->mockTransport
99+
->expects($this->once())
100+
->method('send')
101+
->with('validMessage');
102+
103+
$this->mockTransport
104+
->expects($this->once())
105+
->method('close');
106+
107+
$this->mcpProtocol->connect();
108+
}
109+
110+
/**
111+
* Test that connect sends messages using the transport's send method
112+
*/
113+
public function testConnectSendsMessages(): void
114+
{
115+
$messages = ['test_message'];
116+
117+
$this->mockTransport
118+
->expects($this->once())
119+
->method('start');
120+
121+
$this->mockTransport
122+
->method('isConnected')
123+
->willReturn(true, false);
124+
125+
$this->mockTransport
126+
->method('receive')
127+
->willReturn($messages);
128+
129+
$this->mockTransport
130+
->expects($this->once())
131+
->method('send')
132+
->with($messages[0]);
133+
134+
$this->mockTransport
135+
->expects($this->once())
136+
->method('close');
137+
138+
$this->mcpProtocol->connect();
139+
}
140+
}

0 commit comments

Comments
 (0)