使用 phpunit 在 cakephp 4 中测试文件更新

问题描述 投票:0回答:0

我正在对我上传的文件进行测试,但在测试中文件被视为一个字符串,我需要一个 Laminas\Diactoros\UploadedFile。我怎样才能模拟这个测试?

这是我的控制器:

        $animal = $this->Animal->patchEntity($animal, $this->request->getData());
         if(!$animal->getErrors){
            $image = $this->request->getData('image_file');
            if($image && ($image->getClientMediaType()!='image/png' && $image->getClientMediaType()!='image/jpg'&& $image->getClientMediaType()!='image/jpeg' && $image->getClientFilename()!="")){

这在我的测试中:

        $_FILES = [
        'image_file' => [
            'name'     => 'student-import-test-1.csv',
            'type'     => 'text/csv',
            'tmp_name' => 'tests/Fixture/csv/',
            'error'    => 0,
            'size'     => 258
        ]
    ];

    $data=[
        'name' => 'AñadirAnimal',
        'image_file'     => 'student-import-test-1.csv',
        'image_file.type'     => 'text/csv',
        'image_file.tmp_name' => 'tests/Fixture/csv/',
        'image_file.error'    => 0,
        'image_file.size'     => 258,
        'specie' => 'dog',
        'chip' => 'no',
        'sex' => 'intact_male',
        'race' => 'cat',
        'age' => 1,
        'information' => 'Es un animal.',
        'state' => 'sick',
        'animal_shelter' => [
            'start_date' => '2022-11-03 10:47:38',
            'end_date' => '2023-05-03 10:47:38',
            'user_id' => 1,
            'animal_id' => 1
        ]
    ];
testing cakephp file-upload phpunit laminas
© www.soinside.com 2019 - 2024. All rights reserved.