|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Tests\Bridge\OpenAi\TextToSpeech; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\AI\Platform\Bridge\OpenAi\TextToSpeech; |
| 16 | +use Symfony\AI\Platform\Bridge\OpenAi\TextToSpeech\ModelClient; |
| 17 | +use Symfony\AI\Platform\Bridge\OpenAi\TextToSpeech\ResultConverter; |
| 18 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 19 | +use Symfony\AI\Platform\Model; |
| 20 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 21 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 22 | +use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Christopher Hertel <mail@christopher-hertel.de> |
| 26 | + */ |
| 27 | +final class ModelClientTest extends TestCase |
| 28 | +{ |
| 29 | + public function testSupportsTextToSpeechModel() |
| 30 | + { |
| 31 | + $converter = new ResultConverter(); |
| 32 | + $model = new TextToSpeech('tts-1'); |
| 33 | + |
| 34 | + $this->assertTrue($converter->supports($model)); |
| 35 | + } |
| 36 | + |
| 37 | + public function testDoesntSupportOtherModels() |
| 38 | + { |
| 39 | + $converter = new ResultConverter(); |
| 40 | + $model = new Model('test-model'); |
| 41 | + |
| 42 | + $this->assertFalse($converter->supports($model)); |
| 43 | + } |
| 44 | + |
| 45 | + public function testHappyCase() |
| 46 | + { |
| 47 | + $resultCallback = static function (string $method, string $url, array $options): HttpResponse { |
| 48 | + self::assertSame('POST', $method); |
| 49 | + self::assertSame('https://api.openai.com/v1/audio/speech', $url); |
| 50 | + self::assertSame('Authorization: Bearer sk-api-key', $options['normalized_headers']['authorization'][0]); |
| 51 | + $expectedBody = '{"voice":"alloy","instruction":"Speak like a pirate","model":"tts-1","input":"Hello World!"}'; |
| 52 | + self::assertSame($expectedBody, $options['body']); |
| 53 | + |
| 54 | + return new MockResponse(); |
| 55 | + }; |
| 56 | + $httpClient = new MockHttpClient([$resultCallback]); |
| 57 | + $modelClient = new ModelClient($httpClient, 'sk-api-key'); |
| 58 | + $modelClient->request(new TextToSpeech('tts-1'), 'Hello World!', [ |
| 59 | + 'voice' => 'alloy', |
| 60 | + 'instruction' => 'Speak like a pirate', |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + public function testFailsWithoutVoiceOption() |
| 65 | + { |
| 66 | + $this->expectException(InvalidArgumentException::class); |
| 67 | + $this->expectExceptionMessage('The "voice" option is required for TextToSpeech requests.'); |
| 68 | + |
| 69 | + $httpClient = new MockHttpClient(); |
| 70 | + $modelClient = new ModelClient($httpClient, 'sk-api-key'); |
| 71 | + $modelClient->request(new TextToSpeech('tts-1'), 'Hello World!', [ |
| 72 | + 'instruction' => 'Speak like a pirate', |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + public function testFailsWithStreamingOptions() |
| 77 | + { |
| 78 | + $this->expectException(InvalidArgumentException::class); |
| 79 | + $this->expectExceptionMessage('Streaming text to speech results is not supported yet.'); |
| 80 | + |
| 81 | + $httpClient = new MockHttpClient(); |
| 82 | + $modelClient = new ModelClient($httpClient, 'sk-api-key'); |
| 83 | + $modelClient->request(new TextToSpeech('tts-1'), 'Hello World!', [ |
| 84 | + 'voice' => 'alloy', |
| 85 | + 'stream' => true, |
| 86 | + ]); |
| 87 | + } |
| 88 | +} |
0 commit comments