Introduce identical functional tests across several formats

This commit is contained in:
Adrien Crivelli 2017-12-17 16:34:40 +09:00
parent 11b055b29f
commit 32a55a3f13
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
8 changed files with 158 additions and 153 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
/**
* Base class for functional test to write and reload file on disk across different formats.
*/
abstract class AbstractFunctional extends TestCase
{
/**
* Write spreadsheet to disk, reload and return it.
*
* @param Spreadsheet $spreadsheet
* @param string $format
*
* @return Spreadsheet
*/
protected function writeAndReload(Spreadsheet $spreadsheet, $format)
{
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
$writer = IOFactory::createWriter($spreadsheet, $format);
$writer->save($filename);
$reader = IOFactory::createReader($format);
$reloadedSpreadsheet = $reader->load($filename);
unlink($filename);
return $reloadedSpreadsheet;
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Enclosure extends AbstractFunctional
{
public function providerFormats()
{
return [
['Html'],
['Xls'],
['Xlsx'],
['Ods'],
['Csv'],
];
}
/**
* @dataProvider providerFormats
*
* @param string $format
*/
public function testEnclosure($format)
{
$value = '<img alt="" src="http://example.com/image.jpg" />';
$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');
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class FreezePane extends AbstractFunctional
{
public function providerFormats()
{
return [
['Xls'],
['Xlsx'],
];
}
/**
* @dataProvider providerFormats
*
* @param string $format
*/
public function testFreezePane($format)
{
$cellSplit = 'B2';
$topLeftCell = 'E5';
$spreadsheet = new Spreadsheet();
$spreadsheet->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');
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class MergedCells extends AbstractFunctional
{
public function providerFormats()
{
return [
['Html'],
['Xls'],
['Xlsx'],
['Ods'],
];
}
/**
* @dataProvider providerFormats
*
* @param string $format
*/
public function testMergedCells($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');
$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");
}
}

View File

@ -1,63 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class MergedCells extends TestCase
{
public function providerMergedCells()
{
return [
['Html'],
['Xls'],
['Xlsx'],
['Ods'],
];
}
/**
* @dataProvider providerMergedCells
*
* @param string $format
*/
public function testMergedCells($format)
{
$filename = tempnam(sys_get_temp_dir(), strtolower($format));
$this->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;
}
}

View File

@ -2,35 +2,14 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader; namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Csv as ReaderCsv; use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv as WriterCsv;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class CsvTest extends TestCase class CsvTest extends TestCase
{ {
public function testEnclosure()
{
$value = '<img alt="" src="http://example.com/image.jpg" />';
$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() public function testDelimiterDetection()
{ {
$reader = new ReaderCsv(); $reader = new Csv();
self::assertNull($reader->getDelimiter()); self::assertNull($reader->getDelimiter());
$filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv'; $filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv';

View File

@ -1,37 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Xls as ReaderXls;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls as WriterXls;
use PHPUnit_Framework_TestCase;
class XlsTest extends PHPUnit_Framework_TestCase
{
public function testFreezePane()
{
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet');
$cellSplit = 'B2';
$topLeftCell = 'E5';
$spreadsheet = new Spreadsheet();
$active = $spreadsheet->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');
}
}

View File

@ -2,10 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader; namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as ReaderXlsx; use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as WriterXlsx;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class XlsxTest extends TestCase class XlsxTest extends TestCase
@ -16,32 +13,7 @@ class XlsxTest extends TestCase
public function testLoadXlsxWithoutCellReference() public function testLoadXlsxWithoutCellReference()
{ {
$filename = './data/Reader/XLSX/without_cell_reference.xlsx'; $filename = './data/Reader/XLSX/without_cell_reference.xlsx';
$reader = new ReaderXlsx(); $reader = new Xlsx();
$reader->load($filename); $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');
}
} }