環境
laravel6
PHPUNITでモックの使い方詳しい方教示頂けると幸いです。
以下のコードでどうにか、例外を発生させて、catchしたいです。
理由はカバレッジのためになります。
storeAsかwhereでMockeryを使用したら出来そうな気がしているのですが、
うまくいかず、アドバイスよろしくお願いします。
PHP
1public function upload(Request $request) 2{ 3$id = Session::get('id'); 4$test_file = $request->file('file'); 5$info = pathinfo($image_file->getClientOriginalName()); 6$filename = md5(id) . '.' . $info['extension']; 7 8try { 9 $image_url = '/image/' . $filename; 10 $image_file->storeAs('image', $filename, 's3'); 11 12 CustomSetting::where('id', $id)->update([ 'custom_image_url' => $image_url, ]); 13} 14catch(\Throwable $e) { 15 Log::error('Custom image upload error : ' . $image_url); 16 Log::error($e->getMessage()); 17 return response()->json([ 'message' => 'failed', ], 500); 18} 19}
PHP
1// これじゃwhereがstaticだからダメなんですよね...多分 2$testDouble = \Mockery::mock( ImageSetting::class ); 3$testDouble->shouldReceive( 'where' )->andReturn( false );

回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。