From 585409a9496912983f6722c58f19fbd7527b4227 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 24 May 2020 04:03:07 -0700 Subject: [PATCH] Testing - Delete Temp Files When No Longer Needed (#1488) No code changes. The tests in all of these scripts write to at least one temporary file, which is then read and not used again. The file should be deleted to avoid filling up the disk system. --- samples/Basic/07_Reader.php | 1 + samples/Basic/16_Csv.php | 12 ++++++++---- samples/Basic/20_Read_Xls.php | 1 + samples/Basic/24_Readfilter.php | 4 +++- samples/Basic/28_Iterator.php | 9 +++++---- samples/Basic/44_Worksheet_info.php | 2 ++ samples/Chart/34_Chart_update.php | 12 +++++++----- .../Calculation/DefinedNameConfusedForCellTest.php | 1 + tests/PhpSpreadsheetTests/Reader/Xlsx2Test.php | 10 ++-------- tests/PhpSpreadsheetTests/Reader/XlsxTest.php | 1 + .../Writer/Html/InvalidFileNameTest.php | 13 +++++++++++-- .../Writer/Xls/FormulaErrTest.php | 9 +-------- .../Writer/Xlsx/FloatsRetainedTest.php | 1 + 13 files changed, 44 insertions(+), 32 deletions(-) diff --git a/samples/Basic/07_Reader.php b/samples/Basic/07_Reader.php index 4d9bd79e..67b3ae56 100644 --- a/samples/Basic/07_Reader.php +++ b/samples/Basic/07_Reader.php @@ -17,3 +17,4 @@ $helper->logRead('Xlsx', $filename, $callStartTime); // Save $helper->write($spreadsheet, __FILE__); +unlink($filename); diff --git a/samples/Basic/16_Csv.php b/samples/Basic/16_Csv.php index de753d56..15bbf0d4 100644 --- a/samples/Basic/16_Csv.php +++ b/samples/Basic/16_Csv.php @@ -1,13 +1,15 @@ log('Write to CSV format'); /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writer */ -$writer = IOFactory::createWriter($spreadsheet, 'Csv')->setDelimiter(',') +$writer = new CsvWriter($spreadsheet); +$writer->setDelimiter(',') ->setEnclosure('"') ->setSheetIndex(0); @@ -19,13 +21,15 @@ $helper->logWrite($writer, $filename, $callStartTime); $helper->log('Read from CSV format'); /** @var \PhpOffice\PhpSpreadsheet\Reader\Csv $reader */ -$reader = IOFactory::createReader('Csv')->setDelimiter(',') +$reader = new CsvReader(); +$reader->setDelimiter(',') ->setEnclosure('"') ->setSheetIndex(0); $callStartTime = microtime(true); $spreadsheetFromCSV = $reader->load($filename); $helper->logRead('Csv', $filename, $callStartTime); +unlink($filename); // Write Xlsx $helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']); @@ -33,7 +37,7 @@ $helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']); // Write CSV $filenameCSV = $helper->getFilename(__FILE__, 'csv'); /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writerCSV */ -$writerCSV = IOFactory::createWriter($spreadsheetFromCSV, 'Csv'); +$writerCSV = new CsvWriter($spreadsheetFromCSV); $writerCSV->setExcelCompatibility(true); $callStartTime = microtime(true); diff --git a/samples/Basic/20_Read_Xls.php b/samples/Basic/20_Read_Xls.php index 9e5fa014..daeaf664 100644 --- a/samples/Basic/20_Read_Xls.php +++ b/samples/Basic/20_Read_Xls.php @@ -17,6 +17,7 @@ $helper->logWrite($writer, $filename, $callStartTime); $callStartTime = microtime(true); $spreadsheet = IOFactory::load($filename); $helper->logRead('Xls', $filename, $callStartTime); +unlink($filename); // Save $helper->write($spreadsheet, __FILE__); diff --git a/samples/Basic/24_Readfilter.php b/samples/Basic/24_Readfilter.php index 844996f2..ab1c2e41 100644 --- a/samples/Basic/24_Readfilter.php +++ b/samples/Basic/24_Readfilter.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet; use PhpOffice\PhpSpreadsheet\Reader\IReadFilter; +use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; require __DIR__ . '/../Header.php'; @@ -29,10 +30,11 @@ class MyReadFilter implements IReadFilter } $helper->log('Load from Xlsx file'); -$reader = IOFactory::createReader('Xlsx'); +$reader = new XlsxReader(); $reader->setReadFilter(new MyReadFilter()); $callStartTime = microtime(true); $spreadsheet = $reader->load($filename); +unlink($filename); $helper->logRead('Xlsx', $filename, $callStartTime); $helper->log('Remove unnecessary rows'); $spreadsheet->getActiveSheet()->removeRow(2, 18); diff --git a/samples/Basic/28_Iterator.php b/samples/Basic/28_Iterator.php index 4aec7a92..104dc47f 100644 --- a/samples/Basic/28_Iterator.php +++ b/samples/Basic/28_Iterator.php @@ -1,21 +1,22 @@ getTemporaryFilename(); -$writer = new Xlsx($sampleSpreadsheet); +$writer = new XlsxWriter($sampleSpreadsheet); $callStartTime = microtime(true); $writer->save($filename); $helper->logWrite($writer, $filename, $callStartTime); $callStartTime = microtime(true); -$reader = IOFactory::createReader('Xlsx'); +$reader = new XlsxReader(); $spreadsheet = $reader->load($filename); $helper->logRead('Xlsx', $filename, $callStartTime); +unlink($filename); $helper->log('Iterate worksheets'); foreach ($spreadsheet->getWorksheetIterator() as $worksheet) { $helper->log('Worksheet - ' . $worksheet->getTitle()); diff --git a/samples/Basic/44_Worksheet_info.php b/samples/Basic/44_Worksheet_info.php index 33c0cd05..57822369 100644 --- a/samples/Basic/44_Worksheet_info.php +++ b/samples/Basic/44_Worksheet_info.php @@ -24,3 +24,5 @@ var_dump($sheetList); $helper->log('Worksheet Names:'); var_dump($sheetInfo); + +unlink($filename); diff --git a/samples/Chart/34_Chart_update.php b/samples/Chart/34_Chart_update.php index a4287927..5d725c49 100644 --- a/samples/Chart/34_Chart_update.php +++ b/samples/Chart/34_Chart_update.php @@ -1,20 +1,22 @@ getTemporaryFilename(); -$writer = new Xlsx($sampleSpreadsheet); +$writer = new XlsxWriter($sampleSpreadsheet); +$writer->setIncludeCharts(true); $writer->save($filename); $helper->log('Load from Xlsx file'); -$reader = IOFactory::createReader('Xlsx'); +$reader = new XlsxReader(); $reader->setIncludeCharts(true); $spreadsheet = $reader->load($filename); +unlink($filename); $helper->log('Update cell data values that are displayed in the chart'); $worksheet = $spreadsheet->getActiveSheet(); @@ -31,7 +33,7 @@ $worksheet->fromArray( // Save Excel 2007 file $filename = $helper->getFilename(__FILE__); -$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); +$writer = new XlsxWriter($spreadsheet); $writer->setIncludeCharts(true); $callStartTime = microtime(true); $writer->save($filename); diff --git a/tests/PhpSpreadsheetTests/Calculation/DefinedNameConfusedForCellTest.php b/tests/PhpSpreadsheetTests/Calculation/DefinedNameConfusedForCellTest.php index 12d73356..76886c23 100644 --- a/tests/PhpSpreadsheetTests/Calculation/DefinedNameConfusedForCellTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/DefinedNameConfusedForCellTest.php @@ -18,5 +18,6 @@ class DefinedNameConfusedForCellTest extends TestCase $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); $writer->save($filename); self::assertTrue(true); + unlink($filename); } } diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx2Test.php b/tests/PhpSpreadsheetTests/Reader/Xlsx2Test.php index 4d0b6a8a..1220c378 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx2Test.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx2Test.php @@ -11,14 +11,6 @@ use PHPUnit\Framework\TestCase; class Xlsx2Test extends TestCase { - protected function tearDown(): void - { - $outfile = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); - if (file_exists($outfile)) { - unlink($outfile); - } - } - public function testLoadXlsxConditionalFormatting2(): void { // Make sure Conditionals are read correctly from existing file @@ -63,6 +55,7 @@ class Xlsx2Test extends TestCase $writer = IOFactory::createWriter($spreadshee1, 'Xlsx'); $writer->save($outfile); $spreadsheet = $reader->load($outfile); + unlink($outfile); $worksheet = $spreadsheet->getActiveSheet(); $conditionalStyle = $worksheet->getConditionalStyles('A2:A8'); @@ -110,6 +103,7 @@ class Xlsx2Test extends TestCase $writer->save($outfile); $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($outfile); + unlink($outfile); $worksheet = $spreadsheet->getActiveSheet(); $conditionalStyle = $worksheet->getConditionalStyles('A1:A6'); diff --git a/tests/PhpSpreadsheetTests/Reader/XlsxTest.php b/tests/PhpSpreadsheetTests/Reader/XlsxTest.php index 9e0b5f66..b326c142 100644 --- a/tests/PhpSpreadsheetTests/Reader/XlsxTest.php +++ b/tests/PhpSpreadsheetTests/Reader/XlsxTest.php @@ -222,6 +222,7 @@ class XlsxTest extends TestCase $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); $writer->save($resultFilename); $excel = $reader->load($resultFilename); + unlink($resultFilename); // Fake assert. The only thing we need is to ensure the file is loaded without exception self::assertNotNull($excel); } diff --git a/tests/PhpSpreadsheetTests/Writer/Html/InvalidFileNameTest.php b/tests/PhpSpreadsheetTests/Writer/Html/InvalidFileNameTest.php index 7baa2338..36a40e96 100644 --- a/tests/PhpSpreadsheetTests/Writer/Html/InvalidFileNameTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Html/InvalidFileNameTest.php @@ -30,9 +30,8 @@ class InvalidFileNameTest extends Functional\AbstractFunctional $writer->save(''); } - public function testEmptyTempdirNamePdf(): void + public function testNotEmptyTempdirNamePdf(): void { - $this->expectException(WriterException::class); $spreadsheet = new Spreadsheet(); $spreadsheet->getActiveSheet()->getCell('A1')->setValue('Cell 1'); $writer = new Mpdf($spreadsheet); @@ -41,6 +40,16 @@ class InvalidFileNameTest extends Functional\AbstractFunctional $writer->setPaperSize(PageSetup::PAPERSIZE_LEDGER); self::assertEquals($writer->getPaperSize(), PageSetup::PAPERSIZE_LEDGER); self::assertEquals(File::sysGetTempDir() . '/phpsppdf', $writer->getTempDir()); + $writer->setTempDir(File::sysGetTempDir()); + self::assertEquals(File::sysGetTempDir(), $writer->getTempDir()); + } + + public function testEmptyTempdirNamePdf(): void + { + $this->expectException(WriterException::class); + $spreadsheet = new Spreadsheet(); + $spreadsheet->getActiveSheet()->getCell('A1')->setValue('Cell 1'); + $writer = new Mpdf($spreadsheet); $writer->setTempDir(''); } diff --git a/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php b/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php index 61c70cb6..21a7c928 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php @@ -9,14 +9,6 @@ use PHPUnit\Framework\TestCase; class FormulaErrTest extends TestCase { - protected function tearDown(): void - { - $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); - if (file_exists($filename)) { - unlink($filename); - } - } - public function testFormulaError(): void { $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); @@ -31,6 +23,7 @@ class FormulaErrTest extends TestCase $writer->save($filename); $reader = IOFactory::createReader('Xls'); $robj = $reader->load($filename); + unlink($filename); $sheet0 = $robj->setActiveSheetIndex(0); $a1 = $sheet0->getCell('A1')->getCalculatedValue(); self::assertEquals(2, $a1); diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php index aad074de..746b9846 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/FloatsRetainedTest.php @@ -28,6 +28,7 @@ class FloatsRetainedTest extends TestCase $reader = new Reader(); $sheet = $reader->load($outputFilename); + unlink($outputFilename); self::assertSame($value, $sheet->getActiveSheet()->getCell('A1')->getValue()); }