Added first XLSX tests

ODS Tests
This commit is contained in:
madflow 2016-05-29 14:01:36 +02:00
parent 47cafb0216
commit 38a39edc9e
4 changed files with 237 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,112 @@
<?php
namespace Box\Spout\Writer\Style;
class BorderTest extends \PHPUnit_Framework_TestCase
{
/**
* @return void
*/
public function testValidInstance()
{
$noConstructorParams = new Border();
$withConstructorParams = new Border([
new BorderPart(Border::LEFT)
]);
}
/**
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidNameException
*/
public function testInvalidBorderPart()
{
$invalidBorderPart = new BorderPart('invalid');
}
/**
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidStyleException
*/
public function testInvalidBorderPartStyle()
{
$invalidBorderPartStyle = new BorderPart(Border::LEFT, 'invalid');
}
/**
* @expectedException \Box\Spout\Writer\Exception\Border\InvalidWidthException
*/
public function testInvalidBorderPartWidth()
{
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Border::STYLE_DASHED, Color::BLACK, 'invalid');
}
/**
* @return void
*/
public function testNotMoreThanFourPartsPossible()
{
$border = new Border();
$border
->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());
}
}
}
}
}

View File

@ -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');
}
}

View File

@ -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