Add tests for cell styling

This commit is contained in:
Adrien Loison 2017-11-10 21:15:39 +01:00
parent 8dd6487ea3
commit 21f609126b
2 changed files with 82 additions and 29 deletions

View File

@ -37,7 +37,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
@ -48,7 +48,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
@ -59,9 +59,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldListAllUsedStylesInCreatedContentXmlFile() public function testAddRowShouldListAllUsedStylesInCreatedContentXmlFile()
{ {
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.ods'; $fileName = 'test_add_row_should_list_all_used_fonts.ods';
$style = (new StyleBuilder()) $style = (new StyleBuilder())
->setFontBold() ->setFontBold()
@ -104,9 +104,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldWriteDefaultStyleSettings() public function testAddRowShouldWriteDefaultStyleSettings()
{ {
$fileName = 'test_add_row_with_style_should_write_default_style_settings.ods'; $fileName = 'test_add_row_should_write_default_style_settings.ods';
$dataRow = $this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle); $dataRow = $this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle);
$this->writeToODSFile([$dataRow], $fileName); $this->writeToODSFile([$dataRow], $fileName);
@ -120,9 +120,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldApplyStyleToCells() public function testAddRowShouldApplyStyleToCells()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_cells.ods'; $fileName = 'test_add_row_should_apply_style_to_cells.ods';
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$style2 = (new StyleBuilder())->setFontSize(15)->build(); $style2 = (new StyleBuilder())->setFontSize(15)->build();
@ -145,9 +145,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldReuseDuplicateStyles() public function testAddRowShouldReuseDuplicateStyles()
{ {
$fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.ods'; $fileName = 'test_add_row_should_reuse_duplicate_styles.ods';
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
@ -167,9 +167,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified() public function testAddRowShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
{ {
$fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.ods'; $fileName = 'test_add_row_should_add_wrap_text_alignment.ods';
$style = (new StyleBuilder())->setShouldWrapText()->build(); $style = (new StyleBuilder())->setShouldWrapText()->build();
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
@ -188,9 +188,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine() public function testAddRowShouldApplyWrapTextIfCellContainsNewLine()
{ {
$fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.ods'; $fileName = 'test_add_row_should_apply_wrap_text_if_new_lines.ods';
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
["ods--11\nods--11"], ["ods--11\nods--11"],
], $this->defaultStyle); ], $this->defaultStyle);
@ -204,6 +204,32 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertFirstChildHasAttributeEquals('wrap', $customStyleElement, 'table-cell-properties', 'fo:wrap-option'); $this->assertFirstChildHasAttributeEquals('wrap', $customStyleElement, 'table-cell-properties', 'fo:wrap-option');
} }
/**
* @return void
*/
public function testAddRowShouldSupportCellStyling()
{
$fileName = 'test_add_row_should_support_cell_styling.ods';
$boldStyle = (new StyleBuilder())->setFontBold()->build();
$underlineStyle = (new StyleBuilder())->setFontUnderline()->build();
$dataRow = EntityFactory::createRow([
EntityFactory::createCell('ods--11', $boldStyle),
EntityFactory::createCell('ods--12', $underlineStyle),
EntityFactory::createCell('ods--13', $underlineStyle),
]);
$this->writeToODSFile([$dataRow], $fileName);
$cellDomElements = $this->getCellElementsFromContentXmlFile($fileName);
// First row should have 3 styled cells, with cell 2 and 3 sharing the same style
$this->assertEquals('ce2', $cellDomElements[0]->getAttribute('table:style-name'));
$this->assertEquals('ce3', $cellDomElements[1]->getAttribute('table:style-name'));
$this->assertEquals('ce3', $cellDomElements[2]->getAttribute('table:style-name'));
}
/** /**
* @return void * @return void
*/ */

View File

@ -8,6 +8,7 @@ use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
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\Cell;
use Box\Spout\Writer\Common\Entity\Row; 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;
@ -39,7 +40,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCallAddRowBeforeOpeningWriter()
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
@ -50,7 +51,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldThrowExceptionIfCalledBeforeOpeningWriter() public function testAddRowShouldThrowExceptionIfCalledBeforeOpeningWriter()
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
@ -61,9 +62,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile() public function testAddRowShouldListAllUsedFontsInCreatedStylesXmlFile()
{ {
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx'; $fileName = 'test_add_row_should_list_all_used_fonts.xlsx';
$style = (new StyleBuilder()) $style = (new StyleBuilder())
->setFontBold() ->setFontBold()
@ -119,9 +120,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldApplyStyleToCells() public function testAddRowShouldApplyStyleToCells()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_cells.xlsx'; $fileName = 'test_add_row_should_apply_style_to_cells.xlsx';
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$style2 = (new StyleBuilder())->setFontSize(15)->build(); $style2 = (new StyleBuilder())->setFontSize(15)->build();
@ -145,9 +146,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldApplyStyleToEmptyCellsIfNeeded() public function testAddRowShouldApplyStyleToEmptyCellsIfNeeded()
{ {
$fileName = 'test_add_row_with_style_should_apply_style_to_empty_cells_if_needed.xlsx'; $fileName = 'test_add_row_should_apply_style_to_empty_cells_if_needed.xlsx';
$styleWithFont = (new StyleBuilder())->setFontBold()->build(); $styleWithFont = (new StyleBuilder())->setFontBold()->build();
$styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->build(); $styleWithBackground = (new StyleBuilder())->setBackgroundColor(Color::BLUE)->build();
@ -193,9 +194,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldReuseDuplicateStyles() public function testAddRowShouldReuseDuplicateStyles()
{ {
$fileName = 'test_add_row_with_style_should_reuse_duplicate_styles.xlsx'; $fileName = 'test_add_row_should_reuse_duplicate_styles.xlsx';
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
@ -213,9 +214,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified() public function testAddRowShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
{ {
$fileName = 'test_add_row_with_style_should_add_wrap_text_alignment.xlsx'; $fileName = 'test_add_row_should_add_wrap_text_alignment.xlsx';
$style = (new StyleBuilder())->setShouldWrapText()->build(); $style = (new StyleBuilder())->setShouldWrapText()->build();
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
@ -233,9 +234,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testAddRowWithStyleShouldApplyWrapTextIfCellContainsNewLine() public function testAddRowShouldApplyWrapTextIfCellContainsNewLine()
{ {
$fileName = 'test_add_row_with_style_should_apply_wrap_text_if_new_lines.xlsx'; $fileName = 'test_add_row_should_apply_wrap_text_if_new_lines.xlsx';
$dataRows = $this->createStyledRowsFromValues([ $dataRows = $this->createStyledRowsFromValues([
["xlsx--11\nxlsx--11"], ["xlsx--11\nxlsx--11"],
@ -250,6 +251,32 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText'); $this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText');
} }
/**
* @return void
*/
public function testAddRowShouldSupportCellStyling()
{
$fileName = 'test_add_row_should_support_cell_styling.xlsx';
$boldStyle = (new StyleBuilder())->setFontBold()->build();
$underlineStyle = (new StyleBuilder())->setFontUnderline()->build();
$dataRow = EntityFactory::createRow([
EntityFactory::createCell('xlsx--11', $boldStyle),
EntityFactory::createCell('xlsx--12', $underlineStyle),
EntityFactory::createCell('xlsx--13', $underlineStyle),
]);
$this->writeToXLSXFile([$dataRow], $fileName);
$cellDomElements = $this->getCellElementsFromSheetXmlFile($fileName);
// First row should have 3 styled cells, with cell 2 and 3 sharing the same style
$this->assertEquals('1', $cellDomElements[0]->getAttribute('s'));
$this->assertEquals('2', $cellDomElements[1]->getAttribute('s'));
$this->assertEquals('2', $cellDomElements[2]->getAttribute('s'));
}
/** /**
* @return void * @return void
*/ */
@ -421,7 +448,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @return void * @return void
*/ */
public function testReUseBorders() public function testReuseBorders()
{ {
$fileName = 'test_reuse_borders.xlsx'; $fileName = 'test_reuse_borders.xlsx';