Skip to content

Commit d2a328b

Browse files
authored
feat: Add documentation to test file uploads (#1362)
1 parent 8cd89d0 commit d2a328b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

core/file-upload.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,48 @@ uploaded cover, you can have a nice illustrated book record!
297297

298298
Voilà! You can now send files to your API, and link them to any other resource
299299
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

Comments
 (0)