0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】モックでzipファイルを返却する方法!

Posted at

泉(@izumin_0401)です。

今回は、Laravelのモックでzipファイルを返却する方法を解説します!

ブログ記事はこちら

【Laravel】モックでzipファイルを返却する方法

public function mock()
{
    $zip      = new \ZipArchive;
    $zip_path = tempnam(sys_get_temp_dir(), 'zip');

    if ($zip->open($zip_path) === true) {
        // zipファイルにcsvファイルを入れてみる
        $csv = implode("\n", [
            'id,name',
            '1,izumi',
        ]);
        $zip->addFromString('sample.csv', $csv);
        $zip->close();
    }

    $zip_file = file_get_contents($zip_path);
    unlink($zip_path);

    $this->app->bind(\App\Services\HogeService::class, function () use ($zip_file) {
        return Mockery::mock(\App\Services\HogeService::class)
            ->shouldReceive('exec')
            ->andReturn([
                'status_code' => 200,
                'body'        => $zip_file,
            ])
            ->getMock();
    });
}

↑みたいな感じで、zipファイルを作成した後にMockeryを使ってzipファイルを返せばOK!

まとめ

「application/zip」ってキモいよね。

ではまた。

最後に

暇つぶしにTwitterブログもやってるので見てね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?