Temp files should be deleted when an exception is thrown (#327)
If an exception is thrown while writing data, instead of letting the developer handle this situation gracefully, Spout can attempt to delete all the temporary files that were created so far, as well as the output file as it won't be completed and therefore corrupted.
This commit is contained in:
1 parent
442a9837f1
commit
23f8cc4f05
5 files changed
+157
-1
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\ODS;
|
||||
|
||||
use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
@@ -91,6 +92,39 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->writeToODSFile($dataRows, $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown()
|
||||
{
|
||||
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.ods';
|
||||
$dataRows = [
|
||||
['wrong'],
|
||||
[new \stdClass()],
|
||||
];
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$this->recreateTempFolder();
|
||||
$tempFolderPath = $this->getTempFolderPath();
|
||||
|
||||
/** @var \Box\Spout\Writer\ODS\Writer $writer */
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer->setTempFolder($tempFolderPath);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
try {
|
||||
$writer->addRows($dataRows);
|
||||
$this->fail('Exception should have been thrown');
|
||||
} catch (SpoutException $e) {
|
||||
$this->assertFalse(file_exists($fileName), 'Output file should have been deleted');
|
||||
|
||||
$numFiles = iterator_count(new \FilesystemIterator($tempFolderPath, \FilesystemIterator::SKIP_DOTS));
|
||||
$this->assertEquals(0, $numFiles, 'All temp files should have been deleted');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user