CakePHP 3.7 - 测试用例文件上传

问题描述 投票:1回答:2

如何在CakePHP 3中使用控制器测试用例测试文件上传功能?

我一直遇到PHP认为文件实际上没有上传的问题。适用于浏览器测试的验证规则,但不适用于测试用例:

                ->add('file', [
                    'is_uploaded_file' => [
                        'rule' => ['uploadedFile', ['optional' => false]],
                        'message' => 'File is no valid uploaded file'
                   ],

我很快发现is_uploaded_filemove_uploaded_file不可能在单元测试中愚弄。

但是,关于这个的大多数主题都是旧的和/或没有具体的CakePHP,所以我想我会发布一个新的问题。

cakephp file-upload phpunit cakephp-3.0
2个回答
1
投票

您不一定需要修改验证规则,您可以做的是使用实现\Psr\Http\Message\UploadedFileInterface的对象。 CakePHP的默认上传文件验证支持此类对象。

CakePHP需要zendframework/zend-diactoros,所以你可以使用\Zend\Diactoros\UploadedFile并在你的测试中做这样的事情:

$data = [
    // ...
    'file' => new \Zend\Diactoros\UploadedFile([
        '/path/to/the/temporary/file/on/disk',
        1234, // filesize in bytes
        \UPLOAD_ERR_OK, // upload (error) status
        'filename.jpg', // upload filename
        'image/jpeg' // upload mime type
    ])
];

uploadedFile规则会自动将此类对象视为上传文件。

当然,处理文件上传的代码也必须支持该接口,但它并不复杂,您只需要确保将常规文件上载数组转换为UploadedFileInterface实现,以便您的上传处理程序可以满足要求。

它当然可以在上传处理程序本身中完成,因此验证将使用常规文件上载数组以及UploadedFile对象。另一种方法是在创建实体时使用beforeMarshal处理程序/事件更早地转换它们,这有点像这样:

public function beforeMarshal(\Cake\Event\Event $event, \ArrayObject $data, \ArrayObject $options)
{
    $file = \Cake\Utility\Hash::get($data, 'file');
    if ($file === null) {
        return;
    }

    if (!($file instanceof \Psr\Http\Message\UploadedFileInterface)) {
        $file = new \Zend\Diactoros\UploadedFile(
            \Cake\Utility\Hash::get($file, 'tmp_name'),
            \Cake\Utility\Hash::get($file, 'size'),
            \Cake\Utility\Hash::get($file, 'error'),
            \Cake\Utility\Hash::get($file, 'name'),
            \Cake\Utility\Hash::get($file, 'type')
        );
        $data['file'] = $file;
    }
}

如果您随后使用\Psr\Http\Message\UploadedFileInterface::moveTo()移动文件,它将在SAPI(基于浏览器)以及非SAPI(CLI)环境中工作:

try {
    $file->moveTo($targetPath);
} catch (\Exception $exception) {
    $entity->setError(
        'file', [__('The file could not be moved to its destination.')]
    );
}

也可以看看


0
投票

我发布后几乎立刻就知道了。 该解决方案基于https://pierrerambaud.com/blog/php/2012-12-29-testing-upload-file-with-php

因此解决问题的唯一方法是覆盖内置函数:is_uploaded_filemove_uploaded_file

uploadedFile验证规则存在于Cake\Validation内部,我在表事件中使用move函数,因此在App\Model\Table中。

我在控制器测试用例的顶部添加了以下内容:

<?php

namespace Cake\Validation;

function is_uploaded_file($filename)
{
    return true;
}

namespace App\Model\Table;

function move_uploaded_file($filename, $destination)
{
    return copy($filename, $destination);
}

namespace App\Test\TestCase\Controller;

use App\Controller\CarsController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
use Cake\Core\Configure;

/**
 * App\Controller\CarsController Test Case
 */
class CarsControllerTest extends BaseTestCase
{

    use IntegrationTestTrait;

    // ...

它的工作原理!

© www.soinside.com 2019 - 2024. All rights reserved.