Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php lumen 5.5中的测试文件上载_Php_Unit Testing_Testing_Lumen - Fatal编程技术网

Php lumen 5.5中的测试文件上载

Php lumen 5.5中的测试文件上载,php,unit-testing,testing,lumen,Php,Unit Testing,Testing,Lumen,我用的是Lumen 5.5,我写了一个上传文件的简单应用程序 我编写了这样的测试(以下是教程) 我检查了其他问题(如)并尝试了给定的解决方案,但没有成功 我在寻找同一问题的答案时遇到了这个问题,不确定它是否相关,所以我提出了一个与我的用例相关的问题 解决方案很简单:UploadedFile::fake()不适用于JSON,因为它使用XmlHttpRequest(据我所知)伪造文件上载。因此,您必须从以下内容更改测试: public function testUploadingImageSucce

我用的是Lumen 5.5,我写了一个上传文件的简单应用程序

我编写了这样的测试(以下是教程)


我检查了其他问题(如)并尝试了给定的解决方案,但没有成功

我在寻找同一问题的答案时遇到了这个问题,不确定它是否相关,所以我提出了一个与我的用例相关的问题

解决方案很简单:UploadedFile::fake()不适用于JSON,因为它使用XmlHttpRequest(据我所知)伪造文件上载。因此,您必须从以下内容更改测试:

public function testUploadingImageSuccessfully()
{
    $this->json('POST', '/images', [
        'image' => UploadedFile::fake()->image('image.jpg')
    ])->assertResponseOk()
}
为此:

public function testUploadingImageSuccessfully()
{
    $this->call('POST', '/images', [
        'image' => UploadedFile::fake()->image('image.jpg')
    ])->assertResponseOk()
}

希望有帮助

请注意,在
call()
方法的第二个参数中传递文件参数无效。因为第二个参数是有效负载数据

如果将其作为数据传递,则在后端使用如下命令时:

if($request->hasFile('my_file')){
//在这里做一些逻辑
}
$request->hasFile()
将始终返回false

您需要在第5个参数中传递假文件上载,才能使其生效

这是
调用的方法签名

call($method,$uri,$parameters=[],$cookies=[],$files=[],$server=[],$content=null)

这对我很有用。很简单。这可以提交文件上传

1.未经认证的条件

5*1000
是文件大小(KB)。所以我用
5MB
文件做了一个测试

use Faker\Factory as Faker;
use Illuminate\Http\UploadedFile;

class SampleTest extends TestCase
{
    public function testItCanCreateUser()
    {
        $faker = Faker::create();
        $files = [
            'file' => UploadedFile::fake()->create('file.jpg', 5*1000)
        ];
        $response = $this->call('POST', '/chunk', [], [], $files);
        $this->assertEquals(200, $response->getStatusCode());
    }
}
2.具有身份验证的条件(登录用户)

您需要在每个请求头上添加
HTTP.
。我不知道为什么。但它会起作用

public function testUploadingImageSuccessfully()
{
    $this->call('POST', '/images', [
        'image' => UploadedFile::fake()->image('image.jpg')
    ])->assertResponseOk()
}
use Faker\Factory as Faker;
use Illuminate\Http\UploadedFile;

class SampleTest extends TestCase
{
    public function testItCanCreateUser()
    {
        $faker = Faker::create();
        $files = [
            'file' => UploadedFile::fake()->create('file.jpg', 5*1000)
        ];
        $response = $this->call('POST', '/chunk', [], [], $files);
        $this->assertEquals(200, $response->getStatusCode());
    }
}
use Faker\Factory as Faker;
use Illuminate\Http\UploadedFile;

class SampleTest extends TestCase
{
    public function testItCanUpdateProfileUser()
    {
        $faker = Faker::create();
        $files = [
            'file' => UploadedFile::fake()->create('file.jpg', 5*1000)
        ];

        $headers = [
            'Accept' => 'application/json',
            'Authorization' => 'your-jwt-token'
        ];
        $servers = [];
        foreach ($headers as $k => $header) {
            $servers["HTTP_" . $k] = $header;
        }

        $response = $this->call('POST', '/chunk', [], [], $files, $servers);
        $this->assertEquals(200, $response->getStatusCode());
    }
}