Skip to content

Commit 5907527

Browse files
committed
Refactor structured data test
1 parent 74b7252 commit 5907527

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Testing/ClientFake.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ClientFake
1212
*
1313
* @throws JsonException
1414
*/
15-
public static function fakeSuccessResponse(): Response
15+
public static function fakeSuccessResponse(?array $data = null): Response
1616
{
1717
return new Response(200, ['Content-Type' => 'application/json'], json_encode([
1818
'id' => '7c51076a-e4cc-4855-8dbe-66c26818e35f',
@@ -24,7 +24,7 @@ public static function fakeSuccessResponse(): Response
2424
'index' => 0,
2525
'message' => [
2626
'role' => 'assistant',
27-
'content' => json_encode([
27+
'content' => json_encode($data ?? [
2828
'framework_name' => 'Laravel',
2929
'release_date' => '2011',
3030
'programming_language' => 'PHP',

tests/Feature/GrokClientTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public function test_chat_request_returns_response(): void
6161

6262
public function test_chat_request_returns_response_in_specific_format(): void
6363
{
64+
$this->client->setHttpClient(new Client(['handler' => HandlerStack::create(new MockHandler([
65+
ClientFake::fakeSuccessResponse([
66+
'name' => 'Taylor',
67+
'city' => 'Little Rock'
68+
]),
69+
]))]));
70+
6471
$messages = [
6572
['role' => 'system', 'content' => 'You are a helpful assistant.'],
6673
['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
6976
$options = new ChatOptions(model: Model::GROK_2_1212, temperature: 0.7, stream: false, responseFormat: ['type' => 'json_object']);
7077
$response = $this->client->chat($messages, $options);
7178
$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);
7380

7481
$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']);
7786
}
7887
}

0 commit comments

Comments
 (0)