使用Laravel框架PHPUnit的單元測試。PHPUnit的測試的ExpectedException不會引發異常
我與需要的文件被創建的目錄被寫入到它,總之功能工作,該函數獲取數據,將其寫入到一個臨時文件和移動要做一次臨時文件。
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());
}
}
爲了測試是否ErrorException當目錄丟失被拋出,這個測試:
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();
}
當我運行測試,我的日誌表明,我掉進了:
} 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());
}
但我說終端:
1)測試\接口\來源\ IdbStationSourceInterfaceTest :: test_getDataAn dStoreToCSVFile_handles_ErrorException 無法斷言類型「ErrorException」的那則拋出異常。
我不知道該從哪裏去,我閱讀並嘗試了幾件事情,但顯然我做錯了什麼。
編輯1:
試過函數:$ this-> setExpectedException( 「ErrorException」);
,但我得到以下幾點:
1)測試\接口\來源\ IdbStationSourceInterfaceTest :: test_getDataAndStoreToCSVFile_handles_ErrorException 錯誤:調用未定義的方法測試\接口\來源\ IdbStationSourceInterfaceTest :: setExpectedException()
嘗試'$這個 - > setExpectedException( 「ErrorException」);' – ishegg
@ishegg THX的答覆,我修改了問題,包括你的代碼,它不工作了。 – Ben