From 32a55a3f137be9e07aad369232261796d691e279 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 17 Dec 2017 16:34:40 +0900 Subject: [PATCH] Introduce identical functional tests across several formats --- .../Functional/AbstractFunctional.php | 35 +++++++++++ .../Functional/Enclosure.php | 37 +++++++++++ .../Functional/FreezePane.php | 40 ++++++++++++ .../Functional/MergedCells.php | 42 +++++++++++++ tests/PhpSpreadsheetTests/MergedCells.php | 63 ------------------- tests/PhpSpreadsheetTests/Reader/CsvTest.php | 25 +------- tests/PhpSpreadsheetTests/Reader/XlsTest.php | 37 ----------- tests/PhpSpreadsheetTests/Reader/XlsxTest.php | 32 +--------- 8 files changed, 158 insertions(+), 153 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php create mode 100644 tests/PhpSpreadsheetTests/Functional/Enclosure.php create mode 100644 tests/PhpSpreadsheetTests/Functional/FreezePane.php create mode 100644 tests/PhpSpreadsheetTests/Functional/MergedCells.php delete mode 100644 tests/PhpSpreadsheetTests/MergedCells.php delete mode 100644 tests/PhpSpreadsheetTests/Reader/XlsTest.php diff --git a/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php b/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php new file mode 100644 index 00000000..e531bfb5 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/AbstractFunctional.php @@ -0,0 +1,35 @@ +save($filename); + + $reader = IOFactory::createReader($format); + $reloadedSpreadsheet = $reader->load($filename); + unlink($filename); + + return $reloadedSpreadsheet; + } +} diff --git a/tests/PhpSpreadsheetTests/Functional/Enclosure.php b/tests/PhpSpreadsheetTests/Functional/Enclosure.php new file mode 100644 index 00000000..56c09b92 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/Enclosure.php @@ -0,0 +1,37 @@ +'; + + $spreadsheet = new Spreadsheet(); + $spreadsheet->getActiveSheet()->getCell('A1')->setValue($value); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + + $actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(); + self::assertSame($value, $actual, 'should be able to write and read strings with multiples quotes'); + } +} diff --git a/tests/PhpSpreadsheetTests/Functional/FreezePane.php b/tests/PhpSpreadsheetTests/Functional/FreezePane.php new file mode 100644 index 00000000..1c080952 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/FreezePane.php @@ -0,0 +1,40 @@ +getActiveSheet()->freezePane($cellSplit, $topLeftCell); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + + // Read written file + $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); + $actualCellSplit = $reloadedActive->getFreezePane(); + $actualTopLeftCell = $reloadedActive->getTopLeftCell(); + + self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane'); + self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); + } +} diff --git a/tests/PhpSpreadsheetTests/Functional/MergedCells.php b/tests/PhpSpreadsheetTests/Functional/MergedCells.php new file mode 100644 index 00000000..6ac6d350 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/MergedCells.php @@ -0,0 +1,42 @@ +setActiveSheetIndex(0); + $spreadsheet->getActiveSheet()->setCellValue('A1', '1'); + $spreadsheet->getActiveSheet()->setCellValue('B1', '2'); + $spreadsheet->getActiveSheet()->setCellValue('A2', '33'); + $spreadsheet->getActiveSheet()->mergeCells('A2:B2'); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + + $actual = 0; + foreach ($reloadedSpreadsheet->getWorksheetIterator() as $worksheet) { + $actual += count($worksheet->getMergeCells()); + } + + self::assertSame(1, $actual, "Format $format failed, could not read 1 merged cell"); + } +} diff --git a/tests/PhpSpreadsheetTests/MergedCells.php b/tests/PhpSpreadsheetTests/MergedCells.php deleted file mode 100644 index bf0772d2..00000000 --- a/tests/PhpSpreadsheetTests/MergedCells.php +++ /dev/null @@ -1,63 +0,0 @@ -writeMergedCells($filename, $format); - $actual = $this->readMergedCells($filename, $format); - unlink($filename); - - self::assertSame(1, $actual, "Format $format failed, could not read 1 merged cell"); - } - - private function writeMergedCells($filename, $format) - { - $spreadsheet = new Spreadsheet(); - $spreadsheet->setActiveSheetIndex(0); - $spreadsheet->getActiveSheet()->setCellValue('A1', '1'); - $spreadsheet->getActiveSheet()->setCellValue('B1', '2'); - $spreadsheet->getActiveSheet()->setCellValue('A2', '33'); - $spreadsheet->getActiveSheet()->mergeCells('A2:B2'); - - $writer = IOFactory::createWriter($spreadsheet, $format); - - $writer->save($filename); - } - - private function readMergedCells($filename, $format) - { - $reader = IOFactory::createReader($format); - $spreadsheet = $reader->load($filename); - $n = 0; - foreach ($spreadsheet->getWorksheetIterator() as $worksheet) { - foreach ($worksheet->getMergeCells() as $cells) { - ++$n; - } - } - - return $n; - } -} diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php index 0a10c045..b1811777 100644 --- a/tests/PhpSpreadsheetTests/Reader/CsvTest.php +++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php @@ -2,35 +2,14 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader; -use PhpOffice\PhpSpreadsheet\Reader\Csv as ReaderCsv; -use PhpOffice\PhpSpreadsheet\Shared\File; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PhpOffice\PhpSpreadsheet\Writer\Csv as WriterCsv; +use PhpOffice\PhpSpreadsheet\Reader\Csv; use PHPUnit\Framework\TestCase; class CsvTest extends TestCase { - public function testEnclosure() - { - $value = ''; - $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet'); - - // Write temp file with value - $spreadsheet = new Spreadsheet(); - $spreadsheet->getActiveSheet()->getCell('A1')->setValue($value); - $writer = new WriterCsv($spreadsheet); - $writer->save($filename); - - // Read written file - $reader = new ReaderCsv(); - $reloadedSpreadsheet = $reader->load($filename); - $actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(); - self::assertSame($value, $actual, 'should be able to write and read strings with multiples quotes'); - } - public function testDelimiterDetection() { - $reader = new ReaderCsv(); + $reader = new Csv(); self::assertNull($reader->getDelimiter()); $filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv'; diff --git a/tests/PhpSpreadsheetTests/Reader/XlsTest.php b/tests/PhpSpreadsheetTests/Reader/XlsTest.php deleted file mode 100644 index be435e83..00000000 --- a/tests/PhpSpreadsheetTests/Reader/XlsTest.php +++ /dev/null @@ -1,37 +0,0 @@ -getActiveSheet(); - $active->freezePane($cellSplit, $topLeftCell); - - $writer = new WriterXls($spreadsheet); - $writer->save($filename); - - // Read written file - $reader = new ReaderXls(); - $reloadedSpreadsheet = $reader->load($filename); - $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); - $actualCellSplit = $reloadedActive->getFreezePane(); - $actualTopLeftCell = $reloadedActive->getTopLeftCell(); - - self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane'); - self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); - } -} diff --git a/tests/PhpSpreadsheetTests/Reader/XlsxTest.php b/tests/PhpSpreadsheetTests/Reader/XlsxTest.php index a379fdac..fd14714d 100644 --- a/tests/PhpSpreadsheetTests/Reader/XlsxTest.php +++ b/tests/PhpSpreadsheetTests/Reader/XlsxTest.php @@ -2,10 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader; -use PhpOffice\PhpSpreadsheet\Reader\Xlsx as ReaderXlsx; -use PhpOffice\PhpSpreadsheet\Shared\File; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PhpOffice\PhpSpreadsheet\Writer\Xlsx as WriterXlsx; +use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PHPUnit\Framework\TestCase; class XlsxTest extends TestCase @@ -16,32 +13,7 @@ class XlsxTest extends TestCase public function testLoadXlsxWithoutCellReference() { $filename = './data/Reader/XLSX/without_cell_reference.xlsx'; - $reader = new ReaderXlsx(); + $reader = new Xlsx(); $reader->load($filename); } - - public function testFreezePane() - { - $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet'); - - $cellSplit = 'B2'; - $topLeftCell = 'E5'; - - $spreadsheet = new Spreadsheet(); - $active = $spreadsheet->getActiveSheet(); - $active->freezePane($cellSplit, $topLeftCell); - - $writer = new WriterXlsx($spreadsheet); - $writer->save($filename); - - // Read written file - $reader = new ReaderXlsx(); - $reloadedSpreadsheet = $reader->load($filename); - $reloadedActive = $reloadedSpreadsheet->getActiveSheet(); - $actualCellSplit = $reloadedActive->getFreezePane(); - $actualTopLeftCell = $reloadedActive->getTopLeftCell(); - - self::assertSame($cellSplit, $actualCellSplit, 'should be able to set freeze pane'); - self::assertSame($topLeftCell, $actualTopLeftCell, 'should be able to set the top left cell'); - } }