|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ecourty\McpServerBundle\Tests\Command; |
| 6 | + |
| 7 | +use Ecourty\McpServerBundle\Tests\Support\CommandTestCase; |
| 8 | +use Ecourty\McpServerBundle\Tests\Support\Trait\McpAssertTrait; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use Symfony\Component\Console\Tester\CommandTester; |
| 11 | + |
| 12 | +/** |
| 13 | + * @coversDefaultClass \Ecourty\McpServerBundle\Command\EntrypointCommand |
| 14 | + */ |
| 15 | +class EntrypointCommandTest extends CommandTestCase |
| 16 | +{ |
| 17 | + use McpAssertTrait; |
| 18 | + |
| 19 | + private CommandTester $commandTester; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->commandTester = $this->getCommandTester('mcp:stdio'); |
| 24 | + } |
| 25 | + |
| 26 | + #[DataProvider('provideNonExistentMethodRequest')] |
| 27 | + public function testExecuteWithNonExistentMethod(array $request): void |
| 28 | + { |
| 29 | + $this->commandTester->execute([ |
| 30 | + 'jsonrpc_request' => json_encode($request), |
| 31 | + ]); |
| 32 | + |
| 33 | + $this->commandTester->assertCommandIsSuccessful(); |
| 34 | + $output = $this->commandTester->getDisplay(true); |
| 35 | + |
| 36 | + $this->assertNonExistentMethod($output); |
| 37 | + } |
| 38 | + |
| 39 | + #[DataProvider('provideInitializeRequest')] |
| 40 | + public function testExecuteInitialize(array $request): void |
| 41 | + { |
| 42 | + $this->commandTester->execute([ |
| 43 | + 'jsonrpc_request' => json_encode($request), |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->commandTester->assertCommandIsSuccessful(); |
| 47 | + $output = $this->commandTester->getDisplay(true); |
| 48 | + |
| 49 | + $this->assertInitializeResponse($output); |
| 50 | + } |
| 51 | + |
| 52 | + #[DataProvider('provideToolsListRequest')] |
| 53 | + public function testExecuteToolsList(array $request): void |
| 54 | + { |
| 55 | + $this->commandTester->execute([ |
| 56 | + 'jsonrpc_request' => json_encode($request), |
| 57 | + ]); |
| 58 | + |
| 59 | + $this->commandTester->assertCommandIsSuccessful(); |
| 60 | + $output = $this->commandTester->getDisplay(true); |
| 61 | + |
| 62 | + $this->assertToolsList($output); |
| 63 | + } |
| 64 | + |
| 65 | + #[DataProvider('provideNonExistingToolRequest')] |
| 66 | + public function testNonExistentTool(array $request): void |
| 67 | + { |
| 68 | + $this->commandTester->execute([ |
| 69 | + 'jsonrpc_request' => json_encode($request), |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->commandTester->assertCommandIsSuccessful(); |
| 73 | + $output = $this->commandTester->getDisplay(true); |
| 74 | + |
| 75 | + $this->assertNonExistingToolResponse($output); |
| 76 | + } |
| 77 | + |
| 78 | + #[DataProvider('provideToolCallWithInvalidParamsRequest')] |
| 79 | + public function testToolCallWithInvalidParams(array $request): void |
| 80 | + { |
| 81 | + $this->commandTester->execute([ |
| 82 | + 'jsonrpc_request' => json_encode($request), |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->commandTester->assertCommandIsSuccessful(); |
| 86 | + $output = $this->commandTester->getDisplay(true); |
| 87 | + |
| 88 | + $this->assertToolCallWithInvalidParamsResponse($output); |
| 89 | + } |
| 90 | + |
| 91 | + #[DataProvider('provideTestToolCalls')] |
| 92 | + public function testToolCalls(string $method, array $params, mixed $expectedResponse): void |
| 93 | + { |
| 94 | + $request = \sprintf( |
| 95 | + '{"jsonrpc": "2.0", "id": 1, "method": "%s", "params": %s}', |
| 96 | + $method, |
| 97 | + json_encode($params, \JSON_THROW_ON_ERROR), |
| 98 | + ); |
| 99 | + |
| 100 | + $this->commandTester->execute([ |
| 101 | + 'jsonrpc_request' => $request, |
| 102 | + ]); |
| 103 | + |
| 104 | + $this->commandTester->assertCommandIsSuccessful(); |
| 105 | + $output = $this->commandTester->getDisplay(true); |
| 106 | + $responseContent = json_decode($output, true); |
| 107 | + |
| 108 | + $this->assertSame($expectedResponse, $responseContent); |
| 109 | + } |
| 110 | + |
| 111 | + #[DataProvider('providePromptsListRequest')] |
| 112 | + public function testPromptsList(array $request): void |
| 113 | + { |
| 114 | + $this->commandTester->execute([ |
| 115 | + 'jsonrpc_request' => json_encode($request), |
| 116 | + ]); |
| 117 | + |
| 118 | + $this->commandTester->assertCommandIsSuccessful(); |
| 119 | + $output = $this->commandTester->getDisplay(true); |
| 120 | + |
| 121 | + $this->assertPromptsList($output); |
| 122 | + } |
| 123 | + |
| 124 | + #[DataProvider('providePromptGetRequest')] |
| 125 | + public function testPromptGet(array $request, string $scope, string $changes): void |
| 126 | + { |
| 127 | + $this->commandTester->execute([ |
| 128 | + 'jsonrpc_request' => json_encode($request), |
| 129 | + ]); |
| 130 | + |
| 131 | + $this->commandTester->assertCommandIsSuccessful(); |
| 132 | + $output = $this->commandTester->getDisplay(true); |
| 133 | + |
| 134 | + $this->assertPromptGet($output, $changes, $scope); |
| 135 | + } |
| 136 | + |
| 137 | + #[DataProvider('providePromptGetWithMissingArgument')] |
| 138 | + public function testPromptGetWithNonRequiredParameterLeftEmpty(array $request): void |
| 139 | + { |
| 140 | + $this->commandTester->execute([ |
| 141 | + 'jsonrpc_request' => json_encode($request), |
| 142 | + ]); |
| 143 | + |
| 144 | + $this->commandTester->assertCommandIsSuccessful(); |
| 145 | + $output = $this->commandTester->getDisplay(true); |
| 146 | + |
| 147 | + $this->assertPromptGetWithNonRequiredArgumentLeftEmpty($output); |
| 148 | + } |
| 149 | + |
| 150 | + #[DataProvider('providePromptWithUnsafeParametersRequest')] |
| 151 | + public function testPromptWithUnsafeContent(array $request, string $unsafeContent): void |
| 152 | + { |
| 153 | + $this->commandTester->execute([ |
| 154 | + 'jsonrpc_request' => json_encode($request), |
| 155 | + ]); |
| 156 | + |
| 157 | + $this->commandTester->assertCommandIsSuccessful(); |
| 158 | + $output = $this->commandTester->getDisplay(true); |
| 159 | + |
| 160 | + $this->assertPromptWithUnsafeParameters($output, $unsafeContent); |
| 161 | + } |
| 162 | + |
| 163 | + #[DataProvider('provideDirectResourceListRequest')] |
| 164 | + public function testDirectResourceList(array $request): void |
| 165 | + { |
| 166 | + $this->commandTester->execute([ |
| 167 | + 'jsonrpc_request' => json_encode($request), |
| 168 | + ]); |
| 169 | + |
| 170 | + $this->commandTester->assertCommandIsSuccessful(); |
| 171 | + $output = $this->commandTester->getDisplay(true); |
| 172 | + |
| 173 | + $this->assertDirectResourcesList($output); |
| 174 | + } |
| 175 | + |
| 176 | + #[DataProvider('provideTemplateResourceListRequest')] |
| 177 | + public function testTemplateResourceList(array $request): void |
| 178 | + { |
| 179 | + $this->commandTester->execute([ |
| 180 | + 'jsonrpc_request' => json_encode($request), |
| 181 | + ]); |
| 182 | + |
| 183 | + $this->commandTester->assertCommandIsSuccessful(); |
| 184 | + $output = $this->commandTester->getDisplay(true); |
| 185 | + |
| 186 | + $this->assertTemplateResourcesList($output); |
| 187 | + } |
| 188 | + |
| 189 | + #[DataProvider('provideNotFoundResourceRequest')] |
| 190 | + public function testCallNotFoundResource(array $request): void |
| 191 | + { |
| 192 | + $this->commandTester->execute([ |
| 193 | + 'jsonrpc_request' => json_encode($request), |
| 194 | + ]); |
| 195 | + |
| 196 | + $this->commandTester->assertCommandIsSuccessful(); |
| 197 | + $output = $this->commandTester->getDisplay(true); |
| 198 | + |
| 199 | + $this->assertCallNotFoundResource($output); |
| 200 | + } |
| 201 | + |
| 202 | + #[DataProvider('provideDirectResourceRequest')] |
| 203 | + public function testCallDirectResource(array $request): void |
| 204 | + { |
| 205 | + $this->commandTester->execute([ |
| 206 | + 'jsonrpc_request' => json_encode($request), |
| 207 | + ]); |
| 208 | + |
| 209 | + $this->commandTester->assertCommandIsSuccessful(); |
| 210 | + $output = $this->commandTester->getDisplay(true); |
| 211 | + |
| 212 | + $this->assertCallDirectResource($output); |
| 213 | + } |
| 214 | + |
| 215 | + #[DataProvider('provideTestDataForTemplateResourceCall')] |
| 216 | + public function testCallTemplateResource(array $request, array $expectedResult): void |
| 217 | + { |
| 218 | + $this->commandTester->execute([ |
| 219 | + 'jsonrpc_request' => json_encode($request), |
| 220 | + ]); |
| 221 | + |
| 222 | + $this->commandTester->assertCommandIsSuccessful(); |
| 223 | + $output = $this->commandTester->getDisplay(true); |
| 224 | + |
| 225 | + $parsedOutput = json_decode($output, true); |
| 226 | + $result = $parsedOutput['result'] ?? null; |
| 227 | + |
| 228 | + $this->assertNotNull($result); |
| 229 | + |
| 230 | + $this->assertEquals($result, $expectedResult); |
| 231 | + } |
| 232 | +} |
0 commit comments