Skip to content

Commit 619034f

Browse files
committed
Added tests
1 parent 8718072 commit 619034f

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Tests\Unit\Schema\Result;
13+
14+
use Mcp\Schema\Content\TextContent;
15+
use Mcp\Schema\Result\CallToolResult;
16+
use Mcp\Schema\Result\CallToolStructuredContentResult;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class CallToolStructuredContentResultTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider provideJsonSerializeScenarios
23+
*
24+
* @param array<string, mixed> $structuredContent
25+
* @param array<string, mixed> $expected
26+
*/
27+
public function testJsonSerialize(
28+
array $structuredContent,
29+
CallToolResult $callToolResult,
30+
array $expected,
31+
): void {
32+
$result = new CallToolStructuredContentResult($structuredContent, $callToolResult);
33+
34+
$this->assertEquals($expected, $result->jsonSerialize());
35+
}
36+
37+
/**
38+
* @return iterable<string, array{array<string, mixed>, CallToolResult, array<string, mixed>}>
39+
*/
40+
public static function provideJsonSerializeScenarios(): iterable
41+
{
42+
$successContent = new TextContent('Hello, World!');
43+
$errorContent = new TextContent('Failure details');
44+
45+
yield 'success' => [
46+
['result' => 'Hello, World!'],
47+
new CallToolResult([$successContent]),
48+
[
49+
'structuredContent' => ['result' => 'Hello, World!'],
50+
'content' => [$successContent],
51+
'isError' => false,
52+
],
53+
];
54+
55+
yield 'error' => [
56+
['result' => 'Failure details'],
57+
CallToolResult::error([$errorContent]),
58+
[
59+
'structuredContent' => ['result' => 'Failure details'],
60+
'content' => [$errorContent],
61+
'isError' => true,
62+
],
63+
];
64+
}
65+
}

tests/Unit/Server/Handler/Request/CallToolHandlerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Mcp\Schema\JsonRpc\Response;
2222
use Mcp\Schema\Request\CallToolRequest;
2323
use Mcp\Schema\Result\CallToolResult;
24+
use Mcp\Schema\Result\CallToolStructuredContentResult;
2425
use Mcp\Server\Handler\Request\CallToolHandler;
2526
use Mcp\Server\Session\SessionInterface;
2627
use PHPUnit\Framework\MockObject\MockObject;
@@ -342,6 +343,63 @@ public function testHandleWithSpecialCharactersInArguments(): void
342343
$this->assertEquals($expectedResult, $response->result);
343344
}
344345

346+
public function testHandleReturnsStructuredContentResult(): void
347+
{
348+
$request = $this->createCallToolRequest('structured_tool', ['query' => 'php']);
349+
$toolReference = $this->createMock(ToolReference::class);
350+
$innerResult = new CallToolResult([new TextContent('Rendered results')]);
351+
$structuredResult = new CallToolStructuredContentResult(['result' => 'Rendered results'], $innerResult);
352+
353+
$this->referenceProvider
354+
->expects($this->once())
355+
->method('getTool')
356+
->with('structured_tool')
357+
->willReturn($toolReference);
358+
359+
$this->referenceHandler
360+
->expects($this->once())
361+
->method('handle')
362+
->with($toolReference, ['query' => 'php'])
363+
->willReturn($structuredResult);
364+
365+
$toolReference
366+
->expects($this->never())
367+
->method('formatResult');
368+
369+
$response = $this->handler->handle($request, $this->session);
370+
371+
$this->assertInstanceOf(Response::class, $response);
372+
$this->assertSame($structuredResult, $response->result);
373+
}
374+
375+
public function testHandleReturnsCallToolResult(): void
376+
{
377+
$request = $this->createCallToolRequest('result_tool', ['query' => 'php']);
378+
$toolReference = $this->createMock(ToolReference::class);
379+
$callToolResult = new CallToolResult([new TextContent('Rendered results')]);
380+
381+
$this->referenceProvider
382+
->expects($this->once())
383+
->method('getTool')
384+
->with('result_tool')
385+
->willReturn($toolReference);
386+
387+
$this->referenceHandler
388+
->expects($this->once())
389+
->method('handle')
390+
->with($toolReference, ['query' => 'php'])
391+
->willReturn($callToolResult);
392+
393+
$toolReference
394+
->expects($this->never())
395+
->method('formatResult');
396+
397+
$response = $this->handler->handle($request, $this->session);
398+
399+
$this->assertInstanceOf(Response::class, $response);
400+
$this->assertSame($callToolResult, $response->result);
401+
}
402+
345403
/**
346404
* @param array<string, mixed> $arguments
347405
*/

0 commit comments

Comments
 (0)