PHPUnit测试ExpectedException未引发异常
使用带有phpunit的Laravel框架进行单元测试 我正在使用一个函数,该函数要求为要写入的文件创建目录,简而言之,该函数获取数据,将其写入临时文件,并在完成后移动临时文件PHPUnit测试ExpectedException未引发异常,php,laravel-5,exception-handling,phpunit,Php,Laravel 5,Exception Handling,Phpunit,使用带有phpunit的Laravel框架进行单元测试 我正在使用一个函数,该函数要求为要写入的文件创建目录,简而言之,该函数获取数据,将其写入临时文件,并在完成后移动临时文件 public function getDataAndStoreToCSVFile() { Log::info(date('Y-m-d H:i:s') . " -> " . __FILE__ . "::" . __FUNCTION__); try { // make sure dire
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) Tests\Interfaces\Sources\IdbStationSourceInterfaceTest::test\u getDataAndStoreToCSVFile\u handles\u ErrorException
无法断言引发了类型为“ErrorException”的异常
我不知道从那里去哪里,我阅读并尝试了一些事情,但很明显我做错了什么
编辑1:
已尝试:$this->setExpectedException(“ErrorException”)
但我得到了以下信息:
1) Tests\Interfaces\Sources\IdbStationSourceInterfaceTest::test\u getDataAndStoreToCSVFile\u handles\u ErrorException
错误:调用未定义的方法Tests\Interfaces\Sources\IdbStationSourceInterfaceTest::setExpectedException()这是因为捕获了异常。PHPUnits
expectedException
-方法仅注册未处理或重新处理的异常。要么在catch块中重新显示异常,要么只测试在catch块中创建的日志项。从函数getDataAndStoreToCSVFile()
中,您只会抛出带有错误代码和消息的错误。然后您可以在测试用例中使用这些断言
/**
*@expectedException示例异常
*@expectedExceptionCode示例Exception::EceptionCode
*/
公共函数测试\u getDataAndStoreToCSVFile \u处理\u ErrorException(){}
尝试$this->setExpectedException(“ErrorException”)代码>@ishegg Thx对于回复,我修改了问题以包含您的代码,但没有成功。