UploadMutationTest.php in GraphQL 8.4
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() : void {
$schema = <<<GQL
schema {
mutation: Mutation
}
type Mutation {
store(file: Upload!): String
}
scalar Upload
GQL;
$this
->setUpSchema($schema);
$file = \Drupal::service('file_system')
->getTempDirectory() . '/graphql_upload_test.txt';
touch($file);
$this
->mockResolver('Mutation', 'store', function ($parent, $args) {
return $args['file']
->getClientOriginalName();
});
$uploadRequest = Request::create('/graphql/test', 'POST', [
'query' => 'mutation($upload: Upload!) { store(file: $upload) }',
'variables' => [
'upload' => NULL,
],
'map' => [
'file' => [
'variables.upload',
],
],
], [], [
'file' => [
'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);
}
}