12
12
namespace Symfony \AI \Platform \Tests \Bridge \ElevenLabs ;
13
13
14
14
use PHPUnit \Framework \Attributes \CoversClass ;
15
+ use PHPUnit \Framework \Attributes \Group ;
15
16
use PHPUnit \Framework \Attributes \UsesClass ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
use Symfony \AI \Platform \Bridge \ElevenLabs \ElevenLabs ;
21
22
use Symfony \AI \Platform \Message \Content \Audio ;
22
23
use Symfony \AI \Platform \Model ;
23
24
use Symfony \Component \HttpClient \MockHttpClient ;
25
+ use Symfony \Component \HttpClient \Response \JsonMockResponse ;
26
+ use Symfony \Component \HttpClient \Response \MockResponse ;
24
27
25
28
#[CoversClass(ElevenLabsClient::class)]
26
29
#[UsesClass(ElevenLabs::class)]
@@ -43,18 +46,27 @@ public function testSupportsModel()
43
46
44
47
public function testClientCannotPerformWithInvalidModel ()
45
48
{
49
+ $ mockHttpClient = new MockHttpClient ([
50
+ new JsonMockResponse ([
51
+ [
52
+ 'model_id ' => 'bar ' ,
53
+ 'can_do_text_to_speech ' => false ,
54
+ ],
55
+ ]),
56
+ new JsonMockResponse ([]),
57
+ ]);
46
58
$ normalizer = new AudioNormalizer ();
47
59
48
60
$ client = new ElevenLabsClient (
49
- new MockHttpClient () ,
61
+ $ mockHttpClient ,
50
62
'my-api-key ' ,
51
63
'https://api.elevenlabs.io/v1 ' ,
52
64
);
53
65
54
66
$ payload = $ normalizer ->normalize (Audio::fromFile (\dirname (__DIR__ , 5 ).'/fixtures/audio.mp3 ' ));
55
67
56
68
$ this ->expectException (InvalidArgumentException::class);
57
- $ this ->expectExceptionMessage ('The model option is required . ' );
69
+ $ this ->expectExceptionMessage ('The model information could not be retrieved from the ElevenLabs API. Your model might not be supported. Try to use another one . ' );
58
70
$ this ->expectExceptionCode (0 );
59
71
$ client ->request (new ElevenLabs ('foo ' ), $ payload );
60
72
}
@@ -70,14 +82,16 @@ public function testClientCannotPerformSpeechToTextRequestWithInvalidPayload()
70
82
$ this ->expectException (InvalidArgumentException::class);
71
83
$ this ->expectExceptionMessage ('The payload must be an array, received "string". ' );
72
84
$ this ->expectExceptionCode (0 );
73
- $ client ->request (new ElevenLabs (ElevenLabs::SPEECH_TO_TEXT , options: [
74
- 'model ' => 'bar ' ,
75
- ]), 'foo ' );
85
+ $ client ->request (new ElevenLabs (ElevenLabs::ELEVEN_MULTILINGUAL_V2 ), 'foo ' );
76
86
}
77
87
78
88
public function testClientCanPerformSpeechToTextRequest ()
79
89
{
80
- $ httpClient = new MockHttpClient ();
90
+ $ httpClient = new MockHttpClient ([
91
+ new JsonMockResponse ([
92
+ 'text ' => 'foo ' ,
93
+ ]),
94
+ ]);
81
95
$ normalizer = new AudioNormalizer ();
82
96
83
97
$ client = new ElevenLabsClient (
@@ -88,49 +102,25 @@ public function testClientCanPerformSpeechToTextRequest()
88
102
89
103
$ payload = $ normalizer ->normalize (Audio::fromFile (\dirname (__DIR__ , 5 ).'/fixtures/audio.mp3 ' ));
90
104
91
- $ client ->request (new ElevenLabs (ElevenLabs::SPEECH_TO_TEXT , options: [
92
- 'model ' => 'bar ' ,
93
- ]), $ payload );
105
+ $ client ->request (new ElevenLabs (ElevenLabs::SCRIBE_V1 ), $ payload );
94
106
95
107
$ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
96
108
}
97
109
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
110
public function testClientCannotPerformTextToSpeechRequestWithoutValidPayload ()
129
111
{
130
- $ normalizer = new AudioNormalizer ();
112
+ $ mockHttpClient = new MockHttpClient ([
113
+ new JsonMockResponse ([
114
+ [
115
+ 'model_id ' => ElevenLabs::ELEVEN_MULTILINGUAL_V2 ,
116
+ 'can_do_text_to_speech ' => true ,
117
+ ],
118
+ ]),
119
+ new JsonMockResponse ([]),
120
+ ]);
131
121
132
122
$ client = new ElevenLabsClient (
133
- new MockHttpClient () ,
123
+ $ mockHttpClient ,
134
124
'https://api.elevenlabs.io/v1 ' ,
135
125
'my-api-key ' ,
136
126
);
@@ -139,13 +129,24 @@ public function testClientCannotPerformTextToSpeechRequestWithoutValidPayload()
139
129
$ this ->expectExceptionMessage ('The payload must contain a "text" key ' );
140
130
$ this ->expectExceptionCode (0 );
141
131
$ client ->request (new ElevenLabs (options: [
142
- 'model ' => 'bar ' ,
132
+ 'voice ' => 'Dslrhjl3ZpzrctukrQSN ' ,
143
133
]), []);
144
134
}
145
135
136
+ #[Group('foo ' )]
146
137
public function testClientCanPerformTextToSpeechRequest ()
147
138
{
148
- $ httpClient = new MockHttpClient ();
139
+ $ payload = Audio::fromFile (\dirname (__DIR__ , 5 ).'/fixtures/audio.mp3 ' );
140
+
141
+ $ httpClient = new MockHttpClient ([
142
+ new JsonMockResponse ([
143
+ [
144
+ 'model_id ' => ElevenLabs::ELEVEN_MULTILINGUAL_V2 ,
145
+ 'can_do_text_to_speech ' => true ,
146
+ ],
147
+ ]),
148
+ new MockResponse ($ payload ->asBinary ()),
149
+ ]);
149
150
150
151
$ client = new ElevenLabsClient (
151
152
$ httpClient ,
@@ -154,11 +155,11 @@ public function testClientCanPerformTextToSpeechRequest()
154
155
);
155
156
156
157
$ client ->request (new ElevenLabs (options: [
157
- 'model ' => 'bar ' ,
158
+ 'voice ' => 'Dslrhjl3ZpzrctukrQSN ' ,
158
159
]), [
159
160
'text ' => 'foo ' ,
160
161
]);
161
162
162
- $ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
163
+ $ this ->assertSame (2 , $ httpClient ->getRequestsCount ());
163
164
}
164
165
}
0 commit comments