From 38a39edc9e2a467443dce951cc130e336ad8eb76 Mon Sep 17 00:00:00 2001 From: madflow Date: Sun, 29 May 2016 14:01:36 +0200 Subject: [PATCH] Tests #135 Added first XLSX tests ODS Tests --- .../Spout/Writer/ODS/WriterWithStyleTest.php | 61 +++++++++- tests/Spout/Writer/Style/BorderTest.php | 112 ++++++++++++++++++ tests/Spout/Writer/Style/StyleTest.php | 28 +++++ .../Spout/Writer/XLSX/WriterWithStyleTest.php | 38 +++++- 4 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 tests/Spout/Writer/Style/BorderTest.php diff --git a/tests/Spout/Writer/ODS/WriterWithStyleTest.php b/tests/Spout/Writer/ODS/WriterWithStyleTest.php index 9888bfc..91f16f0 100644 --- a/tests/Spout/Writer/ODS/WriterWithStyleTest.php +++ b/tests/Spout/Writer/ODS/WriterWithStyleTest.php @@ -5,6 +5,9 @@ namespace Box\Spout\Writer\ODS; use Box\Spout\Common\Type; use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\TestUsingResource; +use Box\Spout\Writer\ODS\Helper\BorderHelper; +use Box\Spout\Writer\Style\Border; +use Box\Spout\Writer\Style\BorderBuilder; use Box\Spout\Writer\Style\Color; use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Style\StyleBuilder; @@ -208,7 +211,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase ]; $style = (new StyleBuilder())->setShouldWrapText()->build(); - $this->writeToODSFile($dataRows, $fileName,$style); + $this->writeToODSFile($dataRows, $fileName, $style); $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); $this->assertEquals(2, count($styleElements), 'There should be 2 styles (default and custom)'); @@ -236,6 +239,62 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->assertFirstChildHasAttributeEquals('wrap', $customStyleElement, 'table-cell-properties', 'fo:wrap-option'); } + /** + * @return void + */ + public function testBorders() + { + $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()) + ->setBorderBottom(Border::STYLE_SOLID, Color::GREEN, Border::WIDTH_THICK)->build(); + + + $borderTopRedThinDashed = (new BorderBuilder()) + ->setBorderTop(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build(); + + $styles = [ + (new StyleBuilder())->setBorder($borderBottomGreenThickSolid)->build(), + (new StyleBuilder())->build(), + (new StyleBuilder())->setBorder($borderTopRedThinDashed)->build(), + ]; + + $this->writeToODSFileWithMultipleStyles($dataRows, $fileName, $styles); + + $styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName); + $this->assertEquals(3, count($styleElements), 'There should be 3 styles)'); + + $expectedFirst = sprintf( + '%s %s #%s', + BorderHelper::$widthMap[Border::WIDTH_THICK], + BorderHelper::$styleMap[Border::STYLE_SOLID], + Color::GREEN + ); + + $this->assertEquals($expectedFirst, $styleElements[1] + ->getElementsByTagName('table-cell-properties') + ->item(0) + ->getAttribute('fo:border-bottom')); + + $expectedThird = sprintf( + '%s %s #%s', + BorderHelper::$widthMap[Border::WIDTH_THIN], + BorderHelper::$styleMap[Border::STYLE_DASHED], + Color::RED + ); + $this->assertEquals($expectedThird, $styleElements[2] + ->getElementsByTagName('table-cell-properties') + ->item(0) + ->getAttribute('fo:border-top')); + } + + /** * @param array $allRows * @param string $fileName diff --git a/tests/Spout/Writer/Style/BorderTest.php b/tests/Spout/Writer/Style/BorderTest.php new file mode 100644 index 0000000..a73498e --- /dev/null +++ b/tests/Spout/Writer/Style/BorderTest.php @@ -0,0 +1,112 @@ +addPart(new BorderPart(Border::LEFT)) + ->addPart(new BorderPart(Border::RIGHT)) + ->addPart(new BorderPart(Border::TOP)) + ->addPart(new BorderPart(Border::BOTTOM)) + ->addPart(new BorderPart(Border::LEFT)); + + $this->assertEquals(4, count($border->getParts()), 'There should never be more than 4 border parts'); + } + + /** + * @return void + */ + public function testSetParts() + { + $border = new Border(); + $border->setParts([ + new BorderPart(Border::LEFT) + ]); + + $this->assertEquals(1, count($border->getParts()), 'It should be possible to set the border parts'); + } + + /** + * @return void + */ + public function testBorderBuilderFluent() + { + $border = (new BorderBuilder()) + ->setBorderBottom() + ->setBorderTop() + ->setBorderLeft() + ->setBorderRight() + ->build(); + $this->assertEquals(4, count($border->getParts()), 'The border builder exposes a fluent interface'); + } + + /** + * :D :S + * @return void + */ + public function testAnyCombinationOfAllowedBorderPartsParams() + { + $color = Color::BLACK; + foreach(BorderPart::getAllowedNames() as $allowedName) + { + foreach(BorderPart::getAllowedStyles() as $allowedStyle) + { + foreach(BorderPart::getAllowedWidths() as $allowedWidth) + { + $borderPart = new BorderPart($allowedName, $allowedStyle, $color, $allowedWidth); + $border = new Border(); + $border->addPart($borderPart); + $this->assertEquals(1, count($border->getParts())); + + $part = $border->getParts()[$allowedName]; + /** @var $part BorderPart */ + $this->assertEquals($allowedStyle, $part->getStyle()); + $this->assertEquals($allowedWidth, $part->getWidth()); + $this->assertEquals($color, $part->getColor()); + } + } + } + } +} \ No newline at end of file diff --git a/tests/Spout/Writer/Style/StyleTest.php b/tests/Spout/Writer/Style/StyleTest.php index fb93e9b..03e31fe 100644 --- a/tests/Spout/Writer/Style/StyleTest.php +++ b/tests/Spout/Writer/Style/StyleTest.php @@ -129,4 +129,32 @@ class StyleTest extends \PHPUnit_Framework_TestCase $this->assertTrue($currentStyle->serialize() === $mergedStyle->serialize()); } + + /** + * @return void + */ + public function testStyleBuilderShouldApplyBorders() + { + $border = (new BorderBuilder()) + ->setBorderBottom() + ->build(); + $style = (new StyleBuilder())->setBorder($border)->build(); + $this->assertTrue($style->shouldApplyBorder()); + } + + /** + * @return void + */ + public function testStyleBuilderShouldMergeBorders() + { + $border = (new BorderBuilder())->setBorderBottom(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build(); + + $baseStyle = (new StyleBuilder())->setBorder($border)->build(); + $currentStyle = (new StyleBuilder())->build(); + $mergedStyle = $currentStyle->mergeWith($baseStyle); + + $this->assertEquals(null, $currentStyle->getBorder(), 'Current style has no border'); + $this->assertInstanceOf('Box\Spout\Writer\Style\Border', $baseStyle->getBorder(), 'Base style has a border'); + $this->assertInstanceOf('Box\Spout\Writer\Style\Border', $mergedStyle->getBorder(), 'Merged style has a border'); + } } diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index d43b566..e013f9b 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -5,6 +5,8 @@ namespace Box\Spout\Writer\XLSX; use Box\Spout\Common\Type; use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\TestUsingResource; +use Box\Spout\Writer\Style\Border; +use Box\Spout\Writer\Style\BorderBuilder; use Box\Spout\Writer\Style\Color; use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Style\StyleBuilder; @@ -205,7 +207,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase ]; $style = (new StyleBuilder())->setShouldWrapText()->build(); - $this->writeToXLSXFile($dataRows, $fileName,$style); + $this->writeToXLSXFile($dataRows, $fileName, $style); $cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs'); $xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1); @@ -232,6 +234,40 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText'); } + /** + * @return void + */ + public function testBorders() + { + $fileName = 'test_borders.xlsx'; + + $dataRows = [ + ['row-with-border-bottom-green-thick-solid'], + ['row-without-border'], + ['row-with-border-top-red-thin-dashed'], + ]; + + $borderBottomGreenThickSolid = (new BorderBuilder()) + ->setBorderBottom(Border::STYLE_SOLID, Color::GREEN, Border::WIDTH_THICK)->build(); + + + $borderTopRedThinDashed = (new BorderBuilder()) + ->setBorderTop(Border::STYLE_DASHED, Color::RED, Border::WIDTH_THIN)->build(); + + $styles = [ + (new StyleBuilder())->setBorder($borderBottomGreenThickSolid)->build(), + (new StyleBuilder())->build(), + (new StyleBuilder())->setBorder($borderTopRedThinDashed)->build(), + ]; + + $this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, $styles); + $borderElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'borders'); + $this->assertEquals(3, $borderElements->getAttribute('count'), '3 borders present'); + + $styleXfsElements = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs'); + $this->assertEquals(3, $styleXfsElements->getAttribute('count'), '3 cell xfs present'); + } + /** * @param array $allRows * @param string $fileName