如何在laravel灯塔中测试带有附加参数的graphql上传突变



正如标题所述,我希望能够使用可用的标量测试文件的上传,同时为相同的突变提供额外的参数。该文档提供了一个关于如何测试Lighthouse文档模拟文件上传的帮助。但我的突变也需要一些额外的动态参数来进行测试。但我不知道如何提供这些额外的参数。我当前设置的一个示例:

$response = $this->multipartGraphQL(['operations' => {
"query": "mutation ($file: Upload!) {registerUser(email: $email, files: [$file]) {courseRegistrationFiles{id}}}",
"variables": {"file": null}
}', 'map' => '{"0": ["variables.file"]}'], [
'0' => UploadedFile::fake()->create('document.pdf', 100)
]);

需要注意的是,$email应该使用伪造的电子邮件地址来表示,这在该片段上方的行中完成。起初,我认为我必须在与定义uploadeFile::fake((相同的括号内提供电子邮件变量,但这导致了一个错误,即方法的这一部分中的参数应为UploaedFile类型。之后,我还尝试简单地将它们添加到map/variables部分,但这会导致param为null。

现在有点不同,您需要3个数组参数,如:

$operations = [
'operationName' => 'upload',
'query' => 'mutation upload ($file: Upload!) {
upload (file: $file)
}',
'variables' => [
'file' => null,
],
];
$map = [
'0' => ['variables.file'],
];
$file = [
'0' => UploadedFile::fake()->create('test.pdf', 500),
];
$this->multipartGraphQL($operations, $map, $file);

以下是可以帮助您的文档:https://lighthouse-php.com/5.2/testing/phpunit.html#simulating-文件上传

最新更新