Skip to content

Commit dccf3c4

Browse files
author
klapaudius
committed
Refactor tests and improve coverage configuration.
Replaced `TransportInterface` with `SseTransportInterface` in `MCPProtocolTest` for better alignment. Added new test cases to validate `handleMessage` and `ToolRepository` behavior. Updated PHPUnit configuration to exclude `routes.php` from coverage reports.
1 parent 1430ae2 commit dccf3c4

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<include>
2828
<directory suffix=".php">./src</directory>
2929
</include>
30+
<exclude>
31+
<file>src/Resources/config/routes.php</file>
32+
</exclude>
3033
</source>
3134
<coverage>
3235
<report>

tests/Protocol/MCPProtocolTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use KLP\KlpMcpServer\Exceptions\ToolParamsValidatorException;
66
use KLP\KlpMcpServer\Protocol\Handlers\NotificationHandler;
77
use KLP\KlpMcpServer\Protocol\MCPProtocol;
8+
use KLP\KlpMcpServer\Transports\SseTransportInterface;
89
use KLP\KlpMcpServer\Transports\TransportInterface;
910
use PHPUnit\Framework\Attributes\Small;
1011
use PHPUnit\Framework\MockObject\MockObject;
@@ -13,13 +14,13 @@
1314
#[Small]
1415
class MCPProtocolTest extends TestCase
1516
{
16-
private TransportInterface|MockObject $mockTransport;
17+
private SseTransportInterface|MockObject $mockTransport;
1718

1819
private MCPProtocol $mcpProtocol;
1920

2021
protected function setUp(): void
2122
{
22-
$this->mockTransport = $this->createMock(TransportInterface::class);
23+
$this->mockTransport = $this->createMock(SseTransportInterface::class);
2324
$this->mcpProtocol = new MCPProtocol($this->mockTransport);
2425
}
2526

@@ -287,6 +288,19 @@ public function test_handle_message_handles_no_method(): void
287288
$this->mcpProtocol->handleMessage($clientId, $noMethodMessage);
288289
}
289290

291+
public function test_handle_message_handles_ping_request(): void
292+
{
293+
$clientId = 'client_1';
294+
// response to a ping request from client
295+
$noMethodMessage = ['jsonrpc' => '2.0', 'id' => 1, 'result' => []];
296+
297+
$this->mockTransport
298+
->expects($this->never())
299+
->method('pushMessage');
300+
301+
$this->mcpProtocol->handleMessage($clientId, $noMethodMessage);
302+
}
303+
290304
public function test_handle_message_handles_invalid_params(): void
291305
{
292306
$clientId = 'client_1';

tests/Services/ToolService/ToolRepositoryTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use InvalidArgumentException;
66
use KLP\KlpMcpServer\Services\ToolService\ToolInterface;
77
use KLP\KlpMcpServer\Services\ToolService\ToolRepository;
8+
use PHPUnit\Framework\Attributes\Small;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use stdClass;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
1213
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1314
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1415

16+
#[Small]
1517
class ToolRepositoryTest extends TestCase
1618
{
1719
private ContainerInterface|MockObject $container;
@@ -47,6 +49,48 @@ public function test_register_many_with_valid_tool_instances(): void
4749
$this->assertSame($tool2, $tools['tool2']);
4850
}
4951

52+
/**
53+
* Tests the registration of multiple valid tool instances in the tool repository.
54+
*
55+
* Validates that the tool repository correctly registers multiple tool instances
56+
* and ensures that the tools can be retrieved and matched against their original instances.
57+
*/
58+
public function test_register_many_is_called_on_constructor(): void
59+
{
60+
$tool1 = $this->createMock(ToolInterface::class);
61+
$tool2 = $this->createMock(ToolInterface::class);
62+
63+
$tool1->method('getName')->willReturn('tool1');
64+
$tool2->method('getName')->willReturn('tool2');
65+
$invocations = [ 'tool1', 'tool2' ];
66+
$this->container
67+
->expects($matcher = $this->exactly(count($invocations)))
68+
->method('get')
69+
->with($this->callback(function ($toolClass) use ($invocations, $matcher) {
70+
$this->assertEquals($invocations[$matcher->numberOfInvocations() - 1], $toolClass);
71+
return true;
72+
}))
73+
->willReturnOnConsecutiveCalls(
74+
$tool1,
75+
$tool2
76+
);
77+
$this->container
78+
->expects($this->once())
79+
->method('getParameter')
80+
->with('klp_mcp_server.tools')
81+
->willReturn([ 'tool1', 'tool2' ]);
82+
83+
$toolRepository = new ToolRepository($this->container);
84+
85+
$tools = $toolRepository->getTools();
86+
87+
$this->assertCount(2, $tools);
88+
$this->assertSame($tool1, $tools['tool1']);
89+
$this->assertSame($tool2, $tools['tool2']);
90+
$this->assertSame($tool1, $toolRepository->getTool('tool1'));
91+
92+
}
93+
5094
/**
5195
* Tests the registration of multiple tools using valid tool class names.
5296
*

0 commit comments

Comments
 (0)