@@ -61,6 +61,13 @@ public function test_chat_request_returns_response(): void
61
61
62
62
public function test_chat_request_returns_response_in_specific_format (): void
63
63
{
64
+ $ this ->client ->setHttpClient (new Client (['handler ' => HandlerStack::create (new MockHandler ([
65
+ ClientFake::fakeSuccessResponse ([
66
+ 'name ' => 'Taylor ' ,
67
+ 'city ' => 'Little Rock '
68
+ ]),
69
+ ]))]));
70
+
64
71
$ messages = [
65
72
['role ' => 'system ' , 'content ' => 'You are a helpful assistant. ' ],
66
73
['role ' => 'user ' , 'content ' => 'Return a json object with a random name (string) and random city (string). ' ],
@@ -69,10 +76,12 @@ public function test_chat_request_returns_response_in_specific_format(): void
69
76
$ options = new ChatOptions (model: Model::GROK_2_1212 , temperature: 0.7 , stream: false , responseFormat: ['type ' => 'json_object ' ]);
70
77
$ response = $ this ->client ->chat ($ messages , $ options );
71
78
$ content = $ response ['choices ' ][0 ]['message ' ]['content ' ];
72
- $ contentArray = json_decode ($ content , true );
79
+ $ decodedContent = json_decode ($ response [ ' choices ' ][ 0 ][ ' message ' ][ ' content ' ] , true , 512 , JSON_THROW_ON_ERROR );
73
80
74
81
$ this ->assertJson ($ content );
75
- $ this ->assertArrayHasKey ('name ' , $ contentArray );
76
- $ this ->assertArrayHasKey ('city ' , $ contentArray );
82
+ $ this ->assertArrayHasKey ('name ' , $ decodedContent );
83
+ $ this ->assertArrayHasKey ('city ' , $ decodedContent );
84
+ $ this ->assertSame ('Taylor ' , $ decodedContent ['name ' ]);
85
+ $ this ->assertSame ('Little Rock ' , $ decodedContent ['city ' ]);
77
86
}
78
87
}
0 commit comments