|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GrokPHP\Client\Tests\Feature; |
| 4 | + |
| 5 | +use GrokPHP\Client\Clients\GrokClient; |
| 6 | +use GrokPHP\Client\Clients\Vision; |
| 7 | +use GrokPHP\Client\Config\GrokConfig; |
| 8 | +use GrokPHP\Client\Enums\Model; |
| 9 | +use GrokPHP\Client\Exceptions\GrokException; |
| 10 | +use GuzzleHttp\Client; |
| 11 | +use GuzzleHttp\Psr7\Response; |
| 12 | +use Mockery; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class VisionTest extends TestCase |
| 16 | +{ |
| 17 | + private Vision $vision; |
| 18 | + |
| 19 | + /** |
| 20 | + * @throws GrokException |
| 21 | + */ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + // Mock Configuration |
| 27 | + $config = new GrokConfig(getenv('GROK_API_KEY')); |
| 28 | + |
| 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 | + }); |
| 40 | + |
| 41 | + $this->vision = new Vision($client); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * ✅ Test successful image analysis. |
| 46 | + * |
| 47 | + * @throws GrokException |
| 48 | + */ |
| 49 | + public function test_analyze_image_successfully(): void |
| 50 | + { |
| 51 | + $response = $this->vision->analyze('https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg', 'Describe this image'); |
| 52 | + |
| 53 | + $this->assertIsArray($response); |
| 54 | + $this->assertArrayHasKey('choices', $response); |
| 55 | + $this->assertEquals('Mocked Vision Response', $response['choices'][0]['message']['content']); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * ✅ Test invalid model error. |
| 60 | + */ |
| 61 | + public function test_throws_exception_for_invalid_model(): void |
| 62 | + { |
| 63 | + $this->expectException(GrokException::class); |
| 64 | + $this->expectExceptionMessage('The model does not support image input but some images are present in the request.'); |
| 65 | + |
| 66 | + $this->vision->analyze('https://www.shutterstock.com/image-photo/young-english-cocker-spaniel-puppy-600nw-2026045151.jpg', 'Describe this image', Model::GROK_2); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * ✅ Test missing image error. |
| 71 | + */ |
| 72 | + public function test_throws_exception_when_image_not_found(): void |
| 73 | + { |
| 74 | + $this->expectException(GrokException::class); |
| 75 | + $this->expectExceptionMessage('Image file not found or invalid URL: /path/to/nonexistent/image.jpg'); |
| 76 | + |
| 77 | + $this->vision->analyze('/path/to/nonexistent/image.jpg', 'Describe this image'); |
| 78 | + } |
| 79 | +} |
0 commit comments