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