2017-10-19 8 views
0

単体テストのためにLaravelフレームワークをphpunitで使用する。PHPUnitテスト例外がスローされないExceptionException

ファイルを書き込むためにディレクトリを作成する必要がある関数を使用しています。要するに、関数はデータを取得し、一時ファイルに書き込んだり一時ファイルを移動したりします。ディレクトリが欠落しているときErrorExceptionがスローされた場合、このテストをテストする

public function getDataAndStoreToCSVFile() { 
    Log::info(date('Y-m-d H:i:s') . " -> " . __FILE__ . "::" . __FUNCTION__); 
    try { 
     // make sure directories exist 
     if (!Storage::has($this->temporary_directory) || !Storage::has($this->storage_directory)) { 
      $this->createDirectories(); 
     } 

     // get full path of storage disk for files 
     $diskPath = Storage::getAdapter()->getPathPrefix(); 

     // create a complete path to temporary file to allow tempnam to find the directory 
     $full_temporary_file_path = $diskPath.$this->temporary_directory; 

     // fetch stations, station networks and station params seperated by double new line, 
     // will return FALSE if something is missing and file will not be created and not written to 
     if($stations_data_array = $this->getCompleteStationsDataArray("\n\n")){ 

      // create temporary file 
      $temporary_file = tempnam($full_temporary_file_path,'') ; 

      // if both $temporary_file and $stations_data_array exist write entries to file one at a time in CSV format 
      if (file_exists($temporary_file)) { 
       $fp = fopen($temporary_file, 'a'); 
       foreach ($stations_data_array as $fields) { 
        if (is_object($fields) || is_array($fields)) { 

         // $fields is an array 
         $fields = (array)$fields; 
         fputcsv($fp, $fields); 
        } else { 

         // $fields is the separator 
         fwrite($fp, $fields); 
        } 
       } 

       // done writing, close file 
       fclose($fp); 

       // create new permanent name for $temporary_file in the storage directory "full_disk_path.storage_path.yyyymmddhhmmss.timestamp" 
       $storage_file = $diskPath . $this->storage_directory . "/" . date('YmdHis') . "." . time(); 

       // rename $temporary_file to $storage_file 
       if (!rename($temporary_file, $storage_file)) { 
        Log::error(__FILE__ . "::" . __FUNCTION__ . " : Failed to move temporary file from " . $this->temporary_directory . " to " . $this->storage_directory); 
       } 
      } else{ 
       Log::error(__FILE__ . "::" . __FUNCTION__ . " : Temporary file was not available or does not exist."); 
      } 
     } else { 
      Log::error(__FILE__ . "::" . __FUNCTION__ . " : Temporary file was not created."); 
     } 
    } catch (\ErrorException $e) { 
     // Catches missing directory or file, or tempnam couldn't find temporary storage path //Todo add test for this exception 
     Log::error(__FILE__ . "::" . __FUNCTION__ . " : " . $e->getMessage()); 
    } catch (\Exception $e) { 
     // Catches uncaught exceptions 
     Log::error(__FILE__ . "::" . __FUNCTION__ . " : " . $e->getMessage()); 
    } 
} 

} catch (\ErrorException $e) { 
     // Catches missing directory or file, or tempnam couldn't find temporary storage path //Todo add test for this exception 
     Log::error(__FILE__ . "::" . __FUNCTION__ . " : " . $e->getMessage()); 
} 

しかし:私はテストを実行すると

public function test_getDataAndStoreToCSVFile_handles_ErrorException() { 

    // set up data 
    $this->setup_all_data_for_getDataAndStoreToCsvFile_funtion(); 

    // mock class 
    $mock = $this->getMockBuilder('App\Interfaces\Sources\IdbStationSourceInterface') 

    // stub function createDirectories, will now return null and not create directories, missing directories will throw ErrorException 
    ->setMethods(['createDirectories']) 
    ->getMock(); 

    // expect the ErrorException to be thrown 
    $this->expectException('ErrorException'); 

    // run function 
    $mock->getDataAndStoreToCSVFile(); 
} 

、私のログは私がに落ちたことを示しています私の端末は:

1)Tests \ Interfaces \ Sources \ IdbStationSourceInterfaceTest :: test_getDataAn dStoreToCSVFile_handles_ErrorException "ErrorException"型の例外がスローされたことをアサートできませんでした。

どこから行くのかわからない、私はいくつかのことを読んで試しましたが、明らかに何か間違っています。

編集1:

しようとしました:ます$ this-> setExpectedException( "ErrorException");

しかし、私は、次を得る:

1)テスト\インタフェースの\ Sources \ IdbStationSourceInterfaceTest :: test_getDataAndStoreToCSVFile_handles_ErrorException エラー:インターフェースの\ Sources \ IdbStationSourceInterfaceTest :: setExpectedException()

+0

'ます$ this-> setExpectedException( "ErrorException")を試してみてください。返信用' – ishegg

+0

@isheggのThxを、私はそれがうまくいかなかった、あなたのコードを含めるために、質問を変更しました。 – Ben

答えて

1

ザッツ\未定義のメソッドのテストの呼び出しあなたが例外をキャッチしたからです。 PHPUnits expectedException -methodは未処理の例外または再処理例外のみを登録します。 catchブロック内で例外を再現するか、catchブロック内で作成しているログエントリをテストするだけです。

0

function getDataAndStoreToCSVFile()エラーコードとメッセージでエラーがスローされます。次に、これらのアサーションをテストケースで使用できます。

/** *@expectedException ExampleException *@expectedExceptionCode ExampleException::EceptionCode */ public function test_getDataAndStoreToCSVFile_handles_ErrorException() {}

関連する問題