diff --git a/tests/Inspector/InspectorSnapshotTestCase.php b/tests/Inspector/InspectorSnapshotTestCase.php index 71a065a6..8fe3d96a 100644 --- a/tests/Inspector/InspectorSnapshotTestCase.php +++ b/tests/Inspector/InspectorSnapshotTestCase.php @@ -19,20 +19,42 @@ abstract class InspectorSnapshotTestCase extends TestCase { private const INSPECTOR_VERSION = '0.16.8'; + /** + * @param array $toolArgs + */ #[DataProvider('provideMethods')] - public function testResourcesListOutputMatchesSnapshot(string $method): void - { - $process = (new Process([ - 'npx', - \sprintf('@modelcontextprotocol/inspector@%s', self::INSPECTOR_VERSION), - '--cli', - 'php', - $this->getServerScript(), - '--method', - $method, - ]))->mustRun(); - - $output = $process->getOutput(); + public function testMethodOutputMatchesSnapshot( + string $method, + ?string $toolName = null, + array $toolArgs = [], + ?string $uri = null, + ): void { + $inspector = \sprintf('@modelcontextprotocol/inspector@%s', self::INSPECTOR_VERSION); + $args = [ + 'npx', $inspector, '--cli', 'php', $this->getServerScript(), '--method', $method, + ]; + + // Options for tools/call + if (null !== $toolName) { + $args[] = '--tool-name'; + $args[] = $toolName; + + foreach ($toolArgs as $key => $value) { + $args[] = '--tool-arg'; + $args[] = \sprintf('%s=%s', $key, $value); + } + } + + // Options for resources/read + if (null !== $uri) { + $args[] = '--uri'; + $args[] = $uri; + } + + $output = (new Process($args)) + ->mustRun() + ->getOutput(); + $snapshotFile = $this->getSnapshotFilePath($method); if (!file_exists($snapshotFile)) { diff --git a/tests/Inspector/StdioCalculatorExampleTest.php b/tests/Inspector/StdioCalculatorExampleTest.php index b1b1bc5e..b023b5b5 100644 --- a/tests/Inspector/StdioCalculatorExampleTest.php +++ b/tests/Inspector/StdioCalculatorExampleTest.php @@ -17,6 +17,17 @@ public static function provideMethods(): array { return [ ...parent::provideListMethods(), + 'Calculate Sum' => [ + 'method' => 'tools/call', + 'toolName' => 'calculate', + 'toolArgs' => ['a' => 12.5, 'b' => 7.3, 'operation' => 'add'], + ], + 'Read Config' => [ + 'method' => 'resources/read', + 'toolName' => null, // can be removed with newer PHPUnit versions + 'toolArgs' => [], // can be removed with newer PHPUnit versions + 'uri' => 'config://calculator/settings', + ], ]; } diff --git a/tests/Inspector/snapshots/StdioCalculatorExampleTest-resources_read.json b/tests/Inspector/snapshots/StdioCalculatorExampleTest-resources_read.json new file mode 100644 index 00000000..c15d9a8e --- /dev/null +++ b/tests/Inspector/snapshots/StdioCalculatorExampleTest-resources_read.json @@ -0,0 +1,9 @@ +{ + "contents": [ + { + "uri": "config://calculator/settings", + "mimeType": "application/json", + "text": "{\n \"precision\": 2,\n \"allow_negative\": true\n}" + } + ] +} diff --git a/tests/Inspector/snapshots/StdioCalculatorExampleTest-tools_call.json b/tests/Inspector/snapshots/StdioCalculatorExampleTest-tools_call.json new file mode 100644 index 00000000..a73c8b94 --- /dev/null +++ b/tests/Inspector/snapshots/StdioCalculatorExampleTest-tools_call.json @@ -0,0 +1,9 @@ +{ + "content": [ + { + "type": "text", + "text": "19.8" + } + ], + "isError": false +}