Skip to content

Commit bf188d4

Browse files
committed
Update chat unit test to include fake response for the moderation call and chat call
1 parent c389cc3 commit bf188d4

File tree

7 files changed

+17
-892
lines changed

7 files changed

+17
-892
lines changed

Tests/OpenAIProvider/AudioInterfaceTest.php

Lines changed: 0 additions & 166 deletions
This file was deleted.

Tests/OpenAIProvider/ChatInterfaceTest.php

Lines changed: 0 additions & 146 deletions
This file was deleted.

Tests/OpenAIProvider/ChatTest.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use Joomla\Http\Http;
44
use Joomla\Http\HttpFactory;
5-
use Joomla\Http\Response as HttpResponse;
65
use Joomla\AI\Provider\OpenAIProvider;
6+
use Joomla\Http\Response as HttpResponse;
77
use PHPUnit\Framework\TestCase;
88

99
class ChatTest extends TestCase
@@ -27,19 +27,30 @@ public function testSimpleChatCompletion()
2727
],
2828
]);
2929

30+
$fakeModerationResponseBody = json_encode([
31+
'id' => 'modr-test',
32+
'model' => 'text-moderation-001',
33+
'results' => [
34+
[
35+
'flagged' => false,
36+
],
37+
],
38+
]);
39+
3040
$httpFactoryMock = $this->createMock(HttpFactory::class);
3141
$httpClientMock = $this->createMock(Http::class);
3242

3343
$httpFactoryMock->method('getHttp')->willReturn($httpClientMock);
3444

35-
$response = new HttpResponse('php://memory', 200, ['Content-Type' => 'application/json']);
36-
$stream = $response->getBody();
37-
$stream->write($fakeChatResponseBody);
45+
$moderationResponse = new HttpResponse('php://memory', 200, ['Content-Type' => 'application/json']);
46+
$moderationResponse->getBody()->write($fakeModerationResponseBody);
3847

39-
$httpClientMock->method('post')->willReturn($response);
48+
$chatResponse = new HttpResponse('php://memory', 200, ['Content-Type' => 'application/json']);
49+
$chatResponse->getBody()->write($fakeChatResponseBody);
4050

41-
$provider = new OpenAIProviderNoModeration(['api_key' => 'test-api-key'], $httpFactoryMock);
51+
$httpClientMock->method('post')->willReturnOnConsecutiveCalls($moderationResponse, $chatResponse);
4252

53+
$provider = new OpenAIProvider(['api_key' => 'test-api-key'], $httpFactoryMock);
4354
$response = $provider->chat('Hello! How are you?', ['model' => 'gpt-4o-mini']);
4455

4556
$this->assertEquals(200, $response->getStatusCode());
@@ -49,11 +60,3 @@ public function testSimpleChatCompletion()
4960
$this->assertArrayHasKey('usage', $metadata);
5061
}
5162
}
52-
53-
class OpenAIProviderNoModeration extends OpenAIProvider
54-
{
55-
protected function moderateInput($input, array $options = []): bool
56-
{
57-
return false;
58-
}
59-
}

0 commit comments

Comments
 (0)