Skip to content

Commit 9ff6516

Browse files
authored
Add happy cases for functional tests of example 1 (#91)
1 parent 4486776 commit 9ff6516

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

tests/Inspector/InspectorSnapshotTestCase.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,42 @@ abstract class InspectorSnapshotTestCase extends TestCase
1919
{
2020
private const INSPECTOR_VERSION = '0.16.8';
2121

22+
/**
23+
* @param array<string, mixed> $toolArgs
24+
*/
2225
#[DataProvider('provideMethods')]
23-
public function testResourcesListOutputMatchesSnapshot(string $method): void
24-
{
25-
$process = (new Process([
26-
'npx',
27-
\sprintf('@modelcontextprotocol/inspector@%s', self::INSPECTOR_VERSION),
28-
'--cli',
29-
'php',
30-
$this->getServerScript(),
31-
'--method',
32-
$method,
33-
]))->mustRun();
34-
35-
$output = $process->getOutput();
26+
public function testMethodOutputMatchesSnapshot(
27+
string $method,
28+
?string $toolName = null,
29+
array $toolArgs = [],
30+
?string $uri = null,
31+
): void {
32+
$inspector = \sprintf('@modelcontextprotocol/inspector@%s', self::INSPECTOR_VERSION);
33+
$args = [
34+
'npx', $inspector, '--cli', 'php', $this->getServerScript(), '--method', $method,
35+
];
36+
37+
// Options for tools/call
38+
if (null !== $toolName) {
39+
$args[] = '--tool-name';
40+
$args[] = $toolName;
41+
42+
foreach ($toolArgs as $key => $value) {
43+
$args[] = '--tool-arg';
44+
$args[] = \sprintf('%s=%s', $key, $value);
45+
}
46+
}
47+
48+
// Options for resources/read
49+
if (null !== $uri) {
50+
$args[] = '--uri';
51+
$args[] = $uri;
52+
}
53+
54+
$output = (new Process($args))
55+
->mustRun()
56+
->getOutput();
57+
3658
$snapshotFile = $this->getSnapshotFilePath($method);
3759

3860
if (!file_exists($snapshotFile)) {

tests/Inspector/StdioCalculatorExampleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ public static function provideMethods(): array
1717
{
1818
return [
1919
...parent::provideListMethods(),
20+
'Calculate Sum' => [
21+
'method' => 'tools/call',
22+
'toolName' => 'calculate',
23+
'toolArgs' => ['a' => 12.5, 'b' => 7.3, 'operation' => 'add'],
24+
],
25+
'Read Config' => [
26+
'method' => 'resources/read',
27+
'toolName' => null, // can be removed with newer PHPUnit versions
28+
'toolArgs' => [], // can be removed with newer PHPUnit versions
29+
'uri' => 'config://calculator/settings',
30+
],
2031
];
2132
}
2233

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"contents": [
3+
{
4+
"uri": "config://calculator/settings",
5+
"mimeType": "application/json",
6+
"text": "{\n \"precision\": 2,\n \"allow_negative\": true\n}"
7+
}
8+
]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"content": [
3+
{
4+
"type": "text",
5+
"text": "19.8"
6+
}
7+
],
8+
"isError": false
9+
}

0 commit comments

Comments
 (0)