|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\ElevenLabs; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\Platform\Bridge\ElevenLabs\ElevenLabs; |
| 18 | +use Symfony\AI\Platform\Bridge\ElevenLabs\ElevenLabsClient; |
| 19 | +use Symfony\AI\Platform\Contract\Normalizer\Message\Content\AudioNormalizer; |
| 20 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 21 | +use Symfony\AI\Platform\Message\Content\Audio; |
| 22 | +use Symfony\AI\Platform\Model; |
| 23 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 24 | + |
| 25 | +#[CoversClass(ElevenLabsClient::class)] |
| 26 | +#[UsesClass(ElevenLabs::class)] |
| 27 | +#[UsesClass(Model::class)] |
| 28 | +#[UsesClass(Audio::class)] |
| 29 | +#[UsesClass(AudioNormalizer::class)] |
| 30 | +final class ElevenLabsClientTest extends TestCase |
| 31 | +{ |
| 32 | + public function testSupportsModel() |
| 33 | + { |
| 34 | + $client = new ElevenLabsClient( |
| 35 | + new MockHttpClient(), |
| 36 | + 'https://api.elevenlabs.io/v1', |
| 37 | + 'my-api-key', |
| 38 | + ); |
| 39 | + |
| 40 | + $this->assertTrue($client->supports(new ElevenLabs())); |
| 41 | + $this->assertFalse($client->supports(new Model('any-model'))); |
| 42 | + } |
| 43 | + |
| 44 | + public function testClientCannotPerformSpeechToTextRequestWithoutModel() |
| 45 | + { |
| 46 | + $normalizer = new AudioNormalizer(); |
| 47 | + |
| 48 | + $client = new ElevenLabsClient( |
| 49 | + new MockHttpClient(), |
| 50 | + 'https://api.elevenlabs.io/v1', |
| 51 | + 'my-api-key', |
| 52 | + ); |
| 53 | + |
| 54 | + $payload = $normalizer->normalize(Audio::fromFile(dirname(__DIR__, 5).'/fixtures/audio.mp3')); |
| 55 | + |
| 56 | + $this->expectException(InvalidArgumentException::class); |
| 57 | + $this->expectExceptionMessage('The model option is required.'); |
| 58 | + $this->expectExceptionCode(0); |
| 59 | + $client->request(new ElevenLabs(ElevenLabs::SPEECH_TO_TEXT), $payload); |
| 60 | + } |
| 61 | + |
| 62 | + public function testClientCannotPerformSpeechToTextRequestWithInvalidPayload() |
| 63 | + { |
| 64 | + $client = new ElevenLabsClient( |
| 65 | + new MockHttpClient(), |
| 66 | + 'https://api.elevenlabs.io/v1', |
| 67 | + 'my-api-key', |
| 68 | + ); |
| 69 | + |
| 70 | + $this->expectException(InvalidArgumentException::class); |
| 71 | + $this->expectExceptionMessage('The payload must be an array, received "string".'); |
| 72 | + $this->expectExceptionCode(0); |
| 73 | + $client->request(new ElevenLabs(ElevenLabs::SPEECH_TO_TEXT, options: [ |
| 74 | + 'model' => 'bar', |
| 75 | + ]), 'foo'); |
| 76 | + } |
| 77 | + |
| 78 | + public function testClientCanPerformSpeechToTextRequest() |
| 79 | + { |
| 80 | + $httpClient = new MockHttpClient(); |
| 81 | + $normalizer = new AudioNormalizer(); |
| 82 | + |
| 83 | + $client = new ElevenLabsClient( |
| 84 | + $httpClient, |
| 85 | + 'https://api.elevenlabs.io/v1', |
| 86 | + 'my-api-key', |
| 87 | + ); |
| 88 | + |
| 89 | + $payload = $normalizer->normalize(Audio::fromFile(dirname(__DIR__, 5).'/fixtures/audio.mp3')); |
| 90 | + |
| 91 | + $client->request(new ElevenLabs(ElevenLabs::SPEECH_TO_TEXT, options: [ |
| 92 | + 'model' => 'bar', |
| 93 | + ]), $payload); |
| 94 | + |
| 95 | + $this->assertSame(1, $httpClient->getRequestsCount()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testClientCannotPerformTextToSpeechRequestWithoutModel() |
| 99 | + { |
| 100 | + $client = new ElevenLabsClient( |
| 101 | + new MockHttpClient(), |
| 102 | + 'https://api.elevenlabs.io/v1', |
| 103 | + 'my-api-key', |
| 104 | + ); |
| 105 | + |
| 106 | + $this->expectException(InvalidArgumentException::class); |
| 107 | + $this->expectExceptionMessage('The model option is required.'); |
| 108 | + $this->expectExceptionCode(0); |
| 109 | + $client->request(new ElevenLabs(), []); |
| 110 | + } |
| 111 | + |
| 112 | + public function testClientCannotPerformTextToSpeechRequestWithInvalidPayload() |
| 113 | + { |
| 114 | + $client = new ElevenLabsClient( |
| 115 | + new MockHttpClient(), |
| 116 | + 'https://api.elevenlabs.io/v1', |
| 117 | + 'my-api-key', |
| 118 | + ); |
| 119 | + |
| 120 | + $this->expectException(InvalidArgumentException::class); |
| 121 | + $this->expectExceptionMessage('The payload must be an array, received "string".'); |
| 122 | + $this->expectExceptionCode(0); |
| 123 | + $client->request(new ElevenLabs(options: [ |
| 124 | + 'model' => 'bar', |
| 125 | + ]), 'foo'); |
| 126 | + } |
| 127 | + |
| 128 | + public function testClientCannotPerformTextToSpeechRequestWithoutValidPayload() |
| 129 | + { |
| 130 | + $normalizer = new AudioNormalizer(); |
| 131 | + |
| 132 | + $client = new ElevenLabsClient( |
| 133 | + new MockHttpClient(), |
| 134 | + 'https://api.elevenlabs.io/v1', |
| 135 | + 'my-api-key', |
| 136 | + ); |
| 137 | + |
| 138 | + $this->expectException(InvalidArgumentException::class); |
| 139 | + $this->expectExceptionMessage('The payload must contain a "text" key'); |
| 140 | + $this->expectExceptionCode(0); |
| 141 | + $client->request(new ElevenLabs(options: [ |
| 142 | + 'model' => 'bar', |
| 143 | + ]), []); |
| 144 | + } |
| 145 | + |
| 146 | + public function testClientCanPerformTextToSpeechRequest() |
| 147 | + { |
| 148 | + $httpClient = new MockHttpClient(); |
| 149 | + |
| 150 | + $client = new ElevenLabsClient( |
| 151 | + $httpClient, |
| 152 | + 'https://api.elevenlabs.io/v1', |
| 153 | + 'my-api-key', |
| 154 | + ); |
| 155 | + |
| 156 | + $client->request(new ElevenLabs(options: [ |
| 157 | + 'model' => 'bar', |
| 158 | + ]), [ |
| 159 | + 'text' => 'foo', |
| 160 | + ]); |
| 161 | + |
| 162 | + $this->assertSame(1, $httpClient->getRequestsCount()); |
| 163 | + } |
| 164 | +} |
0 commit comments