LoginSignup
0
0

More than 3 years have passed since last update.

【CakePHP3】【PHPUnit】コントローラーのテストでファイルをPOSTする方法

Posted at

半日つぶしたので備忘録として。

POSTデータと$_FILESにそれぞれ画像の情報を入れれば正常に送れる。
(おそらく実践ではcsrf_tokenが必要になるはず)

public function testIndex()
{
    $fileKey = 'image_key';
    $fileInfo = [
        'name' => 'image_name.jpg',
        'tmp_name' => 'path/to/tmp_image_name.jpg',
        'type' => '%拡張子と同じ形式%',
        'error' => UPLOAD_ERR_OK,
        'size' => '%ファイルサイズ%',
    ]
    $_FILES = [
        $fileKey => $fileInfo,
    ];
    $this->post('index', [$fileKey => $fileInfo]);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0