Row objects and Cell styling
This commit introduces Row and Cell entities, that will replace the arrays passed in previously. It also adds support for Cell styling (instead of Row styling only).
This commit is contained in:
1 parent
fec27e9056
commit
7274226b75
32 files changed
+1113
-859
No files matched your search
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||
use Box\Spout\Writer\RowCreationHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
/**
|
||||
@@ -13,6 +14,7 @@ use Box\Spout\Writer\WriterFactory;
|
||||
class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
use RowCreationHelper;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
@@ -91,7 +93,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setName($sheetName);
|
||||
|
||||
$writer->addRow(['xlsx--11', 'xlsx--12']);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
$writer->close();
|
||||
|
||||
return $sheet;
|
||||
@@ -110,9 +112,9 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$writer->addRow(['xlsx--sheet1--11', 'xlsx--sheet1--12']);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--sheet1--11', 'xlsx--sheet1--12']));
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
$writer->addRow(['xlsx--sheet2--11', 'xlsx--sheet2--12', 'xlsx--sheet2--13']);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--sheet2--11', 'xlsx--sheet2--12', 'xlsx--sheet2--13']));
|
||||
|
||||
$writer->close();
|
||||
|
||||
|
||||
@@ -5,7 +5,8 @@ namespace Box\Spout\Writer\XLSX;
|
||||
use Box\Spout\Common\Exception\SpoutException;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\RowCreationHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
|
||||
|
||||
@@ -15,6 +16,7 @@ use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
|
||||
class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
use RowCreationHelper;
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
@@ -35,7 +37,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRow(['xlsx--11', 'xlsx--12']);
|
||||
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +46,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRows([['xlsx--11', 'xlsx--12']]);
|
||||
$writer->addRows($this->createRowsFromValues([['xlsx--11', 'xlsx--12']]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,9 +113,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldThrowExceptionIfWritingStringExceedingMaxNumberOfCharactersAllowedPerCell()
|
||||
{
|
||||
$fileName = 'test_add_row_should_throw_exception_if_string_exceeds_max_num_chars_allowed_per_cell.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
[new \stdClass()],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
}
|
||||
@@ -124,10 +126,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown()
|
||||
{
|
||||
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['wrong'],
|
||||
[new \stdClass()],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
@@ -160,6 +162,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addNewSheetAndMakeItCurrent();
|
||||
@@ -179,6 +182,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
@@ -216,10 +220,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToSheetUsingInlineStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_inline_strings.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true);
|
||||
|
||||
@@ -236,10 +240,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingInlineStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_inline_strings.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
];
|
||||
]);
|
||||
|
||||
$numSheets = 2;
|
||||
$this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = true);
|
||||
@@ -259,10 +263,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToSheetUsingSharedStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_shared_strings.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
@@ -279,10 +283,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingSharedStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_shared_strings.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22', 'xlsx--23'],
|
||||
];
|
||||
]);
|
||||
|
||||
$numSheets = 2;
|
||||
$this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = false);
|
||||
@@ -302,9 +306,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldSupportAssociativeArrays()
|
||||
{
|
||||
$fileName = 'test_add_row_should_support_associative_arrays.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['foo' => 'xlsx--11', 'bar' => 'xlsx--12'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
@@ -321,13 +325,13 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldNotWriteEmptyRows()
|
||||
{
|
||||
$fileName = 'test_add_row_should_not_write_empty_rows.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
[''],
|
||||
['xlsx--21', 'xlsx--22'],
|
||||
['key' => ''],
|
||||
[''],
|
||||
['xlsx--51', 'xlsx--52'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
@@ -344,9 +348,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldSupportMultipleTypesOfData()
|
||||
{
|
||||
$fileName = 'test_add_row_should_support_multiple_types_of_data.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--11', true, '', 0, 10.2, null],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
@@ -362,18 +366,18 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldWriteGivenDataToTheCorrectSheet()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.xlsx';
|
||||
$dataRowsSheet1 = [
|
||||
$dataRowsSheet1 = $this->createRowsFromValues([
|
||||
['xlsx--sheet1--11', 'xlsx--sheet1--12'],
|
||||
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
|
||||
];
|
||||
$dataRowsSheet2 = [
|
||||
]);
|
||||
$dataRowsSheet2 = $this->createRowsFromValues([
|
||||
['xlsx--sheet2--11', 'xlsx--sheet2--12'],
|
||||
['xlsx--sheet2--21', 'xlsx--sheet2--22', 'xlsx--sheet2--23'],
|
||||
];
|
||||
$dataRowsSheet1Again = [
|
||||
]);
|
||||
$dataRowsSheet1Again = $this->createRowsFromValues([
|
||||
['xlsx--sheet1--31', 'xlsx--sheet1--32'],
|
||||
['xlsx--sheet1--41', 'xlsx--sheet1--42', 'xlsx--sheet1--43'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
@@ -419,11 +423,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldAutomaticallyCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOn()
|
||||
{
|
||||
$fileName = 'test_add_row_should_automatically_create_new_sheets_if_max_rows_reached_and_option_turned_on.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--sheet1--11', 'xlsx--sheet1--12'],
|
||||
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
|
||||
['xlsx--sheet2--11', 'xlsx--sheet2--12'], // this should be written in a new sheet
|
||||
];
|
||||
]);
|
||||
|
||||
// set the maxRowsPerSheet limit to 2
|
||||
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
|
||||
@@ -443,11 +447,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldNotCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOff()
|
||||
{
|
||||
$fileName = 'test_add_row_should_not_create_new_sheets_if_max_rows_reached_and_option_turned_off.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['xlsx--sheet1--11', 'xlsx--sheet1--12'],
|
||||
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
|
||||
['xlsx--sheet1--31', 'xlsx--sheet1--32'], // this should NOT be written in a new sheet
|
||||
];
|
||||
]);
|
||||
|
||||
// set the maxRowsPerSheet limit to 2
|
||||
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
|
||||
@@ -466,9 +470,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldEscapeHtmlSpecialCharacters()
|
||||
{
|
||||
$fileName = 'test_add_row_should_escape_html_special_characters.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['I\'m in "great" mood', 'This <must> be escaped & tested'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
@@ -482,9 +486,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowShouldEscapeControlCharacters()
|
||||
{
|
||||
$fileName = 'test_add_row_should_escape_control_characters.xlsx';
|
||||
$dataRows = [
|
||||
$dataRows = $this->createRowsFromValues([
|
||||
['control ' . chr(21) . ' character'],
|
||||
];
|
||||
]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
@@ -498,7 +502,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$fileName = 'test_mime_type.xlsx';
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$dataRows = [['foo']];
|
||||
$dataRows = $this->createRowsFromValues([['foo']]);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
@@ -507,61 +511,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriterShouldAcceptCellObjects()
|
||||
{
|
||||
$fileName = 'test_writer_should_accept_cell_objects.xlsx';
|
||||
$dataRows = [
|
||||
[new Cell('xlsx--11'), new Cell('xlsx--12')],
|
||||
[new Cell('xlsx--21'), new Cell('xlsx--22'), new Cell('xlsx--23')],
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
foreach ($dataRows as $dataRow) {
|
||||
/** @var Cell $cell */
|
||||
foreach ($dataRow as $cell) {
|
||||
$this->assertSharedStringWasWritten($fileName, $cell->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes()
|
||||
{
|
||||
$fileName = 'test_writer_should_accept_cell_objects_with_types.xlsx';
|
||||
|
||||
$dataRowsShared = [
|
||||
[new Cell('i am a string')],
|
||||
];
|
||||
$dataRowsInline = [
|
||||
[new Cell(51465), new Cell(true), new Cell(51465.5)],
|
||||
];
|
||||
|
||||
$dataRows = array_merge($dataRowsShared, $dataRowsInline);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
|
||||
|
||||
foreach ($dataRowsShared as $dataRow) {
|
||||
/** @var Cell $cell */
|
||||
foreach ($dataRow as $cell) {
|
||||
$this->assertSharedStringWasWritten($fileName, (string) $cell->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($dataRowsInline as $dataRow) {
|
||||
/** @var Cell $cell */
|
||||
foreach ($dataRow as $cell) {
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, $cell->getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param Row[] $allRows
|
||||
* @param string $fileName
|
||||
* @param bool $shouldUseInlineStrings
|
||||
* @param bool $shouldCreateSheetsAutomatically
|
||||
@@ -585,7 +535,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param Row[] $allRows
|
||||
* @param int $numSheets
|
||||
* @param string $fileName
|
||||
* @param bool $shouldUseInlineStrings
|
||||
|
||||
@@ -7,10 +7,12 @@ use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Entity\Row;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\RowCreationHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Writer\XLSX\Manager\OptionsManager;
|
||||
|
||||
@@ -20,6 +22,7 @@ use Box\Spout\Writer\XLSX\Manager\OptionsManager;
|
||||
class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use TestUsingResource;
|
||||
use RowCreationHelper;
|
||||
|
||||
/** @var \Box\Spout\Writer\Common\Entity\Style\Style */
|
||||
private $defaultStyle;
|
||||
@@ -38,7 +41,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $this->defaultStyle);
|
||||
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,53 +50,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
|
||||
{
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $this->defaultStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForInvalidStyle()
|
||||
{
|
||||
return [
|
||||
['style'],
|
||||
[new \stdClass()],
|
||||
[null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $style);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidStyle
|
||||
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
*/
|
||||
public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_throw_exception.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle([['xlsx--11', 'xlsx--12']], $style);
|
||||
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,10 +59,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
['xlsx--21', 'xlsx--22'],
|
||||
];
|
||||
|
||||
$style = (new StyleBuilder())
|
||||
->setFontBold()
|
||||
@@ -119,13 +72,19 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
->setFontName('Cambria')
|
||||
->build();
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [$style, $style2]);
|
||||
$dataRows = [
|
||||
$this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $style),
|
||||
$this->createStyledRowFromValues(['xlsx--21', 'xlsx--22'], $style2),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$fontsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fonts');
|
||||
$this->assertEquals(3, $fontsDomElement->getAttribute('count'), 'There should be 3 fonts, including the default one.');
|
||||
|
||||
$fontElements = $fontsDomElement->getElementsByTagName('font');
|
||||
$this->assertEquals(3, $fontElements->length, 'There should be 3 associated "font" elements, including the default one.');
|
||||
|
||||
// First font should be the default one
|
||||
$defaultFontElement = $fontElements->item(0);
|
||||
$this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.');
|
||||
@@ -158,15 +117,17 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldApplyStyleToCells()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_apply_style_to_cells.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11'],
|
||||
['xlsx--21'],
|
||||
['xlsx--31'],
|
||||
];
|
||||
|
||||
$style = (new StyleBuilder())->setFontBold()->build();
|
||||
$style2 = (new StyleBuilder())->setFontSize(15)->build();
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [$style, $style2, null]);
|
||||
$dataRows = [
|
||||
$this->createStyledRowFromValues(['xlsx--11'], $style),
|
||||
$this->createStyledRowFromValues(['xlsx--21'], $style2),
|
||||
$this->createRowFromValues(['xlsx--31']),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
|
||||
$this->assertEquals(3, count($cellDomElements), 'There should be 3 cells.');
|
||||
@@ -182,12 +143,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldApplyStyleToEmptyCellsIfNeeded()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_apply_style_to_empty_cells_if_needed.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', '', 'xlsx--13'],
|
||||
['xlsx--21', '', 'xlsx--23'],
|
||||
['xlsx--31', '', 'xlsx--33'],
|
||||
['xlsx--41', '', 'xlsx--43'],
|
||||
];
|
||||
|
||||
$styleWithFont = (new StyleBuilder())->setFontBold()->build();
|
||||
$styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->build();
|
||||
@@ -195,7 +150,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build();
|
||||
$styleWithBorder = (new StyleBuilder())->setBorder($border)->build();
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [null, $styleWithFont, $styleWithBackground, $styleWithBorder]);
|
||||
$dataRows = [
|
||||
$this->createRowFromValues(['xlsx--11', '', 'xlsx--13']),
|
||||
$this->createStyledRowFromValues(['xlsx--21', '', 'xlsx--23'], $styleWithFont),
|
||||
$this->createStyledRowFromValues(['xlsx--31', '', 'xlsx--33'], $styleWithBackground),
|
||||
$this->createStyledRowFromValues(['xlsx--41', '', 'xlsx--43'], $styleWithBorder),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
|
||||
|
||||
@@ -229,13 +191,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldReuseDuplicateStyles()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.xlsx';
|
||||
$dataRows = [
|
||||
|
||||
$style = (new StyleBuilder())->setFontBold()->build();
|
||||
$dataRows = $this->createStyledRowsFromValues([
|
||||
['xlsx--11'],
|
||||
['xlsx--21'],
|
||||
];
|
||||
$style = (new StyleBuilder())->setFontBold()->build();
|
||||
], $style);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $style);
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
|
||||
$this->assertEquals('1', $cellDomElements[0]->getAttribute('s'));
|
||||
@@ -248,12 +211,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.xlsx';
|
||||
$dataRows = [
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
];
|
||||
$style = (new StyleBuilder())->setShouldWrapText()->build();
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $style);
|
||||
$style = (new StyleBuilder())->setShouldWrapText()->build();
|
||||
$dataRows = $this->createStyledRowsFromValues([
|
||||
['xlsx--11', 'xlsx--12'],
|
||||
], $style);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||
@@ -267,12 +231,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.xlsx';
|
||||
$dataRows = [
|
||||
|
||||
$dataRows = $this->createStyledRowsFromValues([
|
||||
["xlsx--11\nxlsx--11"],
|
||||
['xlsx--21'],
|
||||
];
|
||||
], $this->defaultStyle);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $this->defaultStyle);
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||
@@ -286,11 +251,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAddBackgroundColor()
|
||||
{
|
||||
$fileName = 'test_add_background_color.xlsx';
|
||||
$dataRows = [
|
||||
['BgColor'],
|
||||
];
|
||||
|
||||
$style = (new StyleBuilder())->setBackgroundColor(Color::WHITE)->build();
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $style);
|
||||
$dataRows = $this->createStyledRowsFromValues([
|
||||
['BgColor'],
|
||||
], $style);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills');
|
||||
$this->assertEquals(3, $fillsDomElement->getAttribute('count'), 'There should be 3 fills, including the 2 default ones');
|
||||
|
||||
@@ -314,17 +282,16 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testReuseBackgroundColorSharedDefinition()
|
||||
{
|
||||
$fileName = 'test_add_background_color_shared_definition.xlsx';
|
||||
|
||||
$style = (new StyleBuilder())->setBackgroundColor(Color::RED)->setFontBold()->build();
|
||||
$style2 = (new StyleBuilder())->setBackgroundColor(Color::RED)->build();
|
||||
|
||||
$dataRows = [
|
||||
['row-bold-background-red'],
|
||||
['row-background-red'],
|
||||
$this->createStyledRowFromValues(['row-bold-background-red'], $style),
|
||||
$this->createStyledRowFromValues(['row-background-red'], $style2),
|
||||
];
|
||||
|
||||
$styles = [
|
||||
(new StyleBuilder())->setBackgroundColor(Color::RED)->setFontBold()->build(),
|
||||
(new StyleBuilder())->setBackgroundColor(Color::RED)->build(),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills');
|
||||
$this->assertEquals(
|
||||
@@ -354,12 +321,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$fileName = 'test_borders.xlsx';
|
||||
|
||||
$dataRows = [
|
||||
['row-with-border-bottom-green-thick-solid'],
|
||||
['row-without-border'],
|
||||
['row-with-border-top-red-thin-dashed'],
|
||||
];
|
||||
|
||||
$borderBottomGreenThickSolid = (new BorderBuilder())
|
||||
->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build();
|
||||
|
||||
@@ -372,7 +333,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
(new StyleBuilder())->setBorder($borderTopRedThinDashed)->build(),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
|
||||
$dataRows = [
|
||||
$this->createStyledRowFromValues(['row-with-border-bottom-green-thick-solid'], $styles[0]),
|
||||
$this->createStyledRowFromValues(['row-without-border'], $styles[1]),
|
||||
$this->createStyledRowFromValues(['row-with-border-top-red-thin-dashed'], $styles[2]),
|
||||
];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
|
||||
$this->assertEquals(3, $borderElements->getAttribute('count'), '3 borders present');
|
||||
|
||||
@@ -388,10 +356,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
// Border should be Left, Right, Top, Bottom
|
||||
$fileName = 'test_borders_correct_order.xlsx';
|
||||
|
||||
$dataRows = [
|
||||
['I am a teapot'],
|
||||
];
|
||||
|
||||
$borders = (new BorderBuilder())
|
||||
->setBorderRight()
|
||||
->setBorderTop()
|
||||
@@ -400,7 +364,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
->build();
|
||||
|
||||
$style = (new StyleBuilder())->setBorder($borders)->build();
|
||||
$this->writeToXLSXFile($dataRows, $fileName, $style);
|
||||
|
||||
$dataRows = $this->createStyledRowsFromValues([
|
||||
['I am a teapot'],
|
||||
], $style);
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
|
||||
|
||||
$correctOrdering = [
|
||||
@@ -429,7 +398,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
public function testSetDefaultRowStyle()
|
||||
{
|
||||
$fileName = 'test_set_default_row_style.xlsx';
|
||||
$dataRows = [['xlsx--11']];
|
||||
$dataRows = $this->createRowsFromValues([['xlsx--11']]);
|
||||
|
||||
$defaultFontSize = 50;
|
||||
$defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build();
|
||||
@@ -463,22 +432,15 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$borderRightFontBoldStyle = (new StyleMerger())->merge($borderRightStyle, $fontStyle);
|
||||
|
||||
$dataRows = [
|
||||
['Border-Left'],
|
||||
['Empty'],
|
||||
['Font-Bold'],
|
||||
['Border-Right'],
|
||||
['Border-Right-Font-Bold'],
|
||||
$this->createStyledRowFromValues(['Border-Left'], $borderLeftStyle),
|
||||
$this->createStyledRowFromValues(['Empty'], $emptyStyle),
|
||||
$this->createStyledRowFromValues(['Font-Bold'], $fontStyle),
|
||||
$this->createStyledRowFromValues(['Border-Right'], $borderRightStyle),
|
||||
$this->createStyledRowFromValues(['Border-Right-Font-Bold'], $borderRightFontBoldStyle),
|
||||
];
|
||||
|
||||
$styles = [
|
||||
$borderLeftStyle,
|
||||
$emptyStyle,
|
||||
$fontStyle,
|
||||
$borderRightStyle,
|
||||
$borderRightFontBoldStyle,
|
||||
];
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
|
||||
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
|
||||
|
||||
$this->assertEquals(3, $borderElements->getAttribute('count'), '3 borders in count attribute');
|
||||
@@ -514,12 +476,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param Row[] $allRows
|
||||
* @param string $fileName
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
|
||||
* @return Writer
|
||||
*/
|
||||
private function writeToXLSXFile($allRows, $fileName, $style)
|
||||
private function writeToXLSXFile($allRows, $fileName)
|
||||
{
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
@@ -529,14 +490,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$writer->setShouldUseInlineStrings(true);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
$writer->addRowsWithStyle($allRows, $style);
|
||||
$writer->addRows($allRows);
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param Row[] $allRows
|
||||
* @param string $fileName
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle
|
||||
* @return Writer
|
||||
@@ -558,37 +519,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
return $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles
|
||||
* @return Writer
|
||||
*/
|
||||
private function writeToXLSXFileWithMultipleStyles($allRows, $fileName, $styles)
|
||||
{
|
||||
// there should be as many rows as there are styles passed in
|
||||
$this->assertEquals(count($allRows), count($styles));
|
||||
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->setShouldUseInlineStrings(true);
|
||||
|
||||
$writer->openToFile($resourcePath);
|
||||
for ($i = 0; $i < count($allRows); $i++) {
|
||||
if ($styles[$i] === null) {
|
||||
$writer->addRow($allRows[$i]);
|
||||
} else {
|
||||
$writer->addRowWithStyle($allRows[$i], $styles[$i]);
|
||||
}
|
||||
}
|
||||
$writer->close();
|
||||
|
||||
return $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param string $section
|
||||
|
||||
Reference in new issue
Block a user