Skip to content

Commit 9e1a313

Browse files
authored
Merge pull request #1 from trandbert37/feature/use-symfony-router
Use Symfony Router to generate endpoint
2 parents 7d0a7e3 + e1ed59f commit 9e1a313

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</service>
3737

3838
<service id="klp_mcp_server.transport.sse" class="KLP\KlpMcpServer\Transports\SseTransport">
39-
<argument type="string">%klp_mcp_server.default_path%</argument>
39+
<argument type="service" id="router" />
4040
<argument type="service" id="klp_mcp_server.adapter" on-invalid="null" />
4141
<argument type="service" id="logger" on-invalid="null" />
4242
<argument type="binary">%klp_mcp_server.ping.enabled%</argument>

src/Transports/SseTransport.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use KLP\KlpMcpServer\Transports\SseAdapters\SseAdapterException;
77
use KLP\KlpMcpServer\Transports\SseAdapters\SseAdapterInterface;
88
use Psr\Log\LoggerInterface;
9+
use Symfony\Component\Routing\RouterInterface;
910

1011
/**
1112
* SSE (Server-Sent Events) Transport implementation.
@@ -58,15 +59,15 @@ final class SseTransport implements SseTransportInterface
5859
/**
5960
* Initializes the class with the default path, adapter, logger, and ping settings.
6061
*
61-
* @param string $defaultPath The default path for resources.
62+
* @param RouterInterface $router The router instance (optional).
6263
* @param SseAdapterInterface|null $adapter Optional adapter for message persistence and retrieval (e.g., Redis).
6364
* Enables simulation of request/response patterns over SSE.
6465
* @param LoggerInterface|null $logger The logger instance (optional).
6566
* @param bool $pingEnabled Flag to enable or disable ping functionality.
6667
* @param int $pingInterval The interval, in secondes, at which ping messages are sent to maintain the connection.
6768
*/
6869
public function __construct(
69-
private readonly string $defaultPath,
70+
private readonly RouterInterface $router,
7071
private ?SseAdapterInterface $adapter = null,
7172
private readonly ?LoggerInterface $logger = null,
7273
private bool $pingEnabled = false,
@@ -330,10 +331,7 @@ public function pushMessage(string $clientId, array $message): void
330331

331332
private function getEndpoint(string $sessionId): string
332333
{
333-
return sprintf('/%s/messages?sessionId=%s',
334-
trim($this->defaultPath, '/'),
335-
$sessionId,
336-
);
334+
return $this->router->generate('message_route', ['sessionId' => $sessionId]);
337335
}
338336

339337
/**

tests/Transports/SseTransportTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPUnit\Framework\MockObject\MockObject;
1111
use PHPUnit\Framework\TestCase;
1212
use Psr\Log\LoggerInterface;
13+
use Symfony\Component\Routing\RouterInterface;
1314

1415
#[Small]
1516
class SseTransportTest extends TestCase
@@ -18,14 +19,17 @@ class SseTransportTest extends TestCase
1819

1920
private SseAdapterInterface|MockObject $adapterMock;
2021

22+
private RouterInterface|MockObject $routerMock;
23+
2124
private SseTransport $instance;
2225

2326
protected function setUp(): void
2427
{
2528
parent::setUp();
2629
$this->loggerMock = $this->createMock(LoggerInterface::class);
2730
$this->adapterMock = $this->createMock(SseAdapterInterface::class);
28-
$this->instance = new SseTransport('/default-path', $this->adapterMock, $this->loggerMock);
31+
$this->routerMock = $this->createMock(RouterInterface::class);
32+
$this->instance = new SseTransport($this->routerMock, $this->adapterMock, $this->loggerMock);
2933
}
3034

3135
/**
@@ -58,6 +62,17 @@ public function test_send_string_message(): void
5862
*/
5963
public function test_initialize_generates_client_id_and_sends_endpoint(): void
6064
{
65+
// Arrange
66+
$this->routerMock
67+
->expects($this->once())
68+
->method('generate')
69+
->willReturnCallback(function (string $name, array $parameters = []): string {
70+
$this->assertEquals('message_route', $name);
71+
$this->assertArrayHasKey('sessionId', $parameters);
72+
73+
return '/default-path/messages?sessionId='.$parameters['sessionId'];
74+
})
75+
;
6176
// Act
6277
ob_start();
6378
try {
@@ -84,6 +99,11 @@ public function test_initialize_does_not_overwrite_existing_client_id(): void
8499
// Arrange
85100
$existingClientId = 'predefined-client-id';
86101
$this->setProtectedProperty($this->instance, 'clientId', $existingClientId);
102+
$this->routerMock
103+
->expects($this->once())
104+
->method('generate')
105+
->with('message_route', ['sessionId' => $existingClientId])
106+
->willReturn('/default-path/messages?sessionId=' . $existingClientId);
87107

88108
// Act
89109
ob_start();

0 commit comments

Comments
 (0)