@@ -23,9 +23,9 @@ public function testCreateFromUrl(): void
23
23
{
24
24
$ url = 'https://example.com/image.jpg ' ;
25
25
$ mimeType = 'image/jpeg ' ;
26
-
26
+
27
27
$ file = new File ($ url , $ mimeType );
28
-
28
+
29
29
$ this ->assertEquals (FileTypeEnum::remote (), $ file ->getFileType ());
30
30
$ this ->assertEquals ($ url , $ file ->getUrl ());
31
31
$ this ->assertNull ($ file ->getBase64Data ());
@@ -42,9 +42,9 @@ public function testCreateFromUrl(): void
42
42
public function testCreateFromUrlWithInferredMimeType (): void
43
43
{
44
44
$ url = 'https://example.com/document.pdf ' ;
45
-
45
+
46
46
$ file = new File ($ url );
47
-
47
+
48
48
$ this ->assertEquals (FileTypeEnum::remote (), $ file ->getFileType ());
49
49
$ this ->assertEquals ($ url , $ file ->getUrl ());
50
50
$ this ->assertEquals ('application/pdf ' , $ file ->getMimeType ());
@@ -60,9 +60,9 @@ public function testCreateFromDataUri(): void
60
60
{
61
61
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
62
62
$ dataUri = 'data:text/plain;base64, ' . $ base64Data ;
63
-
63
+
64
64
$ file = new File ($ dataUri );
65
-
65
+
66
66
$ this ->assertEquals (FileTypeEnum::inline (), $ file ->getFileType ());
67
67
$ this ->assertNull ($ file ->getUrl ());
68
68
$ this ->assertEquals ($ base64Data , $ file ->getBase64Data ());
@@ -81,9 +81,9 @@ public function testCreateFromDataUriWithMimeTypeOverride(): void
81
81
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
82
82
$ dataUri = 'data:text/plain;base64, ' . $ base64Data ;
83
83
$ overrideMimeType = 'text/html ' ;
84
-
84
+
85
85
$ file = new File ($ dataUri , $ overrideMimeType );
86
-
86
+
87
87
$ this ->assertEquals (FileTypeEnum::inline (), $ file ->getFileType ());
88
88
$ this ->assertEquals ($ base64Data , $ file ->getBase64Data ());
89
89
$ this ->assertEquals ($ overrideMimeType , $ file ->getMimeType ());
@@ -99,9 +99,9 @@ public function testCreateFromPlainBase64(): void
99
99
{
100
100
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
101
101
$ mimeType = 'text/plain ' ;
102
-
102
+
103
103
$ file = new File ($ base64Data , $ mimeType );
104
-
104
+
105
105
$ this ->assertEquals (FileTypeEnum::inline (), $ file ->getFileType ());
106
106
$ this ->assertNull ($ file ->getUrl ());
107
107
$ this ->assertEquals ($ base64Data , $ file ->getBase64Data ());
@@ -117,8 +117,10 @@ public function testCreateFromPlainBase64(): void
117
117
public function testPlainBase64WithoutMimeTypeThrowsException (): void
118
118
{
119
119
$ this ->expectException (InvalidArgumentException::class);
120
- $ this ->expectExceptionMessage ('MIME type is required when providing plain base64 data without data URI format. ' );
121
-
120
+ $ this ->expectExceptionMessage (
121
+ 'MIME type is required when providing plain base64 data without data URI format. '
122
+ );
123
+
122
124
new File ('SGVsbG8gV29ybGQ= ' );
123
125
}
124
126
@@ -132,10 +134,10 @@ public function testCreateFromLocalFile(): void
132
134
// Create a temporary file
133
135
$ tempFile = tempnam (sys_get_temp_dir (), 'test ' );
134
136
file_put_contents ($ tempFile , 'Hello World ' );
135
-
137
+
136
138
try {
137
139
$ file = new File ($ tempFile , 'text/plain ' );
138
-
140
+
139
141
$ this ->assertEquals (FileTypeEnum::inline (), $ file ->getFileType ());
140
142
$ this ->assertNull ($ file ->getUrl ());
141
143
$ this ->assertEquals (base64_encode ('Hello World ' ), $ file ->getBase64Data ());
@@ -154,7 +156,7 @@ public function testInvalidFileFormatThrowsException(): void
154
156
{
155
157
$ this ->expectException (InvalidArgumentException::class);
156
158
$ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
157
-
159
+
158
160
new File ('not-a-valid-file-or-url ' , 'text/plain ' );
159
161
}
160
162
@@ -167,7 +169,7 @@ public function testNonExistentLocalFileThrowsException(): void
167
169
{
168
170
$ this ->expectException (InvalidArgumentException::class);
169
171
$ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
170
-
172
+
171
173
new File ('/path/to/non/existent/file.txt ' , 'text/plain ' );
172
174
}
173
175
@@ -181,11 +183,13 @@ public function testDirectoryThrowsException(): void
181
183
// Create a directory instead of a file
182
184
$ tempDir = sys_get_temp_dir () . '/test_dir_ ' . uniqid ();
183
185
mkdir ($ tempDir );
184
-
186
+
185
187
try {
186
188
$ this ->expectException (InvalidArgumentException::class);
187
- $ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
188
-
189
+ $ this ->expectExceptionMessage (
190
+ 'Invalid file provided. Expected URL, base64 data, or valid local file path. '
191
+ );
192
+
189
193
new File ($ tempDir , 'text/plain ' );
190
194
} finally {
191
195
rmdir ($ tempDir );
@@ -200,7 +204,7 @@ public function testDirectoryThrowsException(): void
200
204
public function testMimeTypeMethods (): void
201
205
{
202
206
$ file = new File ('https://example.com/video.mp4 ' );
203
-
207
+
204
208
$ this ->assertEquals ('video/mp4 ' , $ file ->getMimeType ());
205
209
$ this ->assertInstanceOf (\WordPress \AiClient \Files \ValueObjects \MimeType::class, $ file ->getMimeTypeObject ());
206
210
$ this ->assertTrue ($ file ->isVideo ());
@@ -217,27 +221,33 @@ public function testMimeTypeMethods(): void
217
221
public function testJsonSchema (): void
218
222
{
219
223
$ schema = File::getJsonSchema ();
220
-
224
+
221
225
$ this ->assertIsArray ($ schema );
222
226
$ this ->assertEquals ('object ' , $ schema ['type ' ]);
223
227
$ this ->assertArrayHasKey ('oneOf ' , $ schema );
224
228
$ this ->assertCount (2 , $ schema ['oneOf ' ]);
225
-
229
+
226
230
// Check remote file schema
227
231
$ remoteSchema = $ schema ['oneOf ' ][0 ];
228
232
$ this ->assertArrayHasKey ('properties ' , $ remoteSchema );
229
233
$ this ->assertArrayHasKey (File::KEY_FILE_TYPE , $ remoteSchema ['properties ' ]);
230
234
$ this ->assertArrayHasKey (File::KEY_MIME_TYPE , $ remoteSchema ['properties ' ]);
231
235
$ this ->assertArrayHasKey (File::KEY_URL , $ remoteSchema ['properties ' ]);
232
- $ this ->assertEquals ([File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_URL ], $ remoteSchema ['required ' ]);
233
-
236
+ $ this ->assertEquals (
237
+ [File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_URL ],
238
+ $ remoteSchema ['required ' ]
239
+ );
240
+
234
241
// Check inline file schema
235
242
$ inlineSchema = $ schema ['oneOf ' ][1 ];
236
243
$ this ->assertArrayHasKey ('properties ' , $ inlineSchema );
237
244
$ this ->assertArrayHasKey (File::KEY_FILE_TYPE , $ inlineSchema ['properties ' ]);
238
245
$ this ->assertArrayHasKey (File::KEY_MIME_TYPE , $ inlineSchema ['properties ' ]);
239
246
$ this ->assertArrayHasKey (File::KEY_BASE64_DATA , $ inlineSchema ['properties ' ]);
240
- $ this ->assertEquals ([File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_BASE64_DATA ], $ inlineSchema ['required ' ]);
247
+ $ this ->assertEquals (
248
+ [File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_BASE64_DATA ],
249
+ $ inlineSchema ['required ' ]
250
+ );
241
251
}
242
252
243
253
/**
@@ -249,10 +259,10 @@ public function testDataUriWithoutMimeType(): void
249
259
{
250
260
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
251
261
$ dataUri = 'data:;base64, ' . $ base64Data ;
252
-
262
+
253
263
$ this ->expectException (InvalidArgumentException::class);
254
264
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
255
-
265
+
256
266
new File ($ dataUri );
257
267
}
258
268
@@ -265,7 +275,7 @@ public function testUrlWithUnknownExtension(): void
265
275
{
266
276
$ this ->expectException (InvalidArgumentException::class);
267
277
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
268
-
278
+
269
279
new File ('https://example.com/file.unknown ' );
270
280
}
271
281
@@ -278,7 +288,7 @@ public function testToArrayRemoteFile(): void
278
288
{
279
289
$ file = new File ('https://example.com/image.jpg ' , 'image/jpeg ' );
280
290
$ json = $ file ->toArray ();
281
-
291
+
282
292
$ this ->assertIsArray ($ json );
283
293
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::remote ()->value , $ json [File::KEY_FILE_TYPE ]);
284
294
$ this ->assertEquals ('image/jpeg ' , $ json [File::KEY_MIME_TYPE ]);
@@ -297,7 +307,7 @@ public function testToArrayInlineFile(): void
297
307
$ dataUri = 'data:text/plain;base64, ' . $ base64Data ;
298
308
$ file = new File ($ dataUri );
299
309
$ json = $ file ->toArray ();
300
-
310
+
301
311
$ this ->assertIsArray ($ json );
302
312
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::inline ()->value , $ json [File::KEY_FILE_TYPE ]);
303
313
$ this ->assertEquals ('text/plain ' , $ json [File::KEY_MIME_TYPE ]);
@@ -317,9 +327,9 @@ public function testFromArrayRemoteFile(): void
317
327
File::KEY_MIME_TYPE => 'image/png ' ,
318
328
File::KEY_URL => 'https://example.com/test.png '
319
329
];
320
-
330
+
321
331
$ file = File::fromArray ($ json );
322
-
332
+
323
333
$ this ->assertInstanceOf (File::class, $ file );
324
334
$ this ->assertTrue ($ file ->getFileType ()->isRemote ());
325
335
$ this ->assertEquals ('image/png ' , $ file ->getMimeType ());
@@ -340,9 +350,9 @@ public function testFromArrayInlineFile(): void
340
350
File::KEY_MIME_TYPE => 'text/plain ' ,
341
351
File::KEY_BASE64_DATA => $ base64Data
342
352
];
343
-
353
+
344
354
$ file = File::fromArray ($ json );
345
-
355
+
346
356
$ this ->assertInstanceOf (File::class, $ file );
347
357
$ this ->assertTrue ($ file ->getFileType ()->isInline ());
348
358
$ this ->assertEquals ('text/plain ' , $ file ->getMimeType ());
@@ -361,17 +371,17 @@ public function testArrayRoundTrip(): void
361
371
$ remoteFile = new File ('https://example.com/doc.pdf ' , 'application/pdf ' );
362
372
$ remoteJson = $ remoteFile ->toArray ();
363
373
$ restoredRemote = File::fromArray ($ remoteJson );
364
-
374
+
365
375
$ this ->assertEquals ($ remoteFile ->getFileType ()->value , $ restoredRemote ->getFileType ()->value );
366
376
$ this ->assertEquals ($ remoteFile ->getMimeType (), $ restoredRemote ->getMimeType ());
367
377
$ this ->assertEquals ($ remoteFile ->getUrl (), $ restoredRemote ->getUrl ());
368
-
378
+
369
379
// Test inline file
370
380
$ dataUri = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ' ;
371
381
$ inlineFile = new File ($ dataUri );
372
382
$ inlineJson = $ inlineFile ->toArray ();
373
383
$ restoredInline = File::fromArray ($ inlineJson );
374
-
384
+
375
385
$ this ->assertEquals ($ inlineFile ->getFileType ()->value , $ restoredInline ->getFileType ()->value );
376
386
$ this ->assertEquals ($ inlineFile ->getMimeType (), $ restoredInline ->getMimeType ());
377
387
$ this ->assertEquals ($ inlineFile ->getBase64Data (), $ restoredInline ->getBase64Data ());
@@ -385,11 +395,10 @@ public function testArrayRoundTrip(): void
385
395
public function testImplementsWithArrayTransformationInterface (): void
386
396
{
387
397
$ file = new File ('https://example.com/test.jpg ' );
388
-
398
+
389
399
$ this ->assertInstanceOf (
390
400
\WordPress \AiClient \Common \Contracts \WithArrayTransformationInterface::class,
391
401
$ file
392
402
);
393
-
394
403
}
395
- }
404
+ }
0 commit comments