簡體   English   中英

在zf2中測試上傳的文件

[英]Testing uploaded file in zf2

我想在zf2控制器中測試一個采取文件並處理它的動作。 我嘗試了以下代碼來對帖子數據做出反應:

public function testIndexActionCanBeAccessed()
{
    $this->dispatch('/', 'POST', array('test_file' => 'abcdefgh'));
    $this->assertResponseStatusCode(200);
}

問題是'test_file'參數是作為post參數處理的。 所以我的問題是,我怎樣才能實現php / zf2將參數作為文件處理而不是作為后置參數?

我試圖在zf2文檔和文件'Zend \\ Test \\ PHPUnit \\ Controller \\ AbstractHttpControllerTestCase'中找到答案但沒有成功。

// within some AbstractHttpControllerTestCase test method
$upload = new \Zend\Stdlib\Parameters([
    'upload' => [
        'name' => 'blah.blah',
        'type' => 'blah',
        'tmp_name' => '/tmp/blah',
        'error' => 0
    ]
]);
$this->getRequest()->setFiles($upload);
$this->dispatch('/url', 'POST');

然后在控制器中:

/**
 * Be strict on where you get file info! No blind unions.
 */
$data = $request->getPost()->toArray();
$data['upload'] = current($request->getFiles()->toArray());

好的,謝謝Sam提供的鏈接我現在可以測試上傳的文件,所以這里是使用簡單的全局變量$_FILES的工作代碼:

public function testIndexActionCanBeAccessed()
{
    $_FILES['userfile']['name'] = 'testName';
    $_FILES['userfile']['tmp_name'] = 'path/to/fixture';

    $this->dispatch('/', 'POST');
    $this->assertResponseStatusCode(200);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM