|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the official PHP MCP SDK. |
| 7 | + * |
| 8 | + * A collaboration between Symfony and the PHP Foundation. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Mcp\Tests\Application; |
| 15 | + |
| 16 | +use Mcp\Schema\JsonRpc\MessageInterface; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\Component\Process\Process; |
| 19 | + |
| 20 | +abstract class ApplicationTestCase extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param list<string> $messages |
| 24 | + * @param array<string,string> $env |
| 25 | + * |
| 26 | + * @return array<string, array<string, array<string,mixed>>> |
| 27 | + */ |
| 28 | + protected function runServer(array $messages, float $timeout = 5.0, array $env = []): array |
| 29 | + { |
| 30 | + if (0 === \count($messages)) { |
| 31 | + return []; |
| 32 | + } |
| 33 | + |
| 34 | + $process = new Process([ |
| 35 | + 'php', |
| 36 | + $this->getServerScript(), |
| 37 | + ], \dirname(__DIR__, 2), [] === $env ? null : $env, null, $timeout); |
| 38 | + |
| 39 | + $process->setInput($this->formatInput($messages)); |
| 40 | + $process->mustRun(); |
| 41 | + |
| 42 | + return $this->decodeJsonLines($process->getOutput()); |
| 43 | + } |
| 44 | + |
| 45 | + abstract protected function getServerScript(): string; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param mixed[] $params |
| 49 | + * |
| 50 | + * @throws \JsonException |
| 51 | + */ |
| 52 | + protected function jsonRequest(string $method, ?array $params = null, ?string $id = null): string |
| 53 | + { |
| 54 | + $payload = [ |
| 55 | + 'jsonrpc' => MessageInterface::JSONRPC_VERSION, |
| 56 | + 'id' => $id, |
| 57 | + 'method' => $method, |
| 58 | + ]; |
| 59 | + |
| 60 | + if (null !== $params) { |
| 61 | + $payload['params'] = $params; |
| 62 | + } |
| 63 | + |
| 64 | + return (string) json_encode($payload, \JSON_THROW_ON_ERROR); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param list<string> $messages |
| 69 | + */ |
| 70 | + private function formatInput(array $messages): string |
| 71 | + { |
| 72 | + return implode("\n", $messages)."\n"; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<string, array<string, array<string,mixed>>> |
| 77 | + */ |
| 78 | + private function decodeJsonLines(string $output): array |
| 79 | + { |
| 80 | + $output = trim($output); |
| 81 | + $responses = []; |
| 82 | + |
| 83 | + if ('' === $output) { |
| 84 | + return $responses; |
| 85 | + } |
| 86 | + |
| 87 | + foreach (preg_split('/\R+/', $output) as $line) { |
| 88 | + if ('' === $line) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + $decoded = json_decode($line, true, 512, \JSON_THROW_ON_ERROR); |
| 94 | + } catch (\JsonException) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (!\is_array($decoded)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $id = $decoded['id'] ?? null; |
| 103 | + |
| 104 | + if (\is_string($id) || \is_int($id)) { |
| 105 | + $responses[(string) $id] = $decoded; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $responses; |
| 110 | + } |
| 111 | + |
| 112 | + protected function getSnapshotFilePath(string $method): string |
| 113 | + { |
| 114 | + $className = substr(static::class, strrpos(static::class, '\\') + 1); |
| 115 | + |
| 116 | + return __DIR__.'/snapshots/'.$className.'-'.str_replace('/', '_', $method).'.json'; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return array<string,mixed> |
| 121 | + * |
| 122 | + * @throws \JsonException |
| 123 | + */ |
| 124 | + protected function loadSnapshot(string $method): array |
| 125 | + { |
| 126 | + $path = $this->getSnapshotFilePath($method); |
| 127 | + |
| 128 | + $contents = file_get_contents($path); |
| 129 | + $this->assertNotFalse($contents, 'Failed to read snapshot: '.$path); |
| 130 | + |
| 131 | + return json_decode($contents, true, 512, \JSON_THROW_ON_ERROR); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param array<string, array<string, mixed>> $response |
| 136 | + * |
| 137 | + * @throws \JsonException |
| 138 | + */ |
| 139 | + protected function assertResponseMatchesSnapshot(array $response, string $method): void |
| 140 | + { |
| 141 | + $this->assertArrayHasKey('result', $response); |
| 142 | + $actual = $response['result']; |
| 143 | + |
| 144 | + $expected = $this->loadSnapshot($method); |
| 145 | + |
| 146 | + $this->assertEquals($expected, $actual, 'Response payload does not match snapshot '.$this->getSnapshotFilePath($method)); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param array<string, array<string, mixed>> $capabilities |
| 151 | + * @param string[] $clientInfo |
| 152 | + * |
| 153 | + * @throws \JsonException |
| 154 | + */ |
| 155 | + protected function initializeMessage( |
| 156 | + ?string $id = null, |
| 157 | + string $protocolVersion = MessageInterface::PROTOCOL_VERSION, |
| 158 | + array $capabilities = [], |
| 159 | + array $clientInfo = [ |
| 160 | + 'name' => 'test-suite', |
| 161 | + 'version' => '1.0.0', |
| 162 | + ], |
| 163 | + ): string { |
| 164 | + $id ??= uniqid(); |
| 165 | + |
| 166 | + return $this->jsonRequest('initialize', [ |
| 167 | + 'protocolVersion' => $protocolVersion, |
| 168 | + 'capabilities' => $capabilities, |
| 169 | + 'clientInfo' => $clientInfo, |
| 170 | + ], $id); |
| 171 | + } |
| 172 | +} |
0 commit comments