@@ -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 ());
@@ -118,7 +118,7 @@ public function testPlainBase64WithoutMimeTypeThrowsException(): void
118
118
{
119
119
$ this ->expectException (InvalidArgumentException::class);
120
120
$ this ->expectExceptionMessage ('MIME type is required when providing plain base64 data without data URI format. ' );
121
-
121
+
122
122
new File ('SGVsbG8gV29ybGQ= ' );
123
123
}
124
124
@@ -132,10 +132,10 @@ public function testCreateFromLocalFile(): void
132
132
// Create a temporary file
133
133
$ tempFile = tempnam (sys_get_temp_dir (), 'test ' );
134
134
file_put_contents ($ tempFile , 'Hello World ' );
135
-
135
+
136
136
try {
137
137
$ file = new File ($ tempFile , 'text/plain ' );
138
-
138
+
139
139
$ this ->assertEquals (FileTypeEnum::inline (), $ file ->getFileType ());
140
140
$ this ->assertNull ($ file ->getUrl ());
141
141
$ this ->assertEquals (base64_encode ('Hello World ' ), $ file ->getBase64Data ());
@@ -154,7 +154,7 @@ public function testInvalidFileFormatThrowsException(): void
154
154
{
155
155
$ this ->expectException (InvalidArgumentException::class);
156
156
$ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
157
-
157
+
158
158
new File ('not-a-valid-file-or-url ' , 'text/plain ' );
159
159
}
160
160
@@ -167,7 +167,7 @@ public function testNonExistentLocalFileThrowsException(): void
167
167
{
168
168
$ this ->expectException (InvalidArgumentException::class);
169
169
$ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
170
-
170
+
171
171
new File ('/path/to/non/existent/file.txt ' , 'text/plain ' );
172
172
}
173
173
@@ -181,11 +181,11 @@ public function testDirectoryThrowsException(): void
181
181
// Create a directory instead of a file
182
182
$ tempDir = sys_get_temp_dir () . '/test_dir_ ' . uniqid ();
183
183
mkdir ($ tempDir );
184
-
184
+
185
185
try {
186
186
$ this ->expectException (InvalidArgumentException::class);
187
187
$ this ->expectExceptionMessage ('Invalid file provided. Expected URL, base64 data, or valid local file path. ' );
188
-
188
+
189
189
new File ($ tempDir , 'text/plain ' );
190
190
} finally {
191
191
rmdir ($ tempDir );
@@ -200,7 +200,7 @@ public function testDirectoryThrowsException(): void
200
200
public function testMimeTypeMethods (): void
201
201
{
202
202
$ file = new File ('https://example.com/video.mp4 ' );
203
-
203
+
204
204
$ this ->assertEquals ('video/mp4 ' , $ file ->getMimeType ());
205
205
$ this ->assertInstanceOf (\WordPress \AiClient \Files \ValueObjects \MimeType::class, $ file ->getMimeTypeObject ());
206
206
$ this ->assertTrue ($ file ->isVideo ());
@@ -217,20 +217,20 @@ public function testMimeTypeMethods(): void
217
217
public function testJsonSchema (): void
218
218
{
219
219
$ schema = File::getJsonSchema ();
220
-
220
+
221
221
$ this ->assertIsArray ($ schema );
222
222
$ this ->assertEquals ('object ' , $ schema ['type ' ]);
223
223
$ this ->assertArrayHasKey ('oneOf ' , $ schema );
224
224
$ this ->assertCount (2 , $ schema ['oneOf ' ]);
225
-
225
+
226
226
// Check remote file schema
227
227
$ remoteSchema = $ schema ['oneOf ' ][0 ];
228
228
$ this ->assertArrayHasKey ('properties ' , $ remoteSchema );
229
229
$ this ->assertArrayHasKey (File::KEY_FILE_TYPE , $ remoteSchema ['properties ' ]);
230
230
$ this ->assertArrayHasKey (File::KEY_MIME_TYPE , $ remoteSchema ['properties ' ]);
231
231
$ this ->assertArrayHasKey (File::KEY_URL , $ remoteSchema ['properties ' ]);
232
232
$ this ->assertEquals ([File::KEY_FILE_TYPE , File::KEY_MIME_TYPE , File::KEY_URL ], $ remoteSchema ['required ' ]);
233
-
233
+
234
234
// Check inline file schema
235
235
$ inlineSchema = $ schema ['oneOf ' ][1 ];
236
236
$ this ->assertArrayHasKey ('properties ' , $ inlineSchema );
@@ -249,10 +249,10 @@ public function testDataUriWithoutMimeType(): void
249
249
{
250
250
$ base64Data = 'SGVsbG8gV29ybGQ= ' ;
251
251
$ dataUri = 'data:;base64, ' . $ base64Data ;
252
-
252
+
253
253
$ this ->expectException (InvalidArgumentException::class);
254
254
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
255
-
255
+
256
256
new File ($ dataUri );
257
257
}
258
258
@@ -265,7 +265,7 @@ public function testUrlWithUnknownExtension(): void
265
265
{
266
266
$ this ->expectException (InvalidArgumentException::class);
267
267
$ this ->expectExceptionMessage ('Unable to determine MIME type. Please provide it explicitly. ' );
268
-
268
+
269
269
new File ('https://example.com/file.unknown ' );
270
270
}
271
271
@@ -278,7 +278,7 @@ public function testToArrayRemoteFile(): void
278
278
{
279
279
$ file = new File ('https://example.com/image.jpg ' , 'image/jpeg ' );
280
280
$ json = $ file ->toArray ();
281
-
281
+
282
282
$ this ->assertIsArray ($ json );
283
283
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::remote ()->value , $ json [File::KEY_FILE_TYPE ]);
284
284
$ this ->assertEquals ('image/jpeg ' , $ json [File::KEY_MIME_TYPE ]);
@@ -297,7 +297,7 @@ public function testToArrayInlineFile(): void
297
297
$ dataUri = 'data:text/plain;base64, ' . $ base64Data ;
298
298
$ file = new File ($ dataUri );
299
299
$ json = $ file ->toArray ();
300
-
300
+
301
301
$ this ->assertIsArray ($ json );
302
302
$ this ->assertEquals (\WordPress \AiClient \Files \Enums \FileTypeEnum::inline ()->value , $ json [File::KEY_FILE_TYPE ]);
303
303
$ this ->assertEquals ('text/plain ' , $ json [File::KEY_MIME_TYPE ]);
@@ -317,9 +317,9 @@ public function testFromArrayRemoteFile(): void
317
317
File::KEY_MIME_TYPE => 'image/png ' ,
318
318
File::KEY_URL => 'https://example.com/test.png '
319
319
];
320
-
320
+
321
321
$ file = File::fromArray ($ json );
322
-
322
+
323
323
$ this ->assertInstanceOf (File::class, $ file );
324
324
$ this ->assertTrue ($ file ->getFileType ()->isRemote ());
325
325
$ this ->assertEquals ('image/png ' , $ file ->getMimeType ());
@@ -340,9 +340,9 @@ public function testFromArrayInlineFile(): void
340
340
File::KEY_MIME_TYPE => 'text/plain ' ,
341
341
File::KEY_BASE64_DATA => $ base64Data
342
342
];
343
-
343
+
344
344
$ file = File::fromArray ($ json );
345
-
345
+
346
346
$ this ->assertInstanceOf (File::class, $ file );
347
347
$ this ->assertTrue ($ file ->getFileType ()->isInline ());
348
348
$ this ->assertEquals ('text/plain ' , $ file ->getMimeType ());
@@ -361,17 +361,17 @@ public function testArrayRoundTrip(): void
361
361
$ remoteFile = new File ('https://example.com/doc.pdf ' , 'application/pdf ' );
362
362
$ remoteJson = $ remoteFile ->toArray ();
363
363
$ restoredRemote = File::fromArray ($ remoteJson );
364
-
364
+
365
365
$ this ->assertEquals ($ remoteFile ->getFileType ()->value , $ restoredRemote ->getFileType ()->value );
366
366
$ this ->assertEquals ($ remoteFile ->getMimeType (), $ restoredRemote ->getMimeType ());
367
367
$ this ->assertEquals ($ remoteFile ->getUrl (), $ restoredRemote ->getUrl ());
368
-
368
+
369
369
// Test inline file
370
370
$ dataUri = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 ' ;
371
371
$ inlineFile = new File ($ dataUri );
372
372
$ inlineJson = $ inlineFile ->toArray ();
373
373
$ restoredInline = File::fromArray ($ inlineJson );
374
-
374
+
375
375
$ this ->assertEquals ($ inlineFile ->getFileType ()->value , $ restoredInline ->getFileType ()->value );
376
376
$ this ->assertEquals ($ inlineFile ->getMimeType (), $ restoredInline ->getMimeType ());
377
377
$ this ->assertEquals ($ inlineFile ->getBase64Data (), $ restoredInline ->getBase64Data ());
@@ -385,11 +385,10 @@ public function testArrayRoundTrip(): void
385
385
public function testImplementsWithArrayTransformationInterface (): void
386
386
{
387
387
$ file = new File ('https://example.com/test.jpg ' );
388
-
388
+
389
389
$ this ->assertInstanceOf (
390
390
\WordPress \AiClient \Common \Contracts \WithArrayTransformationInterface::class,
391
391
$ file
392
392
);
393
-
394
393
}
395
- }
394
+ }
0 commit comments