13
13
14
14
use PHPUnit \Framework \Attributes \CoversClass ;
15
15
use PHPUnit \Framework \Attributes \Small ;
16
+ use PHPUnit \Framework \Attributes \TestWith ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
use Symfony \AI \Platform \Bridge \OpenAi \Whisper ;
18
19
use Symfony \AI \Platform \Bridge \OpenAi \Whisper \ModelClient ;
19
20
use Symfony \AI \Platform \Bridge \OpenAi \Whisper \Task ;
21
+ use Symfony \AI \Platform \Exception \InvalidArgumentException ;
20
22
use Symfony \Component \HttpClient \MockHttpClient ;
21
23
use Symfony \Component \HttpClient \Response \MockResponse ;
22
24
23
25
#[CoversClass(ModelClient::class)]
24
26
#[Small]
25
27
final class ModelClientTest extends TestCase
26
28
{
29
+ public function testItThrowsExceptionWhenApiKeyIsEmpty ()
30
+ {
31
+ $ this ->expectException (InvalidArgumentException::class);
32
+ $ this ->expectExceptionMessage ('The API key must not be empty. ' );
33
+
34
+ new ModelClient (new MockHttpClient (), '' );
35
+ }
36
+
37
+ #[TestWith(['api-key-without-prefix ' ])]
38
+ #[TestWith(['pk-api-key ' ])]
39
+ #[TestWith(['SK-api-key ' ])]
40
+ #[TestWith(['skapikey ' ])]
41
+ #[TestWith(['sk api-key ' ])]
42
+ #[TestWith(['sk ' ])]
43
+ public function testItThrowsExceptionWhenApiKeyDoesNotStartWithSk (string $ invalidApiKey )
44
+ {
45
+ $ this ->expectException (InvalidArgumentException::class);
46
+ $ this ->expectExceptionMessage ('The API key must start with "sk-". ' );
47
+
48
+ new ModelClient (new MockHttpClient (), $ invalidApiKey );
49
+ }
50
+
51
+ public function testItAcceptsValidApiKey ()
52
+ {
53
+ $ modelClient = new ModelClient (new MockHttpClient (), 'sk-valid-api-key ' );
54
+
55
+ $ this ->assertInstanceOf (ModelClient::class, $ modelClient );
56
+ }
57
+
27
58
public function testItSupportsWhisperModel ()
28
59
{
29
- $ client = new ModelClient (new MockHttpClient (), 'test-key ' );
60
+ $ client = new ModelClient (new MockHttpClient (), 'sk- test-key ' );
30
61
$ this ->assertTrue ($ client ->supports (new Whisper ()));
31
62
}
32
63
@@ -41,7 +72,7 @@ function ($method, $url): MockResponse {
41
72
},
42
73
]);
43
74
44
- $ client = new ModelClient ($ httpClient , 'test-key ' );
75
+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
45
76
$ client ->request (new Whisper (), ['file ' => 'audio-data ' ]);
46
77
47
78
$ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
@@ -58,7 +89,7 @@ function ($method, $url): MockResponse {
58
89
},
59
90
]);
60
91
61
- $ client = new ModelClient ($ httpClient , 'test-key ' );
92
+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
62
93
$ client ->request (new Whisper (), ['file ' => 'audio-data ' ], ['task ' => Task::TRANSCRIPTION ]);
63
94
64
95
$ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
@@ -75,7 +106,7 @@ function ($method, $url): MockResponse {
75
106
},
76
107
]);
77
108
78
- $ client = new ModelClient ($ httpClient , 'test-key ' );
109
+ $ client = new ModelClient ($ httpClient , 'sk- test-key ' );
79
110
$ client ->request (new Whisper (), ['file ' => 'audio-data ' ], ['task ' => Task::TRANSLATION ]);
80
111
81
112
$ this ->assertSame (1 , $ httpClient ->getRequestsCount ());
0 commit comments