@@ -297,3 +297,48 @@ uploaded cover, you can have a nice illustrated book record!
297
297
298
298
Voilà! You can now send files to your API, and link them to any other resource
299
299
in your app.
300
+
301
+ ## Testing
302
+
303
+ To test your upload with ` ApiTestCase ` , you can write a method as below:
304
+
305
+ ``` php
306
+ <?php
307
+ // tests/MediaObjectTest.php
308
+
309
+ namespace App\Tests;
310
+
311
+ use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
312
+ use App\Entity\MediaObject;
313
+ use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
314
+ use Symfony\Component\HttpFoundation\File\UploadedFile;
315
+
316
+ class MediaObjectTest extends ApiTestCase
317
+ {
318
+ use RefreshDatabaseTrait;
319
+
320
+ public function testCreateAMediaObject(): void
321
+ {
322
+ $file = new UploadedFile('fixtures/files/image.png', 'image.png');
323
+ $client = self::createClient();
324
+
325
+ $client->request('POST', '/media_objects', [
326
+ 'headers' => ['Content-Type' => 'multipart/form-data'],
327
+ 'extra' => [
328
+ // If you have additional fields in your MediaObject entity, use the parameters.
329
+ 'parameters' => [
330
+ 'title' => 'My file uploaded',
331
+ ],
332
+ 'files' => [
333
+ 'file' => $file,
334
+ ],
335
+ ]
336
+ ]);
337
+ $this->assertResponseIsSuccessful();
338
+ $this->assertMatchesResourceItemJsonSchema(MediaObject::class);
339
+ $this->assertJsonContains([
340
+ 'title' => 'My file uploaded',
341
+ ]);
342
+ }
343
+ }
344
+ ```
0 commit comments