61 lines
2.0 KiB
PHP
61 lines
2.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Tests\Service\AI;
|
||
|
|
|
||
|
|
use App\Service\AI\GeminiClient;
|
||
|
|
use GeminiAPI\Client;
|
||
|
|
use GeminiAPI\GenerativeModel;
|
||
|
|
use GeminiAPI\Resources\Parts\TextPart;
|
||
|
|
use GeminiAPI\Responses\GenerateContentResponse;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
|
||
|
|
class GeminiClientTest extends TestCase
|
||
|
|
{
|
||
|
|
public function testGenerateFriendlyMessageSuccess(): void
|
||
|
|
{
|
||
|
|
// Mock the response
|
||
|
|
$responseMock = $this->createStub(GenerateContentResponse::class);
|
||
|
|
$responseMock->method('text')
|
||
|
|
->willReturn('This is a friendly update.');
|
||
|
|
|
||
|
|
// Mock the model
|
||
|
|
$modelMock = $this->createMock(GenerativeModel::class);
|
||
|
|
$modelMock->expects($this->once())
|
||
|
|
->method('generateContent')
|
||
|
|
->with($this->isInstanceOf(TextPart::class))
|
||
|
|
->willReturn($responseMock);
|
||
|
|
|
||
|
|
// Mock the client
|
||
|
|
$clientMock = $this->createMock(Client::class);
|
||
|
|
$clientMock->expects($this->once())
|
||
|
|
->method('withV1BetaVersion')
|
||
|
|
->willReturnSelf();
|
||
|
|
$clientMock->expects($this->once())
|
||
|
|
->method('generativeModel')
|
||
|
|
->with('gemini-3-pro-preview')
|
||
|
|
->willReturn($modelMock);
|
||
|
|
|
||
|
|
// Instantiate GeminiClient with mocked Client
|
||
|
|
$geminiClient = new GeminiClient('fake-api-key', $clientMock);
|
||
|
|
|
||
|
|
$result = $geminiClient->generateFriendlyMessage('Raw technical message');
|
||
|
|
|
||
|
|
$this->assertEquals('This is a friendly update.', $result);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGenerateFriendlyMessageException(): void
|
||
|
|
{
|
||
|
|
// Mock the client to throw an exception
|
||
|
|
$clientMock = $this->createStub(Client::class);
|
||
|
|
$clientMock->method('withV1BetaVersion')
|
||
|
|
->willThrowException(new \Exception('API Error'));
|
||
|
|
|
||
|
|
// Instantiate GeminiClient with mocked Client
|
||
|
|
$geminiClient = new GeminiClient('fake-api-key', $clientMock);
|
||
|
|
|
||
|
|
$result = $geminiClient->generateFriendlyMessage('Raw technical message');
|
||
|
|
|
||
|
|
$this->assertNull($result);
|
||
|
|
}
|
||
|
|
}
|