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.
This commit is contained in:
oleibman 2020-05-24 04:03:07 -07:00 committed by GitHub
parent 41b95c1542
commit 585409a949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 44 additions and 32 deletions

View File

@ -17,3 +17,4 @@ $helper->logRead('Xlsx', $filename, $callStartTime);
// Save // Save
$helper->write($spreadsheet, __FILE__); $helper->write($spreadsheet, __FILE__);
unlink($filename);

View File

@ -1,13 +1,15 @@
<?php <?php
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader;
use PhpOffice\PhpSpreadsheet\Writer\Csv as CsvWriter;
require __DIR__ . '/../Header.php'; require __DIR__ . '/../Header.php';
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php'; $spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
$helper->log('Write to CSV format'); $helper->log('Write to CSV format');
/** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writer */ /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writer */
$writer = IOFactory::createWriter($spreadsheet, 'Csv')->setDelimiter(',') $writer = new CsvWriter($spreadsheet);
$writer->setDelimiter(',')
->setEnclosure('"') ->setEnclosure('"')
->setSheetIndex(0); ->setSheetIndex(0);
@ -19,13 +21,15 @@ $helper->logWrite($writer, $filename, $callStartTime);
$helper->log('Read from CSV format'); $helper->log('Read from CSV format');
/** @var \PhpOffice\PhpSpreadsheet\Reader\Csv $reader */ /** @var \PhpOffice\PhpSpreadsheet\Reader\Csv $reader */
$reader = IOFactory::createReader('Csv')->setDelimiter(',') $reader = new CsvReader();
$reader->setDelimiter(',')
->setEnclosure('"') ->setEnclosure('"')
->setSheetIndex(0); ->setSheetIndex(0);
$callStartTime = microtime(true); $callStartTime = microtime(true);
$spreadsheetFromCSV = $reader->load($filename); $spreadsheetFromCSV = $reader->load($filename);
$helper->logRead('Csv', $filename, $callStartTime); $helper->logRead('Csv', $filename, $callStartTime);
unlink($filename);
// Write Xlsx // Write Xlsx
$helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']); $helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']);
@ -33,7 +37,7 @@ $helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']);
// Write CSV // Write CSV
$filenameCSV = $helper->getFilename(__FILE__, 'csv'); $filenameCSV = $helper->getFilename(__FILE__, 'csv');
/** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writerCSV */ /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writerCSV */
$writerCSV = IOFactory::createWriter($spreadsheetFromCSV, 'Csv'); $writerCSV = new CsvWriter($spreadsheetFromCSV);
$writerCSV->setExcelCompatibility(true); $writerCSV->setExcelCompatibility(true);
$callStartTime = microtime(true); $callStartTime = microtime(true);

View File

@ -17,6 +17,7 @@ $helper->logWrite($writer, $filename, $callStartTime);
$callStartTime = microtime(true); $callStartTime = microtime(true);
$spreadsheet = IOFactory::load($filename); $spreadsheet = IOFactory::load($filename);
$helper->logRead('Xls', $filename, $callStartTime); $helper->logRead('Xls', $filename, $callStartTime);
unlink($filename);
// Save // Save
$helper->write($spreadsheet, __FILE__); $helper->write($spreadsheet, __FILE__);

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet; namespace PhpOffice\PhpSpreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter; use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
require __DIR__ . '/../Header.php'; require __DIR__ . '/../Header.php';
@ -29,10 +30,11 @@ class MyReadFilter implements IReadFilter
} }
$helper->log('Load from Xlsx file'); $helper->log('Load from Xlsx file');
$reader = IOFactory::createReader('Xlsx'); $reader = new XlsxReader();
$reader->setReadFilter(new MyReadFilter()); $reader->setReadFilter(new MyReadFilter());
$callStartTime = microtime(true); $callStartTime = microtime(true);
$spreadsheet = $reader->load($filename); $spreadsheet = $reader->load($filename);
unlink($filename);
$helper->logRead('Xlsx', $filename, $callStartTime); $helper->logRead('Xlsx', $filename, $callStartTime);
$helper->log('Remove unnecessary rows'); $helper->log('Remove unnecessary rows');
$spreadsheet->getActiveSheet()->removeRow(2, 18); $spreadsheet->getActiveSheet()->removeRow(2, 18);

View File

@ -1,21 +1,22 @@
<?php <?php
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XLsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XLsxWriter;
require __DIR__ . '/../Header.php'; require __DIR__ . '/../Header.php';
$sampleSpreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php'; $sampleSpreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
$filename = $helper->getTemporaryFilename(); $filename = $helper->getTemporaryFilename();
$writer = new Xlsx($sampleSpreadsheet); $writer = new XlsxWriter($sampleSpreadsheet);
$callStartTime = microtime(true); $callStartTime = microtime(true);
$writer->save($filename); $writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime); $helper->logWrite($writer, $filename, $callStartTime);
$callStartTime = microtime(true); $callStartTime = microtime(true);
$reader = IOFactory::createReader('Xlsx'); $reader = new XlsxReader();
$spreadsheet = $reader->load($filename); $spreadsheet = $reader->load($filename);
$helper->logRead('Xlsx', $filename, $callStartTime); $helper->logRead('Xlsx', $filename, $callStartTime);
unlink($filename);
$helper->log('Iterate worksheets'); $helper->log('Iterate worksheets');
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) { foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$helper->log('Worksheet - ' . $worksheet->getTitle()); $helper->log('Worksheet - ' . $worksheet->getTitle());

View File

@ -24,3 +24,5 @@ var_dump($sheetList);
$helper->log('Worksheet Names:'); $helper->log('Worksheet Names:');
var_dump($sheetInfo); var_dump($sheetInfo);
unlink($filename);

View File

@ -1,20 +1,22 @@
<?php <?php
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
require __DIR__ . '/../Header.php'; require __DIR__ . '/../Header.php';
// Create temporary file that will be read // Create temporary file that will be read
$sampleSpreadsheet = require __DIR__ . '/../templates/chartSpreadsheet.php'; $sampleSpreadsheet = require __DIR__ . '/../templates/chartSpreadsheet.php';
$filename = $helper->getTemporaryFilename(); $filename = $helper->getTemporaryFilename();
$writer = new Xlsx($sampleSpreadsheet); $writer = new XlsxWriter($sampleSpreadsheet);
$writer->setIncludeCharts(true);
$writer->save($filename); $writer->save($filename);
$helper->log('Load from Xlsx file'); $helper->log('Load from Xlsx file');
$reader = IOFactory::createReader('Xlsx'); $reader = new XlsxReader();
$reader->setIncludeCharts(true); $reader->setIncludeCharts(true);
$spreadsheet = $reader->load($filename); $spreadsheet = $reader->load($filename);
unlink($filename);
$helper->log('Update cell data values that are displayed in the chart'); $helper->log('Update cell data values that are displayed in the chart');
$worksheet = $spreadsheet->getActiveSheet(); $worksheet = $spreadsheet->getActiveSheet();
@ -31,7 +33,7 @@ $worksheet->fromArray(
// Save Excel 2007 file // Save Excel 2007 file
$filename = $helper->getFilename(__FILE__); $filename = $helper->getFilename(__FILE__);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer = new XlsxWriter($spreadsheet);
$writer->setIncludeCharts(true); $writer->setIncludeCharts(true);
$callStartTime = microtime(true); $callStartTime = microtime(true);
$writer->save($filename); $writer->save($filename);

View File

@ -18,5 +18,6 @@ class DefinedNameConfusedForCellTest extends TestCase
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
$writer->save($filename); $writer->save($filename);
self::assertTrue(true); self::assertTrue(true);
unlink($filename);
} }
} }

View File

@ -11,14 +11,6 @@ use PHPUnit\Framework\TestCase;
class Xlsx2Test extends 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 public function testLoadXlsxConditionalFormatting2(): void
{ {
// Make sure Conditionals are read correctly from existing file // Make sure Conditionals are read correctly from existing file
@ -63,6 +55,7 @@ class Xlsx2Test extends TestCase
$writer = IOFactory::createWriter($spreadshee1, 'Xlsx'); $writer = IOFactory::createWriter($spreadshee1, 'Xlsx');
$writer->save($outfile); $writer->save($outfile);
$spreadsheet = $reader->load($outfile); $spreadsheet = $reader->load($outfile);
unlink($outfile);
$worksheet = $spreadsheet->getActiveSheet(); $worksheet = $spreadsheet->getActiveSheet();
$conditionalStyle = $worksheet->getConditionalStyles('A2:A8'); $conditionalStyle = $worksheet->getConditionalStyles('A2:A8');
@ -110,6 +103,7 @@ class Xlsx2Test extends TestCase
$writer->save($outfile); $writer->save($outfile);
$reader = IOFactory::createReader('Xlsx'); $reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($outfile); $spreadsheet = $reader->load($outfile);
unlink($outfile);
$worksheet = $spreadsheet->getActiveSheet(); $worksheet = $spreadsheet->getActiveSheet();
$conditionalStyle = $worksheet->getConditionalStyles('A1:A6'); $conditionalStyle = $worksheet->getConditionalStyles('A1:A6');

View File

@ -222,6 +222,7 @@ class XlsxTest extends TestCase
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel);
$writer->save($resultFilename); $writer->save($resultFilename);
$excel = $reader->load($resultFilename); $excel = $reader->load($resultFilename);
unlink($resultFilename);
// Fake assert. The only thing we need is to ensure the file is loaded without exception // Fake assert. The only thing we need is to ensure the file is loaded without exception
self::assertNotNull($excel); self::assertNotNull($excel);
} }

View File

@ -30,9 +30,8 @@ class InvalidFileNameTest extends Functional\AbstractFunctional
$writer->save(''); $writer->save('');
} }
public function testEmptyTempdirNamePdf(): void public function testNotEmptyTempdirNamePdf(): void
{ {
$this->expectException(WriterException::class);
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->getCell('A1')->setValue('Cell 1'); $spreadsheet->getActiveSheet()->getCell('A1')->setValue('Cell 1');
$writer = new Mpdf($spreadsheet); $writer = new Mpdf($spreadsheet);
@ -41,6 +40,16 @@ class InvalidFileNameTest extends Functional\AbstractFunctional
$writer->setPaperSize(PageSetup::PAPERSIZE_LEDGER); $writer->setPaperSize(PageSetup::PAPERSIZE_LEDGER);
self::assertEquals($writer->getPaperSize(), PageSetup::PAPERSIZE_LEDGER); self::assertEquals($writer->getPaperSize(), PageSetup::PAPERSIZE_LEDGER);
self::assertEquals(File::sysGetTempDir() . '/phpsppdf', $writer->getTempDir()); 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(''); $writer->setTempDir('');
} }

View File

@ -9,14 +9,6 @@ use PHPUnit\Framework\TestCase;
class FormulaErrTest extends 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 public function testFormulaError(): void
{ {
$obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
@ -31,6 +23,7 @@ class FormulaErrTest extends TestCase
$writer->save($filename); $writer->save($filename);
$reader = IOFactory::createReader('Xls'); $reader = IOFactory::createReader('Xls');
$robj = $reader->load($filename); $robj = $reader->load($filename);
unlink($filename);
$sheet0 = $robj->setActiveSheetIndex(0); $sheet0 = $robj->setActiveSheetIndex(0);
$a1 = $sheet0->getCell('A1')->getCalculatedValue(); $a1 = $sheet0->getCell('A1')->getCalculatedValue();
self::assertEquals(2, $a1); self::assertEquals(2, $a1);

View File

@ -28,6 +28,7 @@ class FloatsRetainedTest extends TestCase
$reader = new Reader(); $reader = new Reader();
$sheet = $reader->load($outputFilename); $sheet = $reader->load($outputFilename);
unlink($outputFilename);
self::assertSame($value, $sheet->getActiveSheet()->getCell('A1')->getValue()); self::assertSame($value, $sheet->getActiveSheet()->getCell('A1')->getValue());
} }