|
| 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