7
7
use GrokPHP \Client \Config \GrokConfig ;
8
8
use GrokPHP \Client \Enums \Model ;
9
9
use GrokPHP \Client \Exceptions \GrokException ;
10
+ use GrokPHP \Client \Testing \VisionFake ;
10
11
use GuzzleHttp \Client ;
11
- use GuzzleHttp \Psr7 \Response ;
12
- use Mockery ;
12
+ use GuzzleHttp \Handler \MockHandler ;
13
+ use GuzzleHttp \HandlerStack ;
14
+ use JsonException ;
13
15
use PHPUnit \Framework \TestCase ;
14
16
15
17
class VisionTest extends TestCase
@@ -19,26 +21,34 @@ class VisionTest extends TestCase
19
21
/**
20
22
* @throws GrokException
21
23
*/
24
+ private Vision $ visionSuccess ;
25
+
26
+ private Vision $ visionFailure ;
27
+
28
+ /**
29
+ * @throws GrokException
30
+ * @throws JsonException
31
+ */
22
32
protected function setUp (): void
23
33
{
24
34
parent ::setUp ();
25
35
26
- // Mock Configuration
27
- $ config = new GrokConfig (getenv ('GROK_API_KEY ' ));
36
+ $ config = new GrokConfig ('fake-api-key ' );
28
37
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 );
40
44
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 );
42
52
}
43
53
44
54
/**
@@ -48,11 +58,14 @@ protected function setUp(): void
48
58
*/
49
59
public function test_analyze_image_successfully (): void
50
60
{
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
+ );
52
65
53
- $ this ->assertIsArray ($ response );
54
66
$ 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 ' ]);
56
69
}
57
70
58
71
/**
@@ -63,17 +76,24 @@ public function test_throws_exception_for_invalid_model(): void
63
76
$ this ->expectException (GrokException::class);
64
77
$ this ->expectExceptionMessage ('The model does not support image input but some images are present in the request. ' );
65
78
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
+ );
67
84
}
68
85
69
86
/**
70
- * ✅ Test missing image error.
87
+ * Test missing image error.
71
88
*/
72
89
public function test_throws_exception_when_image_not_found (): void
73
90
{
74
91
$ 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 ' );
76
93
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
+ );
78
98
}
79
99
}
0 commit comments