UploadMutationTest.php in GraphQL 8.3
File
tests/src/Kernel/Framework/UploadMutationTest.php
View source
<?php
namespace Drupal\Tests\graphql\Kernel\Framework;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
use Symfony\Component\HttpFoundation\Request;
class UploadMutationTest extends GraphQLTestBase {
public function testFileUpload() {
$file = \Drupal::service('file_system')
->getTempDirectory() . '/graphql_upload_test.txt';
touch($file);
$this
->mockMutation('store', [
'name' => 'store',
'arguments' => [
'file' => 'Upload!',
],
'type' => 'String',
], function ($value, $args) {
$file = $args['file'];
return $file
->getClientOriginalName();
});
$uploadRequest = Request::create('/graphql', 'POST', [
'query' => 'mutation($upload: Upload!) { store(file: $upload) }',
'variables' => [
'upload' => NULL,
],
'map' => [
'test' => [
'variables.upload',
],
],
], [], [
'test' => [
'name' => 'test.txt',
'type' => 'text/plain',
'size' => 42,
'tmp_name' => $file,
'error' => UPLOAD_ERR_OK,
],
]);
$uploadRequest->headers
->add([
'content-type' => 'multipart/form-data',
]);
$response = $this->container
->get('http_kernel')
->handle($uploadRequest);
$result = json_decode($response
->getContent());
$this
->assertEquals('test.txt', $result->data->store);
}
}