22
33use Joomla \Http \Http ;
44use Joomla \Http \HttpFactory ;
5- use Joomla \Http \Response as HttpResponse ;
65use Joomla \AI \Provider \OpenAIProvider ;
6+ use Joomla \Http \Response as HttpResponse ;
77use PHPUnit \Framework \TestCase ;
88
99class 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