Skip to content

Commit 0ce4837

Browse files
committed
test: use fake tests
1 parent 8b7af4d commit 0ce4837

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

tests/Feature/GrokClientTest.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use GrokPHP\Client\Config\GrokConfig;
88
use GrokPHP\Client\Enums\Model;
99
use GrokPHP\Client\Exceptions\GrokException;
10+
use GrokPHP\Client\Testing\ClientFake;
11+
use GuzzleHttp\Client;
12+
use GuzzleHttp\Handler\MockHandler;
13+
use GuzzleHttp\HandlerStack;
14+
use JsonException;
1015
use PHPUnit\Framework\TestCase;
1116

1217
class GrokClientTest extends TestCase
@@ -15,14 +20,26 @@ class GrokClientTest extends TestCase
1520

1621
/**
1722
* @throws GrokException
23+
* @throws JsonException
1824
*/
1925
protected function setUp(): void
2026
{
2127
parent::setUp();
22-
$config = new GrokConfig(getenv('GROK_API_KEY'));
23-
$this->client = new GrokClient($config);
28+
29+
$mock = new MockHandler([
30+
ClientFake::fakeSuccessResponse(),
31+
]);
32+
33+
$handlerStack = HandlerStack::create($mock);
34+
$httpClient = new Client(['handler' => $handlerStack]);
35+
36+
$config = new GrokConfig('fake-api-key');
37+
$this->client = new GrokClient($config, $httpClient);
2438
}
2539

40+
/**
41+
* @throws GrokException|JsonException
42+
*/
2643
public function test_chat_request_returns_response(): void
2744
{
2845
$messages = [
@@ -33,7 +50,12 @@ public function test_chat_request_returns_response(): void
3350
$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: false);
3451
$response = $this->client->chat($messages, $options);
3552

36-
$this->assertIsArray($response);
3753
$this->assertArrayHasKey('choices', $response);
54+
$this->assertSame('assistant', $response['choices'][0]['message']['role']);
55+
56+
$decodedContent = json_decode($response['choices'][0]['message']['content'], true, 512, JSON_THROW_ON_ERROR);
57+
$this->assertSame('Laravel', $decodedContent['framework_name']);
58+
$this->assertSame('2011', $decodedContent['release_date']);
59+
$this->assertSame('PHP', $decodedContent['programming_language']);
3860
}
3961
}

tests/Feature/VisionTest.php

+43-23
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use GrokPHP\Client\Config\GrokConfig;
88
use GrokPHP\Client\Enums\Model;
99
use GrokPHP\Client\Exceptions\GrokException;
10+
use GrokPHP\Client\Testing\VisionFake;
1011
use GuzzleHttp\Client;
11-
use GuzzleHttp\Psr7\Response;
12-
use Mockery;
12+
use GuzzleHttp\Handler\MockHandler;
13+
use GuzzleHttp\HandlerStack;
14+
use JsonException;
1315
use PHPUnit\Framework\TestCase;
1416

1517
class VisionTest extends TestCase
@@ -19,26 +21,34 @@ class VisionTest extends TestCase
1921
/**
2022
* @throws GrokException
2123
*/
24+
private Vision $visionSuccess;
25+
26+
private Vision $visionFailure;
27+
28+
/**
29+
* @throws GrokException
30+
* @throws JsonException
31+
*/
2232
protected function setUp(): void
2333
{
2434
parent::setUp();
2535

26-
// Mock Configuration
27-
$config = new GrokConfig(getenv('GROK_API_KEY'));
36+
$config = new GrokConfig('fake-api-key');
2837

29-
// Mock HTTP Client
30-
$httpClientMock = Mockery::mock(Client::class);
31-
$httpClientMock->shouldReceive('post')->andReturn(
32-
new Response(200, [], json_encode(['choices' => [['message' => ['content' => 'This is a dog.']]]]))
33-
);
34-
35-
// Mock GrokClient
36-
$client = Mockery::mock(GrokClient::class, [$config])->makePartial();
37-
$client->shouldReceive('chat')->andReturnUsing(function ($messages) {
38-
return ['choices' => [['message' => ['content' => 'Mocked Vision Response']]]];
39-
});
38+
$successMock = new MockHandler([
39+
VisionFake::fakeVisionSuccessResponse(),
40+
]);
41+
$successHttpClient = new Client(['handler' => HandlerStack::create($successMock)]);
42+
$successGrokClient = new GrokClient($config, $successHttpClient);
43+
$this->visionSuccess = new Vision($successGrokClient);
4044

41-
$this->vision = new Vision($client);
45+
$failureMock = new MockHandler([
46+
VisionFake::fakeVisionInvalidModelResponse(),
47+
VisionFake::fakeVisionImageNotFoundResponse(),
48+
]);
49+
$failureHttpClient = new Client(['handler' => HandlerStack::create($failureMock)]);
50+
$failureGrokClient = new GrokClient($config, $failureHttpClient);
51+
$this->visionFailure = new Vision($failureGrokClient);
4252
}
4353

4454
/**
@@ -48,11 +58,14 @@ protected function setUp(): void
4858
*/
4959
public function test_analyze_image_successfully(): void
5060
{
51-
$response = $this->vision->analyze('https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg', 'Describe this image');
61+
$response = $this->visionSuccess->analyze(
62+
'https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg',
63+
'Describe this image'
64+
);
5265

53-
$this->assertIsArray($response);
5466
$this->assertArrayHasKey('choices', $response);
55-
$this->assertEquals('Mocked Vision Response', $response['choices'][0]['message']['content']);
67+
$this->assertSame('assistant', $response['choices'][0]['message']['role']);
68+
$this->assertStringContainsString('cat', $response['choices'][0]['message']['content']);
5669
}
5770

5871
/**
@@ -63,17 +76,24 @@ public function test_throws_exception_for_invalid_model(): void
6376
$this->expectException(GrokException::class);
6477
$this->expectExceptionMessage('The model does not support image input but some images are present in the request.');
6578

66-
$this->vision->analyze('https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg', 'Describe this image', Model::GROK_2);
79+
$this->visionFailure->analyze(
80+
'https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg',
81+
'Describe this image',
82+
Model::GROK_2
83+
);
6784
}
6885

6986
/**
70-
* Test missing image error.
87+
* Test missing image error.
7188
*/
7289
public function test_throws_exception_when_image_not_found(): void
7390
{
7491
$this->expectException(GrokException::class);
75-
$this->expectExceptionMessage('Image file not found or invalid URL: /path/to/nonexistent/image.jpg');
92+
$this->expectExceptionMessage('Error fetching image from URL: https://invalid-url.com/nonexistent.jpg');
7693

77-
$this->vision->analyze('/path/to/nonexistent/image.jpg', 'Describe this image');
94+
$this->visionFailure->analyze(
95+
'https://invalid-url.com/nonexistent.jpg',
96+
'Describe this image'
97+
);
7898
}
7999
}

0 commit comments

Comments
 (0)