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:
Adrien Loison 2017-11-04 18:48:59 +01:00
parent fec27e9056
commit 7274226b75
32 changed files with 1113 additions and 859 deletions

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\CSV;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\WriterAbstract; use Box\Spout\Writer\WriterAbstract;
/** /**
@ -78,20 +79,18 @@ class Writer extends WriterAbstract
} }
/** /**
* Adds data to the currently opened writer. * Adds a row to the currently opened writer.
* *
* @param array $dataRow Array containing data to be written. * @param Row $row The row containing cells and styles
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * @throws IOException If unable to write data
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style Ignored here since CSV does not support styling.
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return void * @return void
*/ */
protected function addRowToWriter(array $dataRow, $style) protected function addRowToWriter(Row $row)
{ {
$fieldDelimiter = $this->optionsManager->getOption(Options::FIELD_DELIMITER); $fieldDelimiter = $this->optionsManager->getOption(Options::FIELD_DELIMITER);
$fieldEnclosure = $this->optionsManager->getOption(Options::FIELD_ENCLOSURE); $fieldEnclosure = $this->optionsManager->getOption(Options::FIELD_ENCLOSURE);
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $fieldDelimiter, $fieldEnclosure); $wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $row->getCells(), $fieldDelimiter, $fieldEnclosure);
if ($wasWriteSuccessful === false) { if ($wasWriteSuccessful === false) {
throw new IOException('Unable to write data'); throw new IOException('Unable to write data');
} }

View File

@ -3,10 +3,14 @@
namespace Box\Spout\Writer\Common\Creator; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\Common\Manager\SheetManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
/** /**
* Class EntityFactory * Class EntityFactory
@ -47,7 +51,7 @@ class EntityFactory
* @param mixed $cellValue * @param mixed $cellValue
* @return Cell * @return Cell
*/ */
public function createCell($cellValue) public static function createCell($cellValue)
{ {
return new Cell($cellValue); return new Cell($cellValue);
} }
@ -59,4 +63,17 @@ class EntityFactory
{ {
return new \ZipArchive(); return new \ZipArchive();
} }
/**
* @param array $cells
* @param Style|null $style
* @return Row
*/
public static function createRow(array $cells, Style $style = null)
{
$styleMerger = new StyleMerger();
$rowManager = new RowManager($styleMerger);
return new Row($cells, $style, $rowManager);
}
} }

View File

@ -2,7 +2,9 @@
namespace Box\Spout\Writer\Common\Entity; namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
/** /**
* Class Cell * Class Cell
@ -53,16 +55,29 @@ class Cell
protected $type; protected $type;
/** /**
* Cell constructor. * The cell style
* @param $value mixed * @var Style
*/ */
public function __construct($value) protected $style;
/**
* @var StyleMerger
*/
protected $styleMerger;
/**
* @param $value mixed
* @param Style|null $style
*/
public function __construct($value, Style $style = null)
{ {
$this->setValue($value); $this->setValue($value);
$this->setStyle($style);
$this->styleMerger = new StyleMerger();
} }
/** /**
* @param $value mixed|null * @param mixed|null $value
*/ */
public function setValue($value) public function setValue($value)
{ {
@ -78,6 +93,22 @@ class Cell
return $this->value; return $this->value;
} }
/**
* @param Style|null $style
*/
public function setStyle($style)
{
$this->style = $style ?: new Style();
}
/**
* @return Style
*/
public function getStyle()
{
return $this->style;
}
/** /**
* @return int|null * @return int|null
*/ */
@ -88,6 +119,7 @@ class Cell
/** /**
* Get the current value type * Get the current value type
*
* @param mixed|null $value * @param mixed|null $value
* @return int * @return int
*/ */
@ -127,6 +159,7 @@ class Cell
/** /**
* Not used at the moment * Not used at the moment
*
* @return bool * @return bool
*/ */
public function isFormula() public function isFormula()
@ -165,4 +198,20 @@ class Cell
{ {
return (string) $this->value; return (string) $this->value;
} }
/**
* @param Style|null $style
* @return Cell
*/
public function applyStyle($style)
{
if ($style === null) {
return $this;
}
$mergedStyle = $this->styleMerger->merge($this->style, $style);
$this->setStyle($mergedStyle);
return $this;
}
} }

View File

@ -0,0 +1,126 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\RowManager;
class Row
{
/**
* The cells in this row
* @var Cell[]
*/
protected $cells = [];
/**
* The row style
* @var Style
*/
protected $style;
/**
* Thw row manager
* @var RowManager
*/
protected $rowManager;
/**
* Row constructor.
* @param Cell[] $cells
* @param Style|null $style
* @param RowManager $rowManager
*/
public function __construct(array $cells, $style, RowManager $rowManager)
{
$this
->setCells($cells)
->setStyle($style);
$this->rowManager = $rowManager;
}
/**
* @return Cell[] $cells
*/
public function getCells()
{
return $this->cells;
}
/**
* @param Cell[] $cells
* @return $this
*/
public function setCells(array $cells)
{
$this->cells = [];
foreach ($cells as $cell) {
$this->addCell($cell);
}
return $this;
}
/**
* @return Style
*/
public function getStyle()
{
return $this->style;
}
/**
* @param Style|null $style
* @return Row
*/
public function setStyle($style)
{
$this->style = $style ?: new Style();
return $this;
}
/**
* @param Style $style
* @return Row
*/
public function applyStyle($style)
{
$this->rowManager->applyStyle($this, $style);
return $this;
}
/**
* @param Cell $cell
* @return Row
*/
public function addCell(Cell $cell)
{
$this->cells[] = $cell;
return $this;
}
/**
* Returns whether a row has cells
*
* @return bool
*/
public function hasCells()
{
return $this->rowManager->hasCells($this);
}
/**
* Detect whether this row is considered empty.
* An empty row has either no cells at all - or only empty cells
*
* @return bool
*/
public function isEmpty()
{
return $this->rowManager->isEmpty($this);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
class CellManager
{
/**
* @var StyleMerger
*/
protected $styleMerger;
/**
* @param StyleMerger $styleMerger
*/
public function __construct(StyleMerger $styleMerger)
{
$this->styleMerger = $styleMerger;
}
/**
* Merges a Style into a cell's Style.
*
* @param Cell $cell
* @param Style $style
* @return void
*/
public function applyStyle(Cell $cell, Style $style)
{
$mergedStyle = $this->styleMerger->merge($cell->getStyle(), $style);
$cell->setStyle($mergedStyle);
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
class RowManager
{
/**
* @var StyleMerger
*/
protected $styleMerger;
/**
* @param StyleMerger $styleMerger
*/
public function __construct(StyleMerger $styleMerger)
{
$this->styleMerger = $styleMerger;
}
/**
* @param Row $row
* @param Style $style
* @return $this
*/
public function applyStyle(Row $row, Style $style)
{
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $style);
$row->setStyle($mergedStyle);
}
/**
* Returns whether a row has cells
*
* @param Row $row
* @return bool
*/
public function hasCells(Row $row)
{
return count($row->getCells()) !== 0;
}
/**
* Detect whether a row is considered empty.
* An empty row has either no cells at all - or only one empty cell
*
* @param Row $row
* @return bool
*/
public function isEmpty(Row $row)
{
$cells = $row->getCells();
return count($cells) === 0 || (count($cells) === 1 && $cells[0]->isEmpty());
}
}

View File

@ -2,6 +2,7 @@
namespace Box\Spout\Writer\Common\Manager\Style; namespace Box\Spout\Writer\Common\Manager\Style;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
@ -48,13 +49,12 @@ class StyleManager implements StyleManagerInterface
* Apply additional styles if the given row needs it. * Apply additional styles if the given row needs it.
* Typically, set "wrap text" if a cell contains a new line. * Typically, set "wrap text" if a cell contains a new line.
* *
* @param Style $style The original style * @param Cell $cell
* @param array $dataRow The row the style will be applied to * @return Style
* @return Style The updated style
*/ */
public function applyExtraStylesIfNeeded($style, $dataRow) public function applyExtraStylesIfNeeded(Cell $cell)
{ {
$updatedStyle = $this->applyWrapTextIfCellContainsNewLine($style, $dataRow); $updatedStyle = $this->applyWrapTextIfCellContainsNewLine($cell);
return $updatedStyle; return $updatedStyle;
} }
@ -68,24 +68,22 @@ class StyleManager implements StyleManagerInterface
* A workaround would be to encode "\n" as "_x000D_" but it does not work * A workaround would be to encode "\n" as "_x000D_" but it does not work
* on the Windows version of Excel... * on the Windows version of Excel...
* *
* @param Style $style The original style * @param Cell $cell The cell the style should be applied to
* @param array $dataRow The row the style will be applied to * @return \Box\Spout\Writer\Common\Entity\Style\Style The eventually updated style
* @return Style The eventually updated style
*/ */
protected function applyWrapTextIfCellContainsNewLine($style, $dataRow) protected function applyWrapTextIfCellContainsNewLine(Cell $cell)
{ {
$cellStyle = $cell->getStyle();
// if the "wrap text" option is already set, no-op // if the "wrap text" option is already set, no-op
if ($style->hasSetWrapText()) { if ($cellStyle->hasSetWrapText()) {
return $style; return $cellStyle;
} }
foreach ($dataRow as $cell) { if ($cell->isString() && strpos($cell->getValue(), "\n") !== false) {
if (is_string($cell) && strpos($cell, "\n") !== false) { $cellStyle->setShouldWrapText();
$style->setShouldWrapText();
break;
}
} }
return $style; return $cellStyle;
} }
} }

View File

@ -2,6 +2,7 @@
namespace Box\Spout\Writer\Common\Manager\Style; namespace Box\Spout\Writer\Common\Manager\Style;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
@ -22,9 +23,8 @@ interface StyleManagerInterface
* Apply additional styles if the given row needs it. * Apply additional styles if the given row needs it.
* Typically, set "wrap text" if a cell contains a new line. * Typically, set "wrap text" if a cell contains a new line.
* *
* @param Style $style The original style * @param Cell $cell
* @param array $dataRow The row the style will be applied to
* @return Style The updated style * @return Style The updated style
*/ */
public function applyExtraStylesIfNeeded($style, $dataRow); public function applyExtraStylesIfNeeded(Cell $cell);
} }

View File

@ -7,8 +7,8 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface; use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
@ -198,21 +198,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
} }
/** /**
* Adds data to the current sheet. * Adds a row to the current sheet.
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
* with the creation of new worksheets if one worksheet has reached its maximum capicity. * with the creation of new worksheets if one worksheet has reached its maximum capicity.
* *
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be added
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $style Style to be applied to the row.
* @throws IOException If trying to create a new sheet and unable to open the sheet for writing * @throws IOException If trying to create a new sheet and unable to open the sheet for writing
* @throws WriterException If unable to write data * @throws WriterException If unable to write data
* @return void * @return void
*/ */
public function addRowToCurrentWorksheet($dataRow, Style $style) public function addRowToCurrentWorksheet(Row $row)
{ {
$currentWorksheet = $this->getCurrentWorksheet(); $currentWorksheet = $this->getCurrentWorksheet();
$hasReachedMaxRows = $this->hasCurrentWorkseetReachedMaxRows(); $hasReachedMaxRows = $this->hasCurrentWorksheetReachedMaxRows();
// if we reached the maximum number of rows for the current sheet... // if we reached the maximum number of rows for the current sheet...
if ($hasReachedMaxRows) { if ($hasReachedMaxRows) {
@ -220,19 +218,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) { if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
$currentWorksheet = $this->addNewSheetAndMakeItCurrent(); $currentWorksheet = $this->addNewSheetAndMakeItCurrent();
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style); $this->addRowToWorksheet($currentWorksheet, $row);
} else { } else {
// otherwise, do nothing as the data won't be written anyways // otherwise, do nothing as the data won't be written anyways
} }
} else { } else {
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style); $this->addRowToWorksheet($currentWorksheet, $row);
} }
} }
/** /**
* @return bool Whether the current worksheet has reached the maximum number of rows per sheet. * @return bool Whether the current worksheet has reached the maximum number of rows per sheet.
*/ */
private function hasCurrentWorkseetReachedMaxRows() private function hasCurrentWorksheetReachedMaxRows()
{ {
$currentWorksheet = $this->getCurrentWorksheet(); $currentWorksheet = $this->getCurrentWorksheet();
@ -240,24 +238,20 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
} }
/** /**
* Adds data with the given style to the given sheet. * Adds a row to the given sheet.
* *
* @param Worksheet $worksheet Worksheet to write the row to * @param Worksheet $worksheet Worksheet to write the row to
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be added
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $style Style to be applied to the row.
* @throws WriterException If unable to write data * @throws WriterException If unable to write data
* @return void * @return void
*/ */
private function addRowWithStyleToWorksheet(Worksheet $worksheet, $dataRow, Style $style) private function addRowToWorksheet(Worksheet $worksheet, Row $row)
{ {
$updatedStyle = $this->styleManager->applyExtraStylesIfNeeded($style, $dataRow); $this->worksheetManager->addRow($worksheet, $row);
$registeredStyle = $this->styleManager->registerStyle($updatedStyle);
$this->worksheetManager->addRow($worksheet, $dataRow, $registeredStyle);
// update max num columns for the worksheet // update max num columns for the worksheet
$currentMaxNumColumns = $worksheet->getMaxNumColumns(); $currentMaxNumColumns = $worksheet->getMaxNumColumns();
$cellsCount = count($dataRow); $cellsCount = count($row->getCells());
$worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount)); $worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount));
} }

View File

@ -3,8 +3,8 @@
namespace Box\Spout\Writer\Common\Manager; namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
@ -53,18 +53,16 @@ interface WorkbookManagerInterface
public function setCurrentSheet(Sheet $sheet); public function setCurrentSheet(Sheet $sheet);
/** /**
* Adds data to the current sheet. * Adds a row to the current sheet.
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
* with the creation of new worksheets if one worksheet has reached its maximum capicity. * with the creation of new worksheets if one worksheet has reached its maximum capicity.
* *
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be added
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $style Style to be applied to the row.
* @throws IOException If trying to create a new sheet and unable to open the sheet for writing * @throws IOException If trying to create a new sheet and unable to open the sheet for writing
* @throws WriterException If unable to write data * @throws WriterException If unable to write data
* @return void * @return void
*/ */
public function addRowToCurrentWorksheet($dataRow, Style $style); public function addRowToCurrentWorksheet(Row $row);
/** /**
* Closes the workbook and all its associated sheets. * Closes the workbook and all its associated sheets.

View File

@ -2,7 +2,7 @@
namespace Box\Spout\Writer\Common\Manager; namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
/** /**
@ -12,17 +12,15 @@ use Box\Spout\Writer\Common\Entity\Worksheet;
interface WorksheetManagerInterface interface WorksheetManagerInterface
{ {
/** /**
* Adds data to the worksheet. * Adds a row to the worksheet.
* *
* @param Worksheet $worksheet The worksheet to add the row to * @param Worksheet $worksheet The worksheet to add the row to
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be added
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $rowStyle Style to be applied to the row. NULL means use default style.
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
* @return void * @return void
*/ */
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle); public function addRow(Worksheet $worksheet, Row $row);
/** /**
* Prepares the worksheet to accept data * Prepares the worksheet to accept data

View File

@ -46,7 +46,7 @@ class ManagerFactory implements ManagerFactoryInterface
$fileSystemHelper->createBaseFilesAndFolders(); $fileSystemHelper->createBaseFilesAndFolders();
$styleManager = $this->createStyleManager($optionsManager); $styleManager = $this->createStyleManager($optionsManager);
$worksheetManager = $this->createWorksheetManager(); $worksheetManager = $this->createWorksheetManager($styleManager);
return new WorkbookManager( return new WorkbookManager(
$workbook, $workbook,
@ -60,14 +60,15 @@ class ManagerFactory implements ManagerFactoryInterface
} }
/** /**
* @param StyleManager $styleManager
* @return WorksheetManager * @return WorksheetManager
*/ */
private function createWorksheetManager() private function createWorksheetManager(StyleManager $styleManager)
{ {
$stringsEscaper = $this->helperFactory->createStringsEscaper(); $stringsEscaper = $this->helperFactory->createStringsEscaper();
$stringsHelper = $this->helperFactory->createStringHelper(); $stringsHelper = $this->helperFactory->createStringHelper();
return new WorksheetManager($stringsEscaper, $stringsHelper, $this->entityFactory); return new WorksheetManager($styleManager, $stringsEscaper, $stringsHelper);
} }
/** /**

View File

@ -4,12 +4,14 @@ namespace Box\Spout\Writer\ODS\Manager;
use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper;
use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
/** /**
* Class WorksheetManager * Class WorksheetManager
@ -23,24 +25,24 @@ class WorksheetManager implements WorksheetManagerInterface
/** @var StringHelper String helper */ /** @var StringHelper String helper */
private $stringHelper; private $stringHelper;
/** @var EntityFactory Factory to create entities */ /** @var StyleManager Manages styles */
private $entityFactory; private $styleManager;
/** /**
* WorksheetManager constructor. * WorksheetManager constructor.
* *
* @param \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper * @param StyleManager $styleManager
* @param ODSEscaper $stringsEscaper
* @param StringHelper $stringHelper * @param StringHelper $stringHelper
* @param EntityFactory $entityFactory
*/ */
public function __construct( public function __construct(
\Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper, StyleManager $styleManager,
StringHelper $stringHelper, ODSEscaper $stringsEscaper,
EntityFactory $entityFactory StringHelper $stringHelper
) { ) {
$this->styleManager = $styleManager;
$this->stringsEscaper = $stringsEscaper; $this->stringsEscaper = $stringsEscaper;
$this->stringHelper = $stringHelper; $this->stringHelper = $stringHelper;
$this->entityFactory = $entityFactory;
} }
/** /**
@ -91,24 +93,19 @@ class WorksheetManager implements WorksheetManagerInterface
} }
/** /**
* Adds data to the given worksheet. * Adds a row to the given worksheet.
* *
* @param Worksheet $worksheet The worksheet to add the row to * @param Worksheet $worksheet The worksheet to add the row to
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be added
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $rowStyle Style to be applied to the row. NULL means use default style.
* @throws IOException If the data cannot be written * @throws IOException If the data cannot be written
* @throws InvalidArgumentException If a cell value's type is not supported * @throws InvalidArgumentException If a cell value's type is not supported
* @return void * @return void
*/ */
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle) public function addRow(Worksheet $worksheet, Row $row)
{ {
// $dataRow can be an associative array. We need to transform $cells = $row->getCells();
// it into a regular array, as we'll use the numeric indexes. $cellsCount = count($cells);
$dataRowWithNumericIndexes = array_values($dataRow); $rowStyle = $row->getStyle();
$styleIndex = ($rowStyle->getId() + 1); // 1-based
$cellsCount = count($dataRow);
$data = '<table:table-row table:style-name="ro1">'; $data = '<table:table-row table:style-name="ro1">';
@ -116,14 +113,13 @@ class WorksheetManager implements WorksheetManagerInterface
$nextCellIndex = 1; $nextCellIndex = 1;
for ($i = 0; $i < $cellsCount; $i++) { for ($i = 0; $i < $cellsCount; $i++) {
$currentCellValue = $dataRowWithNumericIndexes[$currentCellIndex]; /** @var Cell $cell */
$cell = $cells[$currentCellIndex];
// Using isset here because it is way faster than array_key_exists... /** @var Cell|null $nextCell */
if (!isset($dataRowWithNumericIndexes[$nextCellIndex]) || $nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null;
$currentCellValue !== $dataRowWithNumericIndexes[$nextCellIndex]) {
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
$data .= $this->getCellXML($currentCellValue, $styleIndex, $numTimesValueRepeated);
if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) {
$data .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $currentCellIndex, $nextCellIndex);
$currentCellIndex = $nextCellIndex; $currentCellIndex = $nextCellIndex;
} }
@ -142,16 +138,40 @@ class WorksheetManager implements WorksheetManagerInterface
$worksheet->setLastWrittenRowIndex($lastWrittenRowIndex + 1); $worksheet->setLastWrittenRowIndex($lastWrittenRowIndex + 1);
} }
/**
* Applies styles to the given style, merging the cell's style with its row's style
* Then builds and returns xml for the cell.
*
* @param Cell $cell
* @param Style $rowStyle
* @param int $currentCellIndex
* @param int $nextCellIndex
* @throws InvalidArgumentException If a cell value's type is not supported
* @return string
*/
private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $currentCellIndex, $nextCellIndex)
{
// Apply styles - the row style is merged at this point
$cell->applyStyle($rowStyle);
$this->styleManager->applyExtraStylesIfNeeded($cell);
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle());
$styleIndex = $registeredStyle->getId() + 1; // 1-based
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
return $this->getCellXML($cell, $styleIndex, $numTimesValueRepeated);
}
/** /**
* Returns the cell XML content, given its value. * Returns the cell XML content, given its value.
* *
* @param mixed $cellValue The value to be written * @param Cell $cell The cell to be written
* @param int $styleIndex Index of the used style * @param int $styleIndex Index of the used style
* @param int $numTimesValueRepeated Number of times the value is consecutively repeated * @param int $numTimesValueRepeated Number of times the value is consecutively repeated
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported * @throws InvalidArgumentException If a cell value's type is not supported
* @return string The cell XML content * @return string The cell XML content
*/ */
private function getCellXML($cellValue, $styleIndex, $numTimesValueRepeated) private function getCellXML(Cell $cell, $styleIndex, $numTimesValueRepeated)
{ {
$data = '<table:table-cell table:style-name="ce' . $styleIndex . '"'; $data = '<table:table-cell table:style-name="ce' . $styleIndex . '"';
@ -159,13 +179,6 @@ class WorksheetManager implements WorksheetManagerInterface
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"'; $data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
} }
/* @TODO Remove code duplication with XLSX writer: https://github.com/box/spout/pull/383#discussion_r113292746 */
if ($cellValue instanceof Cell) {
$cell = $cellValue;
} else {
$cell = $this->entityFactory->createCell($cellValue);
}
if ($cell->isString()) { if ($cell->isString()) {
$data .= ' office:value-type="string" calcext:value-type="string">'; $data .= ' office:value-type="string" calcext:value-type="string">';

View File

@ -9,6 +9,7 @@ use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
@ -42,9 +43,6 @@ abstract class WriterAbstract implements WriterInterface
/** @var StyleMerger Helps merge styles together */ /** @var StyleMerger Helps merge styles together */
protected $styleMerger; protected $styleMerger;
/** @var Style Style to be applied to the next written row(s) */
protected $rowStyle;
/** @var string Content-Type value for the header - to be defined by child class */ /** @var string Content-Type value for the header - to be defined by child class */
protected static $headerContentType; protected static $headerContentType;
@ -64,27 +62,25 @@ abstract class WriterAbstract implements WriterInterface
$this->styleMerger = $styleMerger; $this->styleMerger = $styleMerger;
$this->globalFunctionsHelper = $globalFunctionsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper;
$this->helperFactory = $helperFactory; $this->helperFactory = $helperFactory;
$this->resetRowStyleToDefault();
} }
/** /**
* Opens the streamer and makes it ready to accept data. * Opens the streamer and makes it ready to accept data.
* *
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened * @throws IOException If the writer cannot be opened
* @return void * @return void
*/ */
abstract protected function openWriter(); abstract protected function openWriter();
/** /**
* Adds data to the currently openned writer. * Adds a row to the currently opened writer.
* *
* @param array $dataRow Array containing data to be streamed. * @param Row $row The row containing cells and styles
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * @throws WriterNotOpenedException If the workbook is not created yet
* @param Style $style Style to be applied to the written row * @throws IOException If unable to write data
* @return void * @return void
*/ */
abstract protected function addRowToWriter(array $dataRow, $style); abstract protected function addRowToWriter(Row $row);
/** /**
* Closes the streamer, preventing any additional writing. * Closes the streamer, preventing any additional writing.
@ -94,29 +90,17 @@ abstract class WriterAbstract implements WriterInterface
abstract protected function closeWriter(); abstract protected function closeWriter();
/** /**
* Sets the default styles for all rows added with "addRow". * {@inheritdoc}
* Overriding the default style instead of using "addRowWithStyle" improves performance by 20%.
* @see https://github.com/box/spout/issues/272
*
* @param Style $defaultStyle
* @return WriterAbstract
*/ */
public function setDefaultRowStyle($defaultStyle) public function setDefaultRowStyle(Style $defaultStyle)
{ {
$this->optionsManager->setOption(Options::DEFAULT_ROW_STYLE, $defaultStyle); $this->optionsManager->setOption(Options::DEFAULT_ROW_STYLE, $defaultStyle);
$this->resetRowStyleToDefault();
return $this; return $this;
} }
/** /**
* Inits the writer and opens it to accept data. * {@inheritdoc}
* By using this method, the data will be written to a file.
*
* @api
* @param string $outputFilePath Path of the output file that will contain the data
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable
* @return WriterAbstract
*/ */
public function openToFile($outputFilePath) public function openToFile($outputFilePath)
{ {
@ -132,15 +116,8 @@ abstract class WriterAbstract implements WriterInterface
} }
/** /**
* Inits the writer and opens it to accept data.
* By using this method, the data will be outputted directly to the browser.
*
* @codeCoverageIgnore * @codeCoverageIgnore
* * {@inheritdoc}
* @api
* @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened
* @return WriterAbstract
*/ */
public function openToBrowser($outputFileName) public function openToBrowser($outputFileName)
{ {
@ -177,7 +154,7 @@ abstract class WriterAbstract implements WriterInterface
* Checks if the pointer to the file/stream to write to is available. * Checks if the pointer to the file/stream to write to is available.
* Will throw an exception if not available. * Will throw an exception if not available.
* *
* @throws \Box\Spout\Common\Exception\IOException If the pointer is not available * @throws IOException If the pointer is not available
* @return void * @return void
*/ */
protected function throwIfFilePointerIsNotAvailable() protected function throwIfFilePointerIsNotAvailable()
@ -192,7 +169,7 @@ abstract class WriterAbstract implements WriterInterface
* Throws an exception if already opened. * Throws an exception if already opened.
* *
* @param string $message Error message * @param string $message Error message
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. * @throws WriterAlreadyOpenedException If the writer was already opened and must not be.
* @return void * @return void
*/ */
protected function throwIfWriterAlreadyOpened($message) protected function throwIfWriterAlreadyOpened($message)
@ -203,24 +180,16 @@ abstract class WriterAbstract implements WriterInterface
} }
/** /**
* Write given data to the output. New data will be appended to end of stream. * {@inheritdoc}
*
* @param array $dataRow Array containing data to be streamed.
* If empty, no data is added (i.e. not even as a blank row)
* Example: $dataRow = ['data1', 1234, null, '', 'data5', false];
* @api
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @throws \Box\Spout\Common\Exception\SpoutException If anything else goes wrong while writing data
* @return WriterAbstract
*/ */
public function addRow(array $dataRow) public function addRow(Row $row)
{ {
if ($this->isWriterOpened) { if ($this->isWriterOpened) {
// empty $dataRow should not add an empty line // empty $dataRow should not add an empty line
if (!empty($dataRow)) { if ($row->hasCells()) {
try { try {
$this->addRowToWriter($dataRow, $this->rowStyle); $this->applyDefaultRowStyle($row);
$this->addRowToWriter($row);
} catch (SpoutException $e) { } catch (SpoutException $e) {
// if an exception occurs while writing data, // if an exception occurs while writing data,
// close the writer and remove all files created so far. // close the writer and remove all files created so far.
@ -238,116 +207,41 @@ abstract class WriterAbstract implements WriterInterface
} }
/** /**
* Write given data to the output and apply the given style. * {@inheritdoc}
* @see addRow
*
* @api
* @param array $dataRow Array of array containing data to be streamed.
* @param Style $style Style to be applied to the row.
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterAbstract
*/ */
public function addRowWithStyle(array $dataRow, $style) public function addRows(array $rows)
{ {
if (!$style instanceof Style) { foreach ($rows as $row) {
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.'); if (!$row instanceof Row) {
} $this->closeAndAttemptToCleanupAllFiles();
throw new InvalidArgumentException('The input should be an array of Row');
$this->setRowStyle($style);
$this->addRow($dataRow);
$this->resetRowStyleToDefault();
return $this;
}
/**
* Write given data to the output. New data will be appended to end of stream.
*
* @api
* @param array $dataRows Array of array containing data to be streamed.
* If a row is empty, it won't be added (i.e. not even as a blank row)
* Example: $dataRows = [
* ['data11', 12, , '', 'data13'],
* ['data21', 'data22', null, false],
* ];
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterAbstract
*/
public function addRows(array $dataRows)
{
if (!empty($dataRows)) {
$firstRow = reset($dataRows);
if (!is_array($firstRow)) {
throw new InvalidArgumentException('The input should be an array of arrays');
} }
foreach ($dataRows as $dataRow) { $this->addRow($row);
$this->addRow($dataRow);
}
} }
return $this; return $this;
} }
/** /**
* Write given data to the output and apply the given style. * @TODO: Move this into styleMerger
* @see addRows
* *
* @api * @param Row $row
* @param array $dataRows Array of array containing data to be streamed. * @return $this
* @param Style $style Style to be applied to the rows.
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterAbstract
*/ */
public function addRowsWithStyle(array $dataRows, $style) private function applyDefaultRowStyle(Row $row)
{ {
if (!$style instanceof Style) {
throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.');
}
$this->setRowStyle($style);
$this->addRows($dataRows);
$this->resetRowStyleToDefault();
return $this;
}
/**
* Sets the style to be applied to the next written rows
* until it is changed or reset.
*
* @param Style $style
* @return void
*/
private function setRowStyle($style)
{
// Merge given style with the default one to inherit custom properties
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
$this->rowStyle = $this->styleMerger->merge($style, $defaultRowStyle); if ($defaultRowStyle === null) {
return $this;
}
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
$row->setStyle($mergedStyle);
} }
/** /**
* Resets the style to be applied to the next written rows. * {@inheritdoc}
*
* @return void
*/
private function resetRowStyleToDefault()
{
$this->rowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
}
/**
* Closes the writer. This will close the streamer as well, preventing new data
* to be written to the file.
*
* @api
* @return void
*/ */
public function close() public function close()
{ {

View File

@ -2,6 +2,7 @@
namespace Box\Spout\Writer; namespace Box\Spout\Writer;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
@ -10,7 +11,7 @@ use Box\Spout\Writer\Common\Entity\Style\Style;
interface WriterInterface interface WriterInterface
{ {
/** /**
* Inits the writer and opens it to accept data. * Initializes the writer and opens it to accept data.
* By using this method, the data will be written to a file. * By using this method, the data will be written to a file.
* *
* @param string $outputFilePath Path of the output file that will contain the data * @param string $outputFilePath Path of the output file that will contain the data
@ -20,7 +21,7 @@ interface WriterInterface
public function openToFile($outputFilePath); public function openToFile($outputFilePath);
/** /**
* Inits the writer and opens it to accept data. * Initializes the writer and opens it to accept data.
* By using this method, the data will be outputted directly to the browser. * By using this method, the data will be outputted directly to the browser.
* *
* @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept * @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept
@ -30,56 +31,35 @@ interface WriterInterface
public function openToBrowser($outputFileName); public function openToBrowser($outputFileName);
/** /**
* Write given data to the output. New data will be appended to end of stream. * Sets the default styles for all rows added with "addRow".
* Overriding the default style instead of using "addRowWithStyle" improves performance by 20%.
* @see https://github.com/box/spout/issues/272
* *
* @param array $dataRow Array containing data to be streamed. * @param Style $defaultStyle
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * @return WriterInterface
*/
public function setDefaultRowStyle(Style $defaultStyle);
/**
* Appends a row to the end of the stream.
*
* @param Row $row The row to be appended to the stream
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yetthe writer * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yetthe writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data * @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterInterface * @return WriterInterface
*/ */
public function addRow(array $dataRow); public function addRow(Row $row);
/** /**
* Write given data to the output and apply the given style. * Appends the rows to the end of the stream.
* @see addRow
* *
* @param array $dataRow Array of array containing data to be streamed. * @param Row[] $rows The rows to be appended to the stream
* @param Style $style Style to be applied to the row.
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterInterface
*/
public function addRowWithStyle(array $dataRow, $style);
/**
* Write given data to the output. New data will be appended to end of stream.
*
* @param array $dataRows Array of array containing data to be streamed.
* Example $dataRow = [
* ['data11', 12, , '', 'data13'],
* ['data21', 'data22', null],
* ];
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
* @throws \Box\Spout\Common\Exception\IOException If unable to write data * @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterInterface * @return WriterInterface
*/ */
public function addRows(array $dataRows); public function addRows(array $rows);
/**
* Write given data to the output and apply the given style.
* @see addRows
*
* @param array $dataRows Array of array containing data to be streamed.
* @param Style $style Style to be applied to the rows.
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return WriterInterface
*/
public function addRowsWithStyle(array $dataRows, $style);
/** /**
* Closes the writer. This will close the streamer as well, preventing new data * Closes the writer. This will close the streamer as well, preventing new data

View File

@ -7,6 +7,7 @@ use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
@ -65,10 +66,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
} }
/** /**
* Configures the write and sets the current sheet pointer to a new sheet. * {@inheritdoc}
*
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
* @return void
*/ */
protected function openWriter() protected function openWriter()
{ {
@ -159,27 +157,16 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
} }
/** /**
* Adds data to the currently opened writer. * {@inheritdoc}
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
* with the creation of new worksheets if one worksheet has reached its maximum capicity.
*
* @param array $dataRow Array containing data to be written.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row.
* @throws WriterNotOpenedException If the book is not created yet
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
* @return void
*/ */
protected function addRowToWriter(array $dataRow, $style) protected function addRowToWriter(Row $row)
{ {
$this->throwIfWorkbookIsNotAvailable(); $this->throwIfWorkbookIsNotAvailable();
$this->workbookManager->addRowToCurrentWorksheet($dataRow, $style); $this->workbookManager->addRowToCurrentWorksheet($row);
} }
/** /**
* Closes the writer, preventing any additional writing. * {@inheritdoc}
*
* @return void
*/ */
protected function closeWriter() protected function closeWriter()
{ {

View File

@ -4,11 +4,13 @@ namespace Box\Spout\Writer\XLSX\Manager;
use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\Escaper\XLSX as XLSXEscaper;
use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Helper\CellHelper;
@ -43,7 +45,7 @@ EOD;
/** @var SharedStringsManager Helper to write shared strings */ /** @var SharedStringsManager Helper to write shared strings */
private $sharedStringsManager; private $sharedStringsManager;
/** @var \Box\Spout\Common\Helper\Escaper\XLSX Strings escaper */ /** @var XLSXEscaper Strings escaper */
private $stringsEscaper; private $stringsEscaper;
/** @var StringHelper String helper */ /** @var StringHelper String helper */
@ -58,7 +60,7 @@ EOD;
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param StyleManager $styleManager * @param StyleManager $styleManager
* @param SharedStringsManager $sharedStringsManager * @param SharedStringsManager $sharedStringsManager
* @param \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper * @param XLSXEscaper $stringsEscaper
* @param StringHelper $stringHelper * @param StringHelper $stringHelper
* @param EntityFactory $entityFactory * @param EntityFactory $entityFactory
*/ */
@ -66,7 +68,7 @@ EOD;
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
StyleManager $styleManager, StyleManager $styleManager,
SharedStringsManager $sharedStringsManager, SharedStringsManager $sharedStringsManager,
\Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper, XLSXEscaper $stringsEscaper,
StringHelper $stringHelper, StringHelper $stringHelper,
EntityFactory $entityFactory EntityFactory $entityFactory
) { ) {
@ -87,11 +89,7 @@ EOD;
} }
/** /**
* Prepares the worksheet to accept data * {@inheritdoc}
*
* @param Worksheet $worksheet The worksheet to start
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
* @return void
*/ */
public function startSheet(Worksheet $worksheet) public function startSheet(Worksheet $worksheet)
{ {
@ -119,61 +117,38 @@ EOD;
} }
/** /**
* Adds data to the given worksheet. * {@inheritdoc}
*
* @param Worksheet $worksheet The worksheet to add the row to
* @param array $dataRow Array containing data to be written. Cannot be empty.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @param Style $rowStyle Style to be applied to the row. NULL means use default style.
* @throws IOException If the data cannot be written
* @throws InvalidArgumentException If a cell value's type is not supported
* @return void
*/ */
public function addRow(Worksheet $worksheet, $dataRow, $rowStyle) public function addRow(Worksheet $worksheet, Row $row)
{ {
if (!$this->isEmptyRow($dataRow)) { if (!$row->isEmpty()) {
$this->addNonEmptyRow($worksheet, $dataRow, $rowStyle); $this->addNonEmptyRow($worksheet, $row);
} }
$worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1); $worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1);
} }
/**
* Returns whether the given row is empty
*
* @param array $dataRow Array containing data to be written. Cannot be empty.
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @return bool Whether the given row is empty
*/
private function isEmptyRow($dataRow)
{
$numCells = count($dataRow);
// using "reset()" instead of "$dataRow[0]" because $dataRow can be an associative array
return ($numCells === 1 && CellHelper::isEmpty(reset($dataRow)));
}
/** /**
* Adds non empty row to the worksheet. * Adds non empty row to the worksheet.
* *
* @param Worksheet $worksheet The worksheet to add the row to * @param Worksheet $worksheet The worksheet to add the row to
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param Row $row The row to be written
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * @throws IOException If the data cannot be written
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row. NULL means use default style. * @throws InvalidArgumentException If a cell value's type is not supported
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
* @return void * @return void
*/ */
private function addNonEmptyRow(Worksheet $worksheet, $dataRow, $style) private function addNonEmptyRow(Worksheet $worksheet, Row $row)
{ {
$cellNumber = 0; $cellIndex = 0;
$rowStyle = $row->getStyle();
$rowIndex = $worksheet->getLastWrittenRowIndex() + 1; $rowIndex = $worksheet->getLastWrittenRowIndex() + 1;
$numCells = count($dataRow); $numCells = count($row->getCells());
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">'; $rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
foreach ($dataRow as $cellValue) { foreach ($row->getCells() as $cell) {
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId()); $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex);
$cellNumber++; $cellIndex++;
} }
$rowXML .= '</row>'; $rowXML .= '</row>';
@ -185,28 +160,42 @@ EOD;
} }
/** /**
* Build and return xml for a single cell. * Applies styles to the given style, merging the cell's style with its row's style
* Then builds and returns xml for the cell.
*
* @param Cell $cell
* @param Style $rowStyle
* @param int $rowIndex
* @param int $cellIndex
* @throws InvalidArgumentException If the given value cannot be processed
* @return string
*/
private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndex, $cellIndex)
{
// Apply styles - the row style is merged at this point
$cell->applyStyle($rowStyle);
$this->styleManager->applyExtraStylesIfNeeded($cell);
$registeredStyle = $this->styleManager->registerStyle($cell->getStyle());
return $this->getCellXML($rowIndex, $cellIndex, $cell, $registeredStyle->getId());
}
/**
* Builds and returns xml for a single cell.
* *
* @param int $rowIndex * @param int $rowIndex
* @param int $cellNumber * @param int $cellNumber
* @param mixed $cellValue * @param Cell $cell
* @param int $styleId * @param int $styleId
* @throws InvalidArgumentException If the given value cannot be processed * @throws InvalidArgumentException If the given value cannot be processed
* @return string * @return string
*/ */
private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId)
{ {
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"'; $cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
$cellXML .= ' s="' . $styleId . '"'; $cellXML .= ' s="' . $styleId . '"';
/* @TODO Remove code duplication with ODS writer: https://github.com/box/spout/pull/383#discussion_r113292746 */
if ($cellValue instanceof Cell) {
$cell = $cellValue;
} else {
$cell = $this->entityFactory->createCell($cellValue);
}
if ($cell->isString()) { if ($cell->isString()) {
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
} elseif ($cell->isBoolean()) { } elseif ($cell->isBoolean()) {
@ -252,10 +241,7 @@ EOD;
} }
/** /**
* Closes the worksheet * {@inheritdoc}
*
* @param Worksheet $worksheet
* @return void
*/ */
public function close(Worksheet $worksheet) public function close(Worksheet $worksheet)
{ {

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Reader\XLSX\Helper; namespace Box\Spout\Reader\XLSX\Helper;
use Box\Spout\Common\Helper\Escaper; use Box\Spout\Common\Helper\Escaper;
use Box\Spout\Reader\XLSX\Manager\StyleManager;
/** /**
* Class CellValueFormatterTest * Class CellValueFormatterTest
@ -61,7 +62,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testExcelDate($shouldUse1904Dates, $nodeValue, $expectedDateAsString) public function testExcelDate($shouldUse1904Dates, $nodeValue, $expectedDateAsString)
{ {
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock(); $nodeListMock = $this->createMock(\DOMNodeList::class);
$nodeListMock $nodeListMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
@ -69,7 +70,7 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
->with(0) ->with(0)
->will($this->returnValue((object) ['nodeValue' => $nodeValue])); ->will($this->returnValue((object) ['nodeValue' => $nodeValue]));
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock(); $nodeMock = $this->createMock(\DOMElement::class);
$nodeMock $nodeMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
@ -85,8 +86,8 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
->with(CellValueFormatter::XML_NODE_VALUE) ->with(CellValueFormatter::XML_NODE_VALUE)
->will($this->returnValue($nodeListMock)); ->will($this->returnValue($nodeListMock));
/** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ /** @var StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); $styleManagerMock = $this->createMock(StyleManager::class);
$styleManagerMock $styleManagerMock
->expects($this->once()) ->expects($this->once())
@ -141,8 +142,8 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType) public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType)
{ {
/** @var \Box\Spout\Reader\XLSX\Manager\StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */ /** @var StyleManager|\PHPUnit_Framework_MockObject_MockObject $styleManagerMock */
$styleManagerMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Manager\StyleManager')->disableOriginalConstructor()->getMock(); $styleManagerMock = $this->createMock(StyleManager::class);
$styleManagerMock $styleManagerMock
->expects($this->once()) ->expects($this->once())
->method('shouldFormatNumericValueAsDate') ->method('shouldFormatNumericValueAsDate')
@ -177,14 +178,14 @@ class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatInlineStringCellValue($value, $expectedFormattedValue) public function testFormatInlineStringCellValue($value, $expectedFormattedValue)
{ {
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock(); $nodeListMock = $this->createMock(\DOMNodeList::class);
$nodeListMock $nodeListMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
->method('item') ->method('item')
->with(0) ->with(0)
->will($this->returnValue((object) ['nodeValue' => $value])); ->will($this->returnValue((object) ['nodeValue' => $value]));
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock(); $nodeMock = $this->createMock(\DOMElement::class);
$nodeMock $nodeMock
->expects($this->atLeastOnce()) ->expects($this->atLeastOnce())
->method('getElementsByTagName') ->method('getElementsByTagName')

View File

@ -5,7 +5,8 @@ namespace Box\Spout\Writer\CSV;
use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; 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\WriterFactory;
/** /**
@ -14,6 +15,7 @@ use Box\Spout\Writer\WriterFactory;
class WriterTest extends \PHPUnit_Framework_TestCase class WriterTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** /**
* @expectedException \Box\Spout\Common\Exception\IOException * @expectedException \Box\Spout\Common\Exception\IOException
@ -26,7 +28,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
$writer->addRow(['csv--11', 'csv--12']); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
} }
@ -36,7 +38,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRow(['csv--11', 'csv--12']); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
} }
@ -46,7 +48,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter() public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRows([['csv--11', 'csv--12']]); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
} }
@ -56,7 +58,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays() public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays()
{ {
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->addRows(['csv--11', 'csv--12']); $writer->addRows([['csv--11', 'csv--12']]);
$writer->close(); $writer->close();
} }
@ -82,9 +84,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWriteShouldAddUtf8Bom() public function testWriteShouldAddUtf8Bom()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['csv--11', 'csv--12'], ['csv--11', 'csv--12'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_utf8_bom.csv'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_utf8_bom.csv');
$this->assertContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should contain a UTF-8 BOM'); $this->assertContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should contain a UTF-8 BOM');
@ -95,36 +97,22 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWriteShouldNotAddUtf8Bom() public function testWriteShouldNotAddUtf8Bom()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['csv--11', 'csv--12'], ['csv--11', 'csv--12'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_no_bom.csv', ',', '"', false); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_no_bom.csv', ',', '"', false);
$this->assertNotContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should not contain a UTF-8 BOM'); $this->assertNotContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should not contain a UTF-8 BOM');
} }
/**
* @return void
*/
public function testWriteShouldSupportAssociativeArrays()
{
$allRows = [
['foo' => 'csv--11', 'bar' => 'csv--12'],
];
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_from_associative_arrays.csv');
$writtenContent = $this->trimWrittenContent($writtenContent);
$this->assertEquals('csv--11,csv--12', $writtenContent, 'Values from associative arrays should be written');
}
/** /**
* @return void * @return void
*/ */
public function testWriteShouldSupportNullValues() public function testWriteShouldSupportNullValues()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['csv--11', null, 'csv--13'], ['csv--11', null, 'csv--13'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_null_values.csv'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_null_values.csv');
$writtenContent = $this->trimWrittenContent($writtenContent); $writtenContent = $this->trimWrittenContent($writtenContent);
@ -136,11 +124,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWriteShouldSkipEmptyRows() public function testWriteShouldSkipEmptyRows()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['csv--11', 'csv--12'], ['csv--11', 'csv--12'],
[], [],
['csv--31', 'csv--32'], ['csv--31', 'csv--32'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_empty_rows.csv'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_empty_rows.csv');
$writtenContent = $this->trimWrittenContent($writtenContent); $writtenContent = $this->trimWrittenContent($writtenContent);
@ -152,10 +140,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWriteShouldSupportCustomFieldDelimiter() public function testWriteShouldSupportCustomFieldDelimiter()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['csv--11', 'csv--12', 'csv--13'], ['csv--11', 'csv--12', 'csv--13'],
['csv--21', 'csv--22', 'csv--23'], ['csv--21', 'csv--22', 'csv--23'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_pipe_delimiters.csv', '|'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_pipe_delimiters.csv', '|');
$writtenContent = $this->trimWrittenContent($writtenContent); $writtenContent = $this->trimWrittenContent($writtenContent);
@ -167,9 +155,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testWriteShouldSupportCustomFieldEnclosure() public function testWriteShouldSupportCustomFieldEnclosure()
{ {
$allRows = [ $allRows = $this->createRowsFromValues([
['This is, a comma', 'csv--12', 'csv--13'], ['This is, a comma', 'csv--12', 'csv--13'],
]; ]);
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_pound_enclosures.csv', ',', '#'); $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_pound_enclosures.csv', ',', '#');
$writtenContent = $this->trimWrittenContent($writtenContent); $writtenContent = $this->trimWrittenContent($writtenContent);
@ -177,20 +165,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @return void * @param Row[] $allRows
*/
public function testWriteShouldAcceptCellObjects()
{
$allRows = [
[new Cell('String Value'), new Cell(1)],
];
$writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_cell_objects.csv');
$writtenContent = $this->trimWrittenContent($writtenContent);
$this->assertEquals('"String Value",1', $writtenContent);
}
/**
* @param array $allRows
* @param string $fileName * @param string $fileName
* @param string $fieldDelimiter * @param string $fieldDelimiter
* @param string $fieldEnclosure * @param string $fieldEnclosure

View File

@ -0,0 +1,68 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
use PHPUnit\Framework\TestCase;
class CellTest extends TestCase
{
/**
* @return void
*/
public function testValidInstance()
{
$this->assertInstanceOf(Cell::class, new Cell('cell'));
$this->assertInstanceOf(Cell::class, new Cell('cell-with-style', $this->createMock(Style::class)));
}
/**
* @return void
*/
public function testCellTypeNumeric()
{
$this->assertTrue((new Cell(0))->isNumeric());
$this->assertTrue((new Cell(1))->isNumeric());
}
/**
* @return void
*/
public function testCellTypeString()
{
$this->assertTrue((new Cell('String!'))->isString());
}
/**
* @return void
*/
public function testCellTypeEmptyString()
{
$this->assertTrue((new Cell(''))->isEmpty());
}
/**
* @return void
*/
public function testCellTypeEmptyNull()
{
$this->assertTrue((new Cell(null))->isEmpty());
}
/**
* @return void
*/
public function testCellTypeBool()
{
$this->assertTrue((new Cell(true))->isBoolean());
$this->assertTrue((new Cell(false))->isBoolean());
}
/**
* @return void
*/
public function testCellTypeError()
{
$this->assertTrue((new Cell([]))->isError());
}
}

View File

@ -0,0 +1,114 @@
<?php
namespace Box\Spout\Writer\Common\Entity;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\RowManager;
use PHPUnit\Framework\TestCase;
class RowTest extends TestCase
{
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Style
*/
private function getStyleMock()
{
return $this->createMock(Style::class);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Cell
*/
private function getCellMock()
{
return $this->createMock(Cell::class);
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|RowManager
*/
private function getRowManagerMock()
{
return $this->createMock(RowManager::class);
}
/**
* @return void
*/
public function testValidInstance()
{
$this->assertInstanceOf(
Row::class,
new Row([], null, $this->getRowManagerMock())
);
}
/**
* @return void
*/
public function testSetCells()
{
$row = new Row([], null, $this->getRowManagerMock());
$row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($row->getCells()));
}
/**
* @return void
*/
public function testSetCellsResets()
{
$row = new Row([], null, $this->getRowManagerMock());
$row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($row->getCells()));
$row->setCells([$this->getCellMock()]);
$this->assertEquals(1, count($row->getCells()));
}
/**
* @return void
*/
public function testGetCells()
{
$row = new Row([], null, $this->getRowManagerMock());
$this->assertEquals(0, count($row->getCells()));
$row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($row->getCells()));
}
/**
* @return void
*/
public function testAddCell()
{
$row = new Row([], null, $this->getRowManagerMock());
$row->setCells([$this->getCellMock(), $this->getCellMock()]);
$this->assertEquals(2, count($row->getCells()));
$row->addCell($this->getCellMock());
$this->assertEquals(3, count($row->getCells()));
}
/**
* @return void
*/
public function testFluentInterface()
{
$row = new Row([], null, $this->getRowManagerMock());
$row
->addCell($this->getCellMock())
->setStyle($this->getStyleMock())
->setCells([]);
$this->assertTrue(is_object($row));
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Manager\CellManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use PHPUnit\Framework\TestCase;
class CellManagerTest extends TestCase
{
/**
* @return void
*/
public function testApplyStyle()
{
$cellManager = new CellManager(new StyleMerger());
$cell = new Cell('test');
$this->assertFalse($cell->getStyle()->isFontBold());
$style = (new StyleBuilder())->setFontBold()->build();
$cellManager->applyStyle($cell, $style);
$this->assertTrue($cell->getStyle()->isFontBold());
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use PHPUnit\Framework\TestCase;
class RowManagerTest extends TestCase
{
/**
* @return void
*/
public function testApplyStyle()
{
$rowManager = new RowManager(new StyleMerger());
$row = new Row([new Cell('test')], null, $rowManager);
$this->assertFalse($row->getStyle()->isFontBold());
$style = (new StyleBuilder())->setFontBold()->build();
$rowManager->applyStyle($row, $style);
$this->assertTrue($row->getStyle()->isFontBold());
}
/**
* @return array
*/
public function dataProviderForTestHasCells()
{
return [
// cells, expected hasCells
[[], false],
[[new Cell('')], true],
[[new Cell(null)], true],
[[new Cell('test')], true],
];
}
/**
* @dataProvider dataProviderForTestHasCells
*
* @param array $cells
* @param bool $expectedHasCells
* @return void
*/
public function testHasCells(array $cells, $expectedHasCells)
{
$rowManager = new RowManager(new StyleMerger());
$row = new Row($cells, null, $rowManager);
$this->assertEquals($expectedHasCells, $rowManager->hasCells($row));
}
/**
* @return array
*/
public function dataProviderForTestIsEmptyRow()
{
return [
// cells, expected isEmpty
[[], true],
[[new Cell('')], true],
[[new Cell(''), new Cell(''), new Cell('Okay')], false],
];
}
/**
* @dataProvider dataProviderForTestIsEmptyRow
*
* @param array $cells
* @param bool $expectedIsEmpty
* @return void
*/
public function testIsEmptyRow(array $cells, $expectedIsEmpty)
{
$rowManager = new RowManager(new StyleMerger());
$row = new Row($cells, null, $rowManager);
$this->assertEquals($expectedIsEmpty, $rowManager->isEmpty($row));
}
}

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Manager\Style; namespace Box\Spout\Writer\Common\Manager\Style;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Cell;
/** /**
* Class StyleManagerTest * Class StyleManagerTest
@ -29,7 +30,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($style->shouldWrapText()); $this->assertFalse($style->shouldWrapText());
$styleManager = $this->getStyleManager(); $styleManager = $this->getStyleManager();
$updatedStyle = $styleManager->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]); $updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style));
$this->assertTrue($updatedStyle->shouldWrapText()); $this->assertTrue($updatedStyle->shouldWrapText());
} }
@ -43,7 +44,7 @@ class StyleManagerTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($style->shouldWrapText()); $this->assertTrue($style->shouldWrapText());
$styleManager = $this->getStyleManager(); $styleManager = $this->getStyleManager();
$updatedStyle = $styleManager->applyExtraStylesIfNeeded($style, ["multi\nlines"]); $updatedStyle = $styleManager->applyExtraStylesIfNeeded(new Cell("multi\nlines", $style));
$this->assertTrue($updatedStyle->shouldWrapText()); $this->assertTrue($updatedStyle->shouldWrapText());
} }

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -13,6 +14,7 @@ use Box\Spout\Writer\WriterFactory;
class SheetTest extends \PHPUnit_Framework_TestCase class SheetTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** /**
* @return void * @return void
@ -91,7 +93,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$sheet = $writer->getCurrentSheet(); $sheet = $writer->getCurrentSheet();
$sheet->setName($sheetName); $sheet->setName($sheetName);
$writer->addRow(['ods--11', 'ods--12']); $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
$writer->close(); $writer->close();
} }
@ -108,9 +110,9 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRow(['ods--sheet1--11', 'ods--sheet1--12']); $writer->addRow($this->createRowFromValues(['ods--sheet1--11', 'ods--sheet1--12']));
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$writer->addRow(['ods--sheet2--11', 'ods--sheet2--12', 'ods--sheet2--13']); $writer->addRow($this->createRowFromValues(['ods--sheet2--11', 'ods--sheet2--12', 'ods--sheet2--13']));
$writer->close(); $writer->close();

View File

@ -7,7 +7,9 @@ use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Helper\ZipHelper; use Box\Spout\Writer\Common\Helper\ZipHelper;
use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -16,6 +18,7 @@ use Box\Spout\Writer\WriterFactory;
class WriterTest extends \PHPUnit_Framework_TestCase class WriterTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** /**
* @expectedException \Box\Spout\Common\Exception\IOException * @expectedException \Box\Spout\Common\Exception\IOException
@ -36,7 +39,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->addRow(['ods--11', 'ods--12']); $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
} }
/** /**
@ -45,7 +48,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->addRows([['ods--11', 'ods--12']]); $writer->addRows([$this->createRowFromValues(['ods--11', 'ods--12'])]);
} }
/** /**
@ -85,7 +88,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_add_row_should_throw_exception_if_unsupported_data_type_passed_in.ods'; $fileName = 'test_add_row_should_throw_exception_if_unsupported_data_type_passed_in.ods';
$dataRows = [ $dataRows = [
[new \stdClass()], $this->createRowFromValues([new \stdClass()]),
]; ];
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -98,8 +101,8 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.ods'; $fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.ods';
$dataRows = [ $dataRows = [
['wrong'], $this->createRowFromValues(['wrong']),
[new \stdClass()], $this->createRowFromValues([new \stdClass()]),
]; ];
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
@ -191,10 +194,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToSheet() public function testAddRowShouldWriteGivenDataToSheet()
{ {
$fileName = 'test_add_row_should_write_given_data_to_sheet.ods'; $fileName = 'test_add_row_should_write_given_data_to_sheet.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['ods--11', 'ods--12'], ['ods--11', 'ods--12'],
['ods--21', 'ods--22', 'ods--23'], ['ods--21', 'ods--22', 'ods--23'],
]; ]);
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -211,10 +214,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToTwoSheets() public function testAddRowShouldWriteGivenDataToTwoSheets()
{ {
$fileName = 'test_add_row_should_write_given_data_to_two_sheets.ods'; $fileName = 'test_add_row_should_write_given_data_to_two_sheets.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['ods--11', 'ods--12'], ['ods--11', 'ods--12'],
['ods--21', 'ods--22', 'ods--23'], ['ods--21', 'ods--22', 'ods--23'],
]; ]);
$numSheets = 2; $numSheets = 2;
$this->writeToMultipleSheetsInODSFile($dataRows, $numSheets, $fileName); $this->writeToMultipleSheetsInODSFile($dataRows, $numSheets, $fileName);
@ -234,9 +237,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldSupportAssociativeArrays() public function testAddRowShouldSupportAssociativeArrays()
{ {
$fileName = 'test_add_row_should_support_associative_arrays.ods'; $fileName = 'test_add_row_should_support_associative_arrays.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['foo' => 'ods--11', 'bar' => 'ods--12'], ['foo' => 'ods--11', 'bar' => 'ods--12'],
]; ]);
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -253,9 +256,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldSupportMultipleTypesOfData() public function testAddRowShouldSupportMultipleTypesOfData()
{ {
$fileName = 'test_add_row_should_support_multiple_types_of_data.ods'; $fileName = 'test_add_row_should_support_multiple_types_of_data.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['ods--11', true, '', 0, 10.2, null], ['ods--11', true, '', 0, 10.2, null],
]; ]);
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -290,7 +293,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldUseNumberColumnsRepeatedForRepeatedValues($dataRow, $expectedNumTableCells, $expectedNumColumnsRepeated) public function testAddRowShouldUseNumberColumnsRepeatedForRepeatedValues($dataRow, $expectedNumTableCells, $expectedNumColumnsRepeated)
{ {
$fileName = 'test_add_row_should_use_number_columns_repeated.ods'; $fileName = 'test_add_row_should_use_number_columns_repeated.ods';
$this->writeToODSFile([$dataRow], $fileName); $this->writeToODSFile($this->createRowsFromValues([$dataRow]), $fileName);
$sheetXmlNode = $this->getSheetXmlNode($fileName, 1); $sheetXmlNode = $this->getSheetXmlNode($fileName, 1);
$tableCellNodes = $sheetXmlNode->getElementsByTagName('table-cell'); $tableCellNodes = $sheetXmlNode->getElementsByTagName('table-cell');
@ -314,18 +317,18 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToTheCorrectSheet() public function testAddRowShouldWriteGivenDataToTheCorrectSheet()
{ {
$fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.ods'; $fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.ods';
$dataRowsSheet1 = [ $dataRowsSheet1 = $this->createRowsFromValues([
['ods--sheet1--11', 'ods--sheet1--12'], ['ods--sheet1--11', 'ods--sheet1--12'],
['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'], ['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'],
]; ]);
$dataRowsSheet2 = [ $dataRowsSheet2 = $this->createRowsFromValues([
['ods--sheet2--11', 'ods--sheet2--12'], ['ods--sheet2--11', 'ods--sheet2--12'],
['ods--sheet2--21', 'ods--sheet2--22', 'ods--sheet2--23'], ['ods--sheet2--21', 'ods--sheet2--22', 'ods--sheet2--23'],
]; ]);
$dataRowsSheet1Again = [ $dataRowsSheet1Again = $this->createRowsFromValues([
['ods--sheet1--31', 'ods--sheet1--32'], ['ods--sheet1--31', 'ods--sheet1--32'],
['ods--sheet1--41', 'ods--sheet1--42', 'ods--sheet1--43'], ['ods--sheet1--41', 'ods--sheet1--42', 'ods--sheet1--43'],
]; ]);
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -369,11 +372,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldAutomaticallyCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOn() public function testAddRowShouldAutomaticallyCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOn()
{ {
$fileName = 'test_add_row_should_automatically_create_new_sheets_if_max_rows_reached_and_option_turned_on.ods'; $fileName = 'test_add_row_should_automatically_create_new_sheets_if_max_rows_reached_and_option_turned_on.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['ods--sheet1--11', 'ods--sheet1--12'], ['ods--sheet1--11', 'ods--sheet1--12'],
['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'], ['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'],
['ods--sheet2--11', 'ods--sheet2--12'], // this should be written in a new sheet ['ods--sheet2--11', 'ods--sheet2--12'], // this should be written in a new sheet
]; ]);
// set the maxRowsPerSheet limit to 2 // set the maxRowsPerSheet limit to 2
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\ODS\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2); \ReflectionHelper::setStaticValue('\Box\Spout\Writer\ODS\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
@ -393,11 +396,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldNotCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOff() public function testAddRowShouldNotCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOff()
{ {
$fileName = 'test_add_row_should_not_create_new_sheets_if_max_rows_reached_and_option_turned_off.ods'; $fileName = 'test_add_row_should_not_create_new_sheets_if_max_rows_reached_and_option_turned_off.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['ods--sheet1--11', 'ods--sheet1--12'], ['ods--sheet1--11', 'ods--sheet1--12'],
['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'], ['ods--sheet1--21', 'ods--sheet1--22', 'ods--sheet1--23'],
['ods--sheet1--31', 'ods--sheet1--32'], // this should NOT be written in a new sheet ['ods--sheet1--31', 'ods--sheet1--32'], // this should NOT be written in a new sheet
]; ]);
// set the maxRowsPerSheet limit to 2 // set the maxRowsPerSheet limit to 2
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\ODS\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2); \ReflectionHelper::setStaticValue('\Box\Spout\Writer\ODS\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
@ -416,9 +419,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldEscapeHtmlSpecialCharacters() public function testAddRowShouldEscapeHtmlSpecialCharacters()
{ {
$fileName = 'test_add_row_should_escape_html_special_characters.ods'; $fileName = 'test_add_row_should_escape_html_special_characters.ods';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['I\'m in "great" mood', 'This <must> be escaped & tested'], ['I\'m in "great" mood', 'This <must> be escaped & tested'],
]; ]);
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -434,7 +437,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$fileName = 'test_add_row_should_keep_new_lines.ods'; $fileName = 'test_add_row_should_keep_new_lines.ods';
$dataRow = ["I have\na dream"]; $dataRow = ["I have\na dream"];
$this->writeToODSFile([$dataRow], $fileName); $this->writeToODSFile($this->createRowsFromValues([$dataRow]), $fileName);
$this->assertValueWasWrittenToSheet($fileName, 1, 'I have'); $this->assertValueWasWrittenToSheet($fileName, 1, 'I have');
$this->assertValueWasWrittenToSheet($fileName, 1, 'a dream'); $this->assertValueWasWrittenToSheet($fileName, 1, 'a dream');
@ -458,55 +461,14 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$dataRow = ['foo']; $dataRow = ['foo'];
$this->writeToODSFile([$dataRow], $fileName); $this->writeToODSFile($this->createRowsFromValues([$dataRow]), $fileName);
$finfo = new \finfo(FILEINFO_MIME_TYPE); $finfo = new \finfo(FILEINFO_MIME_TYPE);
$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath)); $this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath));
} }
/** /**
* @return void * @param Row[] $allRows
*/
public function testWriteShouldAcceptCellObjects()
{
$fileName = 'test_writer_should_accept_cell_objects.ods';
$dataRows = [
[new Cell('ods--11'), new Cell('ods--12')],
[new Cell('ods--21'), new Cell('ods--22'), new Cell('ods--23')],
];
$this->writeToODSFile($dataRows, $fileName);
foreach ($dataRows as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertValueWasWritten($fileName, $cell->getValue());
}
}
}
/**
* @return void
*/
public function testWriteShouldAcceptCellObjectsWithDifferentValueTypes()
{
$fileName = 'test_writer_should_accept_cell_objects_with_types.ods';
$dataRows = [
[new Cell('i am a string'), new Cell(51465), new Cell(true), new Cell(51465.5)],
];
$this->writeToODSFile($dataRows, $fileName);
foreach ($dataRows as $dataRow) {
/** @var Cell $cell */
foreach ($dataRow as $cell) {
$this->assertValueWasWritten($fileName, (string) $cell->getValue(), '');
}
}
}
/**
* @param array $allRows
* @param string $fileName * @param string $fileName
* @param bool $shouldCreateSheetsAutomatically * @param bool $shouldCreateSheetsAutomatically
* @return Writer * @return Writer
@ -528,7 +490,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @param array $allRows * @param Row[] $allRows
* @param int $numSheets * @param int $numSheets
* @param string $fileName * @param string $fileName
* @param bool $shouldCreateSheetsAutomatically * @param bool $shouldCreateSheetsAutomatically

View File

@ -7,9 +7,11 @@ use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; 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\Border;
use Box\Spout\Writer\Common\Entity\Style\Color; use Box\Spout\Writer\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -18,6 +20,7 @@ use Box\Spout\Writer\WriterFactory;
class WriterWithStyleTest extends \PHPUnit_Framework_TestCase class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** @var Style */ /** @var Style */
private $defaultStyle; private $defaultStyle;
@ -36,7 +39,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->addRowWithStyle(['ods--11', 'ods--12'], $this->defaultStyle); $writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle));
} }
/** /**
@ -45,53 +48,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter() public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->addRowWithStyle(['ods--11', 'ods--12'], $this->defaultStyle); $writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--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.ods';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath);
$writer->addRowWithStyle(['ods--11', 'ods--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.ods';
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath);
$writer->addRowsWithStyle([['ods--11', 'ods--12']], $style);
} }
/** /**
@ -100,10 +57,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldListAllUsedStylesInCreatedContentXmlFile() public function testAddRowWithStyleShouldListAllUsedStylesInCreatedContentXmlFile()
{ {
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.ods'; $fileName = 'test_add_row_with_style_should_list_all_used_fonts.ods';
$dataRows = [
['ods--11', 'ods--12'],
['ods--21', 'ods--22'],
];
$style = (new StyleBuilder()) $style = (new StyleBuilder())
->setFontBold() ->setFontBold()
@ -118,7 +71,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
->setBackgroundColor(Color::GREEN) ->setBackgroundColor(Color::GREEN)
->build(); ->build();
$this->writeToODSFileWithMultipleStyles($dataRows, $fileName, [$style, $style2]); $dataRows = [
$this->createRowFromValues(['ods--11', 'ods--12'], $style),
$this->createRowFromValues(['ods--21', 'ods--22'], $style2),
];
$this->writeToODSFile($dataRows, $fileName);
$cellStyleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $cellStyleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertEquals(3, count($cellStyleElements), 'There should be 3 separate cell styles, including the default one.'); $this->assertEquals(3, count($cellStyleElements), 'There should be 3 separate cell styles, including the default one.');
@ -144,9 +102,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldWriteDefaultStyleSettings() public function testAddRowWithStyleShouldWriteDefaultStyleSettings()
{ {
$fileName = 'test_add_row_with_style_should_write_default_style_settings.ods'; $fileName = 'test_add_row_with_style_should_write_default_style_settings.ods';
$dataRow = ['ods--11', 'ods--12']; $dataRow = $this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle);
$this->writeToODSFile([$dataRow], $fileName, $this->defaultStyle); $this->writeToODSFile([$dataRow], $fileName);
$textPropertiesElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'style:text-properties'); $textPropertiesElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'style:text-properties');
$this->assertEquals(Style::DEFAULT_FONT_SIZE . 'pt', $textPropertiesElement->getAttribute('fo:font-size')); $this->assertEquals(Style::DEFAULT_FONT_SIZE . 'pt', $textPropertiesElement->getAttribute('fo:font-size'));
@ -160,15 +118,16 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldApplyStyleToCells() public function testAddRowWithStyleShouldApplyStyleToCells()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_cells.ods'; $fileName = 'test_add_row_with_style_should_apply_style_to_cells.ods';
$dataRows = [
['ods--11'],
['ods--21'],
['ods--31'],
];
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$style2 = (new StyleBuilder())->setFontSize(15)->build(); $style2 = (new StyleBuilder())->setFontSize(15)->build();
$dataRows = [
$this->createStyledRowFromValues(['ods--11'], $style),
$this->createStyledRowFromValues(['ods--21'], $style2),
$this->createRowFromValues(['ods--31']),
];
$this->writeToODSFileWithMultipleStyles($dataRows, $fileName, [$style, $style2, null]); $this->writeToODSFile($dataRows, $fileName);
$cellDomElements = $this->getCellElementsFromContentXmlFile($fileName); $cellDomElements = $this->getCellElementsFromContentXmlFile($fileName);
$this->assertEquals(3, count($cellDomElements), 'There should be 3 cells with content'); $this->assertEquals(3, count($cellDomElements), 'There should be 3 cells with content');
@ -184,13 +143,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldReuseDuplicateStyles() public function testAddRowWithStyleShouldReuseDuplicateStyles()
{ {
$fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.ods'; $fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.ods';
$dataRows = [
$style = (new StyleBuilder())->setFontBold()->build();
$dataRows = $this->createStyledRowsFromValues([
['ods--11'], ['ods--11'],
['ods--21'], ['ods--21'],
]; ], $style);
$style = (new StyleBuilder())->setFontBold()->build();
$this->writeToODSFile($dataRows, $fileName, $style); $this->writeToODSFile($dataRows, $fileName);
$cellDomElements = $this->getCellElementsFromContentXmlFile($fileName); $cellDomElements = $this->getCellElementsFromContentXmlFile($fileName);
$this->assertEquals(2, count($cellDomElements), 'There should be 2 cells with content'); $this->assertEquals(2, count($cellDomElements), 'There should be 2 cells with content');
@ -205,12 +165,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified() public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
{ {
$fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.ods'; $fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.ods';
$dataRows = [
['ods--11', 'ods--12'],
];
$style = (new StyleBuilder())->setShouldWrapText()->build();
$this->writeToODSFile($dataRows, $fileName, $style); $style = (new StyleBuilder())->setShouldWrapText()->build();
$dataRows = $this->createStyledRowsFromValues([
['ods--11', 'ods--12'],
], $style);
$this->writeToODSFile($dataRows, $fileName);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)'); $this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)');
@ -225,11 +186,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine() public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine()
{ {
$fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.ods'; $fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.ods';
$dataRows = [ $dataRows = $this->createStyledRowsFromValues([
["ods--11\nods--11"], ["ods--11\nods--11"],
]; ], $this->defaultStyle);
$this->writeToODSFile($dataRows, $fileName, $this->defaultStyle); $this->writeToODSFile($dataRows, $fileName);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)'); $this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)');
@ -244,12 +205,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddBackgroundColor() public function testAddBackgroundColor()
{ {
$fileName = 'test_default_background_style.ods'; $fileName = 'test_default_background_style.ods';
$dataRows = [
['defaultBgColor'],
];
$style = (new StyleBuilder())->setBackgroundColor(Color::WHITE)->build(); $style = (new StyleBuilder())->setBackgroundColor(Color::WHITE)->build();
$this->writeToODSFile($dataRows, $fileName, $style); $dataRows = $this->createStyledRowsFromValues([
['defaultBgColor'],
], $style);
$this->writeToODSFile($dataRows, $fileName);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)'); $this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)');
@ -265,12 +227,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_borders.ods'; $fileName = 'test_borders.ods';
$dataRows = [
['row-with-border-bottom-green-thick-solid'],
['row-without-border'],
['row-with-border-top-red-thin-dashed'],
];
$borderBottomGreenThickSolid = (new BorderBuilder()) $borderBottomGreenThickSolid = (new BorderBuilder())
->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build(); ->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build();
@ -283,7 +239,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
(new StyleBuilder())->setBorder($borderTopRedThinDashed)->build(), (new StyleBuilder())->setBorder($borderTopRedThinDashed)->build(),
]; ];
$this->writeToODSFileWithMultipleStyles($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->writeToODSFile($dataRows, $fileName);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
@ -328,7 +290,10 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testSetDefaultRowStyle() public function testSetDefaultRowStyle()
{ {
$fileName = 'test_set_default_row_style.ods'; $fileName = 'test_set_default_row_style.ods';
$dataRows = [['ods--11']];
$dataRows = $this->createRowsFromValues([
['ods--11'],
]);
$defaultFontSize = 50; $defaultFontSize = 50;
$defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build(); $defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build();
@ -340,12 +305,11 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @param array $allRows * @param Row[] $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return Writer * @return Writer
*/ */
private function writeToODSFile($allRows, $fileName, $style) private function writeToODSFile($allRows, $fileName)
{ {
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -354,16 +318,16 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::ODS); $writer = WriterFactory::create(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRowsWithStyle($allRows, $style); $writer->addRows($allRows);
$writer->close(); $writer->close();
return $writer; return $writer;
} }
/** /**
* @param array $allRows * @param Row[] $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle * @param Style $defaultStyle
* @return Writer * @return Writer
*/ */
private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle) private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle)
@ -382,36 +346,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
return $writer; return $writer;
} }
/**
* @param array $allRows
* @param string $fileName
* @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles
* @return Writer
*/
private function writeToODSFileWithMultipleStyles($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\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS);
$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 $fileName
* @return \DOMNode[] * @return \DOMNode[]

View File

@ -0,0 +1,65 @@
<?php
namespace Box\Spout\Writer;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style;
/**
* Trait RowCreationHelper
*/
trait RowCreationHelper
{
/**
* @param array $cellValues
* @param Style|null $rowStyle
* @return Row
*/
protected function createRowFromValues(array $cellValues, Style $rowStyle = null)
{
$row = EntityFactory::createRow([], $rowStyle);
foreach ($cellValues as $cellValue) {
$row->addCell(EntityFactory::createCell($cellValue));
}
return $row;
}
/**
* @param array $cellValues
* @param Style $rowStyle
* @return Row
*/
protected function createStyledRowFromValues(array $cellValues, Style $rowStyle)
{
return $this->createRowFromValues($cellValues, $rowStyle);
}
/**
* @param array $rowValues
* @param Style|null $rowsStyle
* @return Row[]
*/
protected function createRowsFromValues(array $rowValues, Style $rowsStyle = null)
{
$rows = [];
foreach ($rowValues as $cellValues) {
$rows[] = $this->createRowFromValues($cellValues, $rowsStyle);
}
return $rows;
}
/**
* @param array $rowValues
* @param Style $rowsStyle
* @return Row[]
*/
protected function createStyledRowsFromValues(array $rowValues, Style $rowsStyle)
{
return $this->createRowsFromValues($rowValues, $rowsStyle);
}
}

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -13,6 +14,7 @@ use Box\Spout\Writer\WriterFactory;
class SheetTest extends \PHPUnit_Framework_TestCase class SheetTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** /**
* @return void * @return void
@ -91,7 +93,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$sheet = $writer->getCurrentSheet(); $sheet = $writer->getCurrentSheet();
$sheet->setName($sheetName); $sheet->setName($sheetName);
$writer->addRow(['xlsx--11', 'xlsx--12']); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
$writer->close(); $writer->close();
return $sheet; return $sheet;
@ -110,9 +112,9 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRow(['xlsx--sheet1--11', 'xlsx--sheet1--12']); $writer->addRow($this->createRowFromValues(['xlsx--sheet1--11', 'xlsx--sheet1--12']));
$writer->addNewSheetAndMakeItCurrent(); $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(); $writer->close();

View File

@ -5,7 +5,8 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; 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\WriterFactory;
use Box\Spout\Writer\XLSX\Manager\WorksheetManager; use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
@ -15,6 +16,7 @@ use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
class WriterTest extends \PHPUnit_Framework_TestCase class WriterTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** /**
* @expectedException \Box\Spout\Common\Exception\IOException * @expectedException \Box\Spout\Common\Exception\IOException
@ -35,7 +37,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::XLSX); $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() public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::XLSX); $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() public function testAddRowShouldThrowExceptionIfWritingStringExceedingMaxNumberOfCharactersAllowedPerCell()
{ {
$fileName = 'test_add_row_should_throw_exception_if_string_exceeds_max_num_chars_allowed_per_cell.xlsx'; $fileName = 'test_add_row_should_throw_exception_if_string_exceeds_max_num_chars_allowed_per_cell.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
[new \stdClass()], [new \stdClass()],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
} }
@ -124,10 +126,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown() public function testAddRowShouldCleanupAllFilesIfExceptionIsThrown()
{ {
$fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx'; $fileName = 'test_add_row_should_cleanup_all_files_if_exception_thrown.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['wrong'], ['wrong'],
[new \stdClass()], [new \stdClass()],
]; ]);
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -160,6 +162,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
@ -179,6 +182,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -216,10 +220,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToSheetUsingInlineStrings() public function testAddRowShouldWriteGivenDataToSheetUsingInlineStrings()
{ {
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_inline_strings.xlsx'; $fileName = 'test_add_row_should_write_given_data_to_sheet_using_inline_strings.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['xlsx--11', 'xlsx--12'], ['xlsx--11', 'xlsx--12'],
['xlsx--21', 'xlsx--22', 'xlsx--23'], ['xlsx--21', 'xlsx--22', 'xlsx--23'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true); $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = true);
@ -236,10 +240,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingInlineStrings() public function testAddRowShouldWriteGivenDataToTwoSheetsUsingInlineStrings()
{ {
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_inline_strings.xlsx'; $fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_inline_strings.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['xlsx--11', 'xlsx--12'], ['xlsx--11', 'xlsx--12'],
['xlsx--21', 'xlsx--22', 'xlsx--23'], ['xlsx--21', 'xlsx--22', 'xlsx--23'],
]; ]);
$numSheets = 2; $numSheets = 2;
$this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = true); $this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = true);
@ -259,10 +263,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToSheetUsingSharedStrings() public function testAddRowShouldWriteGivenDataToSheetUsingSharedStrings()
{ {
$fileName = 'test_add_row_should_write_given_data_to_sheet_using_shared_strings.xlsx'; $fileName = 'test_add_row_should_write_given_data_to_sheet_using_shared_strings.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['xlsx--11', 'xlsx--12'], ['xlsx--11', 'xlsx--12'],
['xlsx--21', 'xlsx--22', 'xlsx--23'], ['xlsx--21', 'xlsx--22', 'xlsx--23'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false); $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
@ -279,10 +283,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingSharedStrings() public function testAddRowShouldWriteGivenDataToTwoSheetsUsingSharedStrings()
{ {
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_shared_strings.xlsx'; $fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_shared_strings.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['xlsx--11', 'xlsx--12'], ['xlsx--11', 'xlsx--12'],
['xlsx--21', 'xlsx--22', 'xlsx--23'], ['xlsx--21', 'xlsx--22', 'xlsx--23'],
]; ]);
$numSheets = 2; $numSheets = 2;
$this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = false); $this->writeToMultipleSheetsInXLSXFile($dataRows, $numSheets, $fileName, $shouldUseInlineStrings = false);
@ -302,9 +306,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldSupportAssociativeArrays() public function testAddRowShouldSupportAssociativeArrays()
{ {
$fileName = 'test_add_row_should_support_associative_arrays.xlsx'; $fileName = 'test_add_row_should_support_associative_arrays.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['foo' => 'xlsx--11', 'bar' => 'xlsx--12'], ['foo' => 'xlsx--11', 'bar' => 'xlsx--12'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
@ -321,13 +325,13 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldNotWriteEmptyRows() public function testAddRowShouldNotWriteEmptyRows()
{ {
$fileName = 'test_add_row_should_not_write_empty_rows.xlsx'; $fileName = 'test_add_row_should_not_write_empty_rows.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
[''], [''],
['xlsx--21', 'xlsx--22'], ['xlsx--21', 'xlsx--22'],
['key' => ''], ['key' => ''],
[''], [''],
['xlsx--51', 'xlsx--52'], ['xlsx--51', 'xlsx--52'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
@ -344,9 +348,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldSupportMultipleTypesOfData() public function testAddRowShouldSupportMultipleTypesOfData()
{ {
$fileName = 'test_add_row_should_support_multiple_types_of_data.xlsx'; $fileName = 'test_add_row_should_support_multiple_types_of_data.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['xlsx--11', true, '', 0, 10.2, null], ['xlsx--11', true, '', 0, 10.2, null],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false); $this->writeToXLSXFile($dataRows, $fileName, $shouldUseInlineStrings = false);
@ -362,18 +366,18 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldWriteGivenDataToTheCorrectSheet() public function testAddRowShouldWriteGivenDataToTheCorrectSheet()
{ {
$fileName = 'test_add_row_should_write_given_data_to_the_correct_sheet.xlsx'; $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--11', 'xlsx--sheet1--12'],
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'], ['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
]; ]);
$dataRowsSheet2 = [ $dataRowsSheet2 = $this->createRowsFromValues([
['xlsx--sheet2--11', 'xlsx--sheet2--12'], ['xlsx--sheet2--11', 'xlsx--sheet2--12'],
['xlsx--sheet2--21', 'xlsx--sheet2--22', 'xlsx--sheet2--23'], ['xlsx--sheet2--21', 'xlsx--sheet2--22', 'xlsx--sheet2--23'],
]; ]);
$dataRowsSheet1Again = [ $dataRowsSheet1Again = $this->createRowsFromValues([
['xlsx--sheet1--31', 'xlsx--sheet1--32'], ['xlsx--sheet1--31', 'xlsx--sheet1--32'],
['xlsx--sheet1--41', 'xlsx--sheet1--42', 'xlsx--sheet1--43'], ['xlsx--sheet1--41', 'xlsx--sheet1--42', 'xlsx--sheet1--43'],
]; ]);
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -419,11 +423,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldAutomaticallyCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOn() public function testAddRowShouldAutomaticallyCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOn()
{ {
$fileName = 'test_add_row_should_automatically_create_new_sheets_if_max_rows_reached_and_option_turned_on.xlsx'; $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--11', 'xlsx--sheet1--12'],
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'], ['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
['xlsx--sheet2--11', 'xlsx--sheet2--12'], // this should be written in a new sheet ['xlsx--sheet2--11', 'xlsx--sheet2--12'], // this should be written in a new sheet
]; ]);
// set the maxRowsPerSheet limit to 2 // set the maxRowsPerSheet limit to 2
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2); \ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
@ -443,11 +447,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldNotCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOff() public function testAddRowShouldNotCreateNewSheetsIfMaxRowsReachedAndOptionTurnedOff()
{ {
$fileName = 'test_add_row_should_not_create_new_sheets_if_max_rows_reached_and_option_turned_off.xlsx'; $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--11', 'xlsx--sheet1--12'],
['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'], ['xlsx--sheet1--21', 'xlsx--sheet1--22', 'xlsx--sheet1--23'],
['xlsx--sheet1--31', 'xlsx--sheet1--32'], // this should NOT be written in a new sheet ['xlsx--sheet1--31', 'xlsx--sheet1--32'], // this should NOT be written in a new sheet
]; ]);
// set the maxRowsPerSheet limit to 2 // set the maxRowsPerSheet limit to 2
\ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2); \ReflectionHelper::setStaticValue('\Box\Spout\Writer\XLSX\Manager\WorkbookManager', 'maxRowsPerWorksheet', 2);
@ -466,9 +470,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldEscapeHtmlSpecialCharacters() public function testAddRowShouldEscapeHtmlSpecialCharacters()
{ {
$fileName = 'test_add_row_should_escape_html_special_characters.xlsx'; $fileName = 'test_add_row_should_escape_html_special_characters.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['I\'m in "great" mood', 'This <must> be escaped & tested'], ['I\'m in "great" mood', 'This <must> be escaped & tested'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
@ -482,9 +486,9 @@ class WriterTest extends \PHPUnit_Framework_TestCase
public function testAddRowShouldEscapeControlCharacters() public function testAddRowShouldEscapeControlCharacters()
{ {
$fileName = 'test_add_row_should_escape_control_characters.xlsx'; $fileName = 'test_add_row_should_escape_control_characters.xlsx';
$dataRows = [ $dataRows = $this->createRowsFromValues([
['control ' . chr(21) . ' character'], ['control ' . chr(21) . ' character'],
]; ]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
@ -498,7 +502,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_mime_type.xlsx'; $fileName = 'test_mime_type.xlsx';
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$dataRows = [['foo']]; $dataRows = $this->createRowsFromValues([['foo']]);
$this->writeToXLSXFile($dataRows, $fileName); $this->writeToXLSXFile($dataRows, $fileName);
@ -507,61 +511,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @return void * @param Row[] $allRows
*/
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 string $fileName * @param string $fileName
* @param bool $shouldUseInlineStrings * @param bool $shouldUseInlineStrings
* @param bool $shouldCreateSheetsAutomatically * @param bool $shouldCreateSheetsAutomatically
@ -585,7 +535,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @param array $allRows * @param Row[] $allRows
* @param int $numSheets * @param int $numSheets
* @param string $fileName * @param string $fileName
* @param bool $shouldUseInlineStrings * @param bool $shouldUseInlineStrings

View File

@ -7,10 +7,12 @@ use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; 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\Border;
use Box\Spout\Writer\Common\Entity\Style\Color; use Box\Spout\Writer\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Manager\OptionsManager; use Box\Spout\Writer\XLSX\Manager\OptionsManager;
@ -20,6 +22,7 @@ use Box\Spout\Writer\XLSX\Manager\OptionsManager;
class WriterWithStyleTest extends \PHPUnit_Framework_TestCase class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
use RowCreationHelper;
/** @var \Box\Spout\Writer\Common\Entity\Style\Style */ /** @var \Box\Spout\Writer\Common\Entity\Style\Style */
private $defaultStyle; private $defaultStyle;
@ -38,7 +41,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::XLSX); $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() public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$writer = WriterFactory::create(Type::XLSX); $writer = WriterFactory::create(Type::XLSX);
$writer->addRowWithStyle(['xlsx--11', 'xlsx--12'], $this->defaultStyle); $writer->addRow($this->createStyledRowFromValues(['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);
} }
/** /**
@ -102,10 +59,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile() public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile()
{ {
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx'; $fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx';
$dataRows = [
['xlsx--11', 'xlsx--12'],
['xlsx--21', 'xlsx--22'],
];
$style = (new StyleBuilder()) $style = (new StyleBuilder())
->setFontBold() ->setFontBold()
@ -119,13 +72,19 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
->setFontName('Cambria') ->setFontName('Cambria')
->build(); ->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'); $fontsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fonts');
$this->assertEquals(3, $fontsDomElement->getAttribute('count'), 'There should be 3 fonts, including the default one.'); $this->assertEquals(3, $fontsDomElement->getAttribute('count'), 'There should be 3 fonts, including the default one.');
$fontElements = $fontsDomElement->getElementsByTagName('font'); $fontElements = $fontsDomElement->getElementsByTagName('font');
$this->assertEquals(3, $fontElements->length, 'There should be 3 associated "font" elements, including the default one.'); $this->assertEquals(3, $fontElements->length, 'There should be 3 associated "font" elements, including the default one.');
// First font should be the default one // First font should be the default one
$defaultFontElement = $fontElements->item(0); $defaultFontElement = $fontElements->item(0);
$this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.'); $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() public function testAddRowWithStyleShouldApplyStyleToCells()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_cells.xlsx'; $fileName = 'test_add_row_with_style_should_apply_style_to_cells.xlsx';
$dataRows = [
['xlsx--11'],
['xlsx--21'],
['xlsx--31'],
];
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$style2 = (new StyleBuilder())->setFontSize(15)->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); $cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
$this->assertEquals(3, count($cellDomElements), 'There should be 3 cells.'); $this->assertEquals(3, count($cellDomElements), 'There should be 3 cells.');
@ -182,12 +143,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldApplyStyleToEmptyCellsIfNeeded() public function testAddRowWithStyleShouldApplyStyleToEmptyCellsIfNeeded()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_empty_cells_if_needed.xlsx'; $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(); $styleWithFont = (new StyleBuilder())->setFontBold()->build();
$styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->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(); $border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build();
$styleWithBorder = (new StyleBuilder())->setBorder($border)->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); $cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
@ -229,13 +191,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldReuseDuplicateStyles() public function testAddRowWithStyleShouldReuseDuplicateStyles()
{ {
$fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.xlsx'; $fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.xlsx';
$dataRows = [
$style = (new StyleBuilder())->setFontBold()->build();
$dataRows = $this->createStyledRowsFromValues([
['xlsx--11'], ['xlsx--11'],
['xlsx--21'], ['xlsx--21'],
]; ], $style);
$style = (new StyleBuilder())->setFontBold()->build();
$this->writeToXLSXFile($dataRows, $fileName, $style); $this->writeToXLSXFile($dataRows, $fileName);
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName); $cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
$this->assertEquals('1', $cellDomElements[0]->getAttribute('s')); $this->assertEquals('1', $cellDomElements[0]->getAttribute('s'));
@ -248,12 +211,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified() public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
{ {
$fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.xlsx'; $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'); $cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1); $xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
@ -267,12 +231,13 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine() public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine()
{ {
$fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.xlsx'; $fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.xlsx';
$dataRows = [
$dataRows = $this->createStyledRowsFromValues([
["xlsx--11\nxlsx--11"], ["xlsx--11\nxlsx--11"],
['xlsx--21'], ['xlsx--21'],
]; ], $this->defaultStyle);
$this->writeToXLSXFile($dataRows, $fileName, $this->defaultStyle); $this->writeToXLSXFile($dataRows, $fileName);
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs'); $cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1); $xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
@ -286,11 +251,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testAddBackgroundColor() public function testAddBackgroundColor()
{ {
$fileName = 'test_add_background_color.xlsx'; $fileName = 'test_add_background_color.xlsx';
$dataRows = [
['BgColor'],
];
$style = (new StyleBuilder())->setBackgroundColor(Color::WHITE)->build(); $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'); $fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills');
$this->assertEquals(3, $fillsDomElement->getAttribute('count'), 'There should be 3 fills, including the 2 default ones'); $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() public function testReuseBackgroundColorSharedDefinition()
{ {
$fileName = 'test_add_background_color_shared_definition.xlsx'; $fileName = 'test_add_background_color_shared_definition.xlsx';
$style = (new StyleBuilder())->setBackgroundColor(Color::RED)->setFontBold()->build();
$style2 = (new StyleBuilder())->setBackgroundColor(Color::RED)->build();
$dataRows = [ $dataRows = [
['row-bold-background-red'], $this->createStyledRowFromValues(['row-bold-background-red'], $style),
['row-background-red'], $this->createStyledRowFromValues(['row-background-red'], $style2),
]; ];
$styles = [ $this->writeToXLSXFile($dataRows, $fileName);
(new StyleBuilder())->setBackgroundColor(Color::RED)->setFontBold()->build(),
(new StyleBuilder())->setBackgroundColor(Color::RED)->build(),
];
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
$fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills'); $fillsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'fills');
$this->assertEquals( $this->assertEquals(
@ -354,12 +321,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_borders.xlsx'; $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()) $borderBottomGreenThickSolid = (new BorderBuilder())
->setBorderBottom(Color::GREEN, Border::WIDTH_THICK, Border::STYLE_SOLID)->build(); ->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(), (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'); $borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
$this->assertEquals(3, $borderElements->getAttribute('count'), '3 borders present'); $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 // Border should be Left, Right, Top, Bottom
$fileName = 'test_borders_correct_order.xlsx'; $fileName = 'test_borders_correct_order.xlsx';
$dataRows = [
['I am a teapot'],
];
$borders = (new BorderBuilder()) $borders = (new BorderBuilder())
->setBorderRight() ->setBorderRight()
->setBorderTop() ->setBorderTop()
@ -400,7 +364,12 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
->build(); ->build();
$style = (new StyleBuilder())->setBorder($borders)->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'); $borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
$correctOrdering = [ $correctOrdering = [
@ -429,7 +398,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
public function testSetDefaultRowStyle() public function testSetDefaultRowStyle()
{ {
$fileName = 'test_set_default_row_style.xlsx'; $fileName = 'test_set_default_row_style.xlsx';
$dataRows = [['xlsx--11']]; $dataRows = $this->createRowsFromValues([['xlsx--11']]);
$defaultFontSize = 50; $defaultFontSize = 50;
$defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build(); $defaultStyle = (new StyleBuilder())->setFontSize($defaultFontSize)->build();
@ -463,22 +432,15 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$borderRightFontBoldStyle = (new StyleMerger())->merge($borderRightStyle, $fontStyle); $borderRightFontBoldStyle = (new StyleMerger())->merge($borderRightStyle, $fontStyle);
$dataRows = [ $dataRows = [
['Border-Left'], $this->createStyledRowFromValues(['Border-Left'], $borderLeftStyle),
['Empty'], $this->createStyledRowFromValues(['Empty'], $emptyStyle),
['Font-Bold'], $this->createStyledRowFromValues(['Font-Bold'], $fontStyle),
['Border-Right'], $this->createStyledRowFromValues(['Border-Right'], $borderRightStyle),
['Border-Right-Font-Bold'], $this->createStyledRowFromValues(['Border-Right-Font-Bold'], $borderRightFontBoldStyle),
]; ];
$styles = [ $this->writeToXLSXFile($dataRows, $fileName);
$borderLeftStyle,
$emptyStyle,
$fontStyle,
$borderRightStyle,
$borderRightFontBoldStyle,
];
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles);
$borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders'); $borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders');
$this->assertEquals(3, $borderElements->getAttribute('count'), '3 borders in count attribute'); $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 string $fileName
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return Writer * @return Writer
*/ */
private function writeToXLSXFile($allRows, $fileName, $style) private function writeToXLSXFile($allRows, $fileName)
{ {
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -529,14 +490,14 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$writer->setShouldUseInlineStrings(true); $writer->setShouldUseInlineStrings(true);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRowsWithStyle($allRows, $style); $writer->addRows($allRows);
$writer->close(); $writer->close();
return $writer; return $writer;
} }
/** /**
* @param array $allRows * @param Row[] $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle * @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle
* @return Writer * @return Writer
@ -558,37 +519,6 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
return $writer; 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 $fileName
* @param string $section * @param string $section

View File

@ -5,6 +5,7 @@ require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/tests/Spout/TestUsingResource.php'; require_once dirname(__DIR__) . '/tests/Spout/TestUsingResource.php';
require_once dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php'; require_once dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php';
require_once dirname(__DIR__) . '/tests/Spout/Reader/CSV/SpoutTestStream.php'; require_once dirname(__DIR__) . '/tests/Spout/Reader/CSV/SpoutTestStream.php';
require_once dirname(__DIR__) . '/tests/Spout/Writer/RowCreationHelper.php';
// Make sure a timezone is set to be able to work with dates // Make sure a timezone is set to be able to work with dates
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');