Skip to content

Commit 592140d

Browse files
authored
Add assertStructuredContent method (#140)
* assertStructuredContent method * Fix phpstan error
1 parent 26907a0 commit 592140d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/Server/Testing/TestResponse.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ public function assertDontSee(array|string $text): static
104104
return $this;
105105
}
106106

107+
/**
108+
* @param array<string, mixed> $structuredContent
109+
*/
110+
public function assertStructuredContent(array $structuredContent): static
111+
{
112+
Assert::assertSame(
113+
$structuredContent,
114+
$this->response->toArray()['result']['structuredContent'] ?? null,
115+
'The expected structured content does not match the actual structured content.'
116+
);
117+
118+
return $this;
119+
}
120+
107121
public function assertNotificationCount(int $count): static
108122
{
109123
Assert::assertCount($count, $this->notifications, "The expected number of notifications [{$count}] does not match the actual count.");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
use Laravel\Mcp\Response;
4+
use Laravel\Mcp\ResponseFactory;
5+
use Laravel\Mcp\Server;
6+
use Laravel\Mcp\Server\Tool;
7+
use PHPUnit\Framework\ExpectationFailedException;
8+
9+
class BookingServer extends Server
10+
{
11+
protected array $tools = [
12+
GetBookingTool::class,
13+
GetBookingWithoutStructuredContentTool::class,
14+
];
15+
}
16+
17+
class GetBookingTool extends Tool
18+
{
19+
protected string $name = 'booking/get';
20+
21+
protected string $title = 'Get booking';
22+
23+
protected string $description = 'Get a booking';
24+
25+
public function handle(): ResponseFactory
26+
{
27+
return Response::structured([
28+
'type' => 'booking',
29+
'id' => 123,
30+
'status' => 'confirmed',
31+
]);
32+
}
33+
}
34+
35+
class GetBookingWithoutStructuredContentTool extends Tool
36+
{
37+
protected string $name = 'booking/get/no-structured';
38+
39+
protected string $title = 'Get booking';
40+
41+
protected string $description = 'Get a booking';
42+
43+
public function handle(): Response
44+
{
45+
return Response::text('This is the confirmed booking 123');
46+
}
47+
}
48+
49+
it('may assert the structured content', function (): void {
50+
$response = BookingServer::tool(GetBookingTool::class);
51+
52+
$response->assertStructuredContent([
53+
'type' => 'booking',
54+
'id' => 123,
55+
'status' => 'confirmed',
56+
]);
57+
});
58+
59+
it('fails to assert the structured content is wrong', function (): void {
60+
$response = BookingServer::tool(GetBookingTool::class);
61+
62+
$response->assertStructuredContent([
63+
'type' => 'booking',
64+
'id' => 124,
65+
'status' => 'pending',
66+
]);
67+
})->throws(ExpectationFailedException::class);
68+
69+
it('fails to assert the structured content is not present', function (): void {
70+
$response = BookingServer::tool(GetBookingWithoutStructuredContentTool::class);
71+
72+
$response->assertStructuredContent([
73+
'type' => 'booking',
74+
'id' => 123,
75+
'status' => 'confirmed',
76+
]);
77+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)