@@ -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 ());
@@ -221,27 +225,33 @@ public function testMimeTypeMethods(): void
221
225
public function testJsonSchema (): void
222
226
{
223
227
$ schema = File::getJsonSchema ();
224
-
228
+
225
229
$ this ->assertIsArray ($ schema );
226
230
$ this ->assertEquals ('object ' , $ schema ['type ' ]);
227
231
$ this ->assertArrayHasKey ('oneOf ' , $ schema );
228
232
$ this ->assertCount (2 , $ schema ['oneOf ' ]);
229
-
233
+
230
234
// Check remote file schema
231
235
$ remoteSchema = $ schema ['oneOf ' ][0 ];
232
236
$ this ->assertArrayHasKey ('properties ' , $ remoteSchema );
233
237
$ this ->assertArrayHasKey (File::KEY_FILE_TYPE , $ remoteSchema ['properties ' ]);
234
238
$ this ->assertArrayHasKey (File::KEY_MIME_TYPE , $ remoteSchema ['properties ' ]);
235
239
$ this ->assertArrayHasKey (File::KEY_URL , $ remoteSchema ['properties ' ]);
236
- $ this ->assertEquals ([File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_URL ], $ remoteSchema ['required ' ]);
237
-
240
+ $ this ->assertEquals (
241
+ [File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_URL ],
242
+ $ remoteSchema ['required ' ]
243
+ );
244
+
238
245
// Check inline file schema
239
246
$ inlineSchema = $ schema ['oneOf ' ][1 ];
240
247
$ this ->assertArrayHasKey ('properties ' , $ inlineSchema );
241
248
$ this ->assertArrayHasKey (File::KEY_FILE_TYPE , $ inlineSchema ['properties ' ]);
242
249
$ this ->assertArrayHasKey (File::KEY_MIME_TYPE , $ inlineSchema ['properties ' ]);
243
250
$ this ->assertArrayHasKey (File::KEY_BASE64_DATA , $ inlineSchema ['properties ' ]);
244
- $ this ->assertEquals ([File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_BASE64_DATA ], $ inlineSchema ['required ' ]);
251
+ $ this ->assertEquals (
252
+ [File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_BASE64_DATA ],
253
+ $ inlineSchema ['required ' ]
254
+ );
245
255
}
246
256
247
257
/**
@@ -253,10 +263,10 @@ public function testDataUriWithoutMimeType(): void
253
263
{
254
264
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
255
265
$ dataUri = 'data:;base64, ' . $ base64Data ;
256
-
266
+
257
267
$ this ->expectException (InvalidArgumentException::class);
258
268
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
259
-
269
+
260
270
new File ($ dataUri );
261
271
}
262
272
@@ -269,7 +279,7 @@ public function testUrlWithUnknownExtension(): void
269
279
{
270
280
$ this ->expectException (InvalidArgumentException::class);
271
281
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
272
-
282
+
273
283
new File ('https://example.com/file.unknown ' );
274
284
}
275
285
@@ -282,7 +292,7 @@ public function testToArrayRemoteFile(): void
282
292
{
283
293
$ file = new File ('https://example.com/image.jpg ' , 'image/jpeg ' );
284
294
$ json = $ file ->toArray ();
285
-
295
+
286
296
$ this ->assertIsArray ($ json );
287
297
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::remote ()->value , $ json [File::KEY_FILE_TYPE ]);
288
298
$ this ->assertEquals ('image/jpeg ' , $ json [File::KEY_MIME_TYPE ]);
@@ -301,7 +311,7 @@ public function testToArrayInlineFile(): void
301
311
$ dataUri = 'data:text/plain;base64, ' . $ base64Data ;
302
312
$ file = new File ($ dataUri );
303
313
$ json = $ file ->toArray ();
304
-
314
+
305
315
$ this ->assertIsArray ($ json );
306
316
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::inline ()->value , $ json [File::KEY_FILE_TYPE ]);
307
317
$ this ->assertEquals ('text/plain ' , $ json [File::KEY_MIME_TYPE ]);
@@ -321,9 +331,9 @@ public function testFromArrayRemoteFile(): void
321
331
File::KEY_MIME_TYPE => 'image/png ' ,
322
332
File::KEY_URL => 'https://example.com/test.png '
323
333
];
324
-
334
+
325
335
$ file = File::fromArray ($ json );
326
-
336
+
327
337
$ this ->assertInstanceOf (File::class, $ file );
328
338
$ this ->assertTrue ($ file ->getFileType ()->isRemote ());
329
339
$ this ->assertEquals ('image/png ' , $ file ->getMimeType ());
@@ -344,9 +354,9 @@ public function testFromArrayInlineFile(): void
344
354
File::KEY_MIME_TYPE => 'text/plain ' ,
345
355
File::KEY_BASE64_DATA => $ base64Data
346
356
];
347
-
357
+
348
358
$ file = File::fromArray ($ json );
349
-
359
+
350
360
$ this ->assertInstanceOf (File::class, $ file );
351
361
$ this ->assertTrue ($ file ->getFileType ()->isInline ());
352
362
$ this ->assertEquals ('text/plain ' , $ file ->getMimeType ());
@@ -365,17 +375,17 @@ public function testArrayRoundTrip(): void
365
375
$ remoteFile = new File ('https://example.com/doc.pdf ' , 'application/pdf ' );
366
376
$ remoteJson = $ remoteFile ->toArray ();
367
377
$ restoredRemote = File::fromArray ($ remoteJson );
368
-
378
+
369
379
$ this ->assertEquals ($ remoteFile ->getFileType ()->value , $ restoredRemote ->getFileType ()->value );
370
380
$ this ->assertEquals ($ remoteFile ->getMimeType (), $ restoredRemote ->getMimeType ());
371
381
$ this ->assertEquals ($ remoteFile ->getUrl (), $ restoredRemote ->getUrl ());
372
-
382
+
373
383
// Test inline file
374
384
$ dataUri = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ' ;
375
385
$ inlineFile = new File ($ dataUri );
376
386
$ inlineJson = $ inlineFile ->toArray ();
377
387
$ restoredInline = File::fromArray ($ inlineJson );
378
-
388
+
379
389
$ this ->assertEquals ($ inlineFile ->getFileType ()->value , $ restoredInline ->getFileType ()->value );
380
390
$ this ->assertEquals ($ inlineFile ->getMimeType (), $ restoredInline ->getMimeType ());
381
391
$ this ->assertEquals ($ inlineFile ->getBase64Data (), $ restoredInline ->getBase64Data ());
@@ -389,11 +399,10 @@ public function testArrayRoundTrip(): void
389
399
public function testImplementsWithArrayTransformationInterface (): void
390
400
{
391
401
$ file = new File ('https://example.com/test.jpg ' );
392
-
402
+
393
403
$ this ->assertInstanceOf (
394
404
\WordPress \AiClient \Common \Contracts \WithArrayTransformationInterface::class,
395
405
$ file
396
406
);
397
-
398
407
}
399
- }
408
+ }
0 commit comments