Merge remote-tracking branch 'remotes/origin/master' into custom-column-widths

# Conflicts:
#	src/Spout/Writer/XLSX/Manager/WorksheetManager.php
This commit is contained in:
Alexander Hofstede committed 2019-12-19 23:02:47 +01:00
commit 49e621b822
102 files changed
+1051 -381

No files matched your search

+14
View File
@@ -78,10 +78,24 @@ class RowTest extends \PHPUnit\Framework\TestCase
$row = new Row([], null);
$cellMock = $this->getCellMock();
$row->setCellAtIndex($cellMock, 3);
$this->assertEquals($cellMock, $row->getCellAtIndex(3));
$this->assertNull($row->getCellAtIndex(10));
}
/**
* @return void
*/
public function testSetCellAtIndex()
{
$row = new Row([], null);
$cellMock = $this->getCellMock();
$row->setCellAtIndex($cellMock, 1);
$this->assertEquals(2, $row->getNumCells());
$this->assertNull($row->getCellAtIndex(0));
}
/**
* @return void
*/
+11
View File
@@ -277,6 +277,17 @@ class ReaderTest extends TestCase
$this->assertEquals([$expectedRow], $allRows);
}
/**
* @return void
*/
public function testReadShouldSupportWhitespaceAsXML()
{
$allRows = $this->getAllRowsForFile('sheet_with_whitespaces_as_xml.ods');
$expectedRow = ["Lorem ipsum\tdolor sit amet"];
$this->assertEquals([$expectedRow], $allRows);
}
/**
* @NOTE: The LIBXML_NOENT is used to ACTUALLY substitute entities (and should therefore not be used)
*
@@ -3,7 +3,9 @@
namespace Box\Spout\Writer\Common\Creator\Style;
use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use PHPUnit\Framework\TestCase;
@@ -41,4 +43,22 @@ class StyleBuilderTest extends TestCase
$this->assertInstanceOf(Border::class, $baseStyle->getBorder(), 'Base style has a border');
$this->assertInstanceOf(Border::class, $mergedStyle->getBorder(), 'Merged style has a border');
}
/**
* @return void
*/
public function testStyleBuilderShouldApplyCellAlignment()
{
$style = (new StyleBuilder())->setCellAlignment(CellAlignment::CENTER)->build();
$this->assertTrue($style->shouldApplyCellAlignment());
}
/**
* @return void
*/
public function testStyleBuilderShouldThrowOnInvalidCellAlignment()
{
$this->expectException(InvalidArgumentException::class);
(new StyleBuilder())->setCellAlignment('invalid_cell_alignment')->build();
}
}
@@ -12,7 +12,7 @@ class CellHelperTest extends TestCase
/**
* @return array
*/
public function dataProviderForTestGetCellIndexFromColumnIndex()
public function dataProviderForTestGetColumnLettersFromColumnIndex()
{
return [
[0, 'A'],
@@ -24,14 +24,15 @@ class CellHelperTest extends TestCase
}
/**
* @dataProvider dataProviderForTestGetCellIndexFromColumnIndex
* @dataProvider dataProviderForTestGetColumnLettersFromColumnIndex
*
* @param int $columnIndex
* @param string $expectedColumnLetters
*
* @param int $columnIndex
* @param string $expectedCellIndex
* @return void
*/
public function testGetCellIndexFromColumnIndex($columnIndex, $expectedCellIndex)
public function testGetColumnLettersFromColumnIndex($columnIndex, $expectedColumnLetters)
{
$this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex));
$this->assertEquals($expectedColumnLetters, CellHelper::getColumnLettersFromColumnIndex($columnIndex));
}
}
@@ -45,6 +45,7 @@ class StyleMergerTest extends TestCase
->setFontBold()
->setFontColor(Color::YELLOW)
->setBackgroundColor(Color::BLUE)
->setFormat('0.00')
->build();
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
$mergedStyle = $this->styleMerger->merge($currentStyle, $baseStyle);
@@ -60,6 +61,7 @@ class StyleMergerTest extends TestCase
$this->assertTrue($mergedStyle->isFontUnderline());
$this->assertEquals(Color::YELLOW, $mergedStyle->getFontColor());
$this->assertEquals(Color::BLUE, $mergedStyle->getBackgroundColor());
$this->assertEquals('0.00', $mergedStyle->getFormat());
}
/**
+19
View File
@@ -2,6 +2,7 @@
namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Entity\Cell;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException;
@@ -321,6 +322,24 @@ class WriterTest extends TestCase
}
}
/**
* @return void
*/
public function testAddRowShouldSupportCellInError()
{
$fileName = 'test_add_row_should_support_cell_in_error.ods';
$cell = WriterEntityFactory::createCell('#DIV/0');
$cell->setType(Cell::TYPE_ERROR);
$row = WriterEntityFactory::createRow([$cell]);
$this->writeToODSFile([$row], $fileName);
$this->assertValueWasWritten($fileName, 'calcext:value-type="error"');
$this->assertValueWasWritten($fileName, '<text:p>#DIV/0</text:p>');
}
/**
* @return void
*/
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Reader\Wrapper\XMLReader;
@@ -204,6 +205,25 @@ class WriterWithStyleTest extends TestCase
$this->assertFirstChildHasAttributeEquals('wrap', $customStyleElement, 'table-cell-properties', 'fo:wrap-option');
}
/**
* @return void
*/
public function testAddRowShouldApplyCellAlignment()
{
$fileName = 'test_add_row_should_apply_cell_alignment.xlsx';
$rightAlignedStyle = (new StyleBuilder())->setCellAlignment(CellAlignment::RIGHT)->build();
$dataRows = $this->createStyledRowsFromValues([['ods--11']], $rightAlignedStyle);
$this->writeToODSFile($dataRows, $fileName);
$styleElements = $this->getCellStyleElementsFromContentXmlFile($fileName);
$this->assertCount(2, $styleElements, 'There should be 2 styles (default and custom)');
$customStyleElement = $styleElements[1];
$this->assertFirstChildHasAttributeEquals('end', $customStyleElement, 'paragraph-properties', 'fo:text-align');
}
/**
* @return void
*/
@@ -58,20 +58,50 @@ class StyleRegistryTest extends TestCase
$borderRight = (new BorderBuilder())->setBorderRight()->build();
$styleBorderLeft = (new StyleBuilder())->setBorder($borderLeft)->build();
$styleBoderRight = (new StyleBuilder())->setBorder($borderRight)->build();
$styleBoderRightBold = (new StyleBuilder())->setBorder($borderRight)->setFontBold()->build();
$styleBorderRight = (new StyleBuilder())->setBorder($borderRight)->build();
$styleBorderRightBold = (new StyleBuilder())->setBorder($borderRight)->setFontBold()->build();
$styleNoBorder = (new StyleBuilder())->setFontItalic()->build();
$styleRegistry->registerStyle($styleBorderLeft);
$styleRegistry->registerStyle($styleBoderRight);
$styleRegistry->registerStyle($styleBoderRightBold);
$styleRegistry->registerStyle($styleBorderRight);
$styleRegistry->registerStyle($styleBorderRightBold);
$styleRegistry->registerStyle($styleNoBorder);
$this->assertCount(2, $styleRegistry->getRegisteredBorders(), 'There should be 2 registered borders');
$this->assertEquals(1, $styleRegistry->getBorderIdForStyleId($styleBorderLeft->getId()), 'First style with border set should have index 1 (0 is for the default style)');
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBoderRight->getId()), 'Second style with border set - different from first style - should have index 2');
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBoderRightBold->getId()), 'Style with border already set should have the same index');
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBorderRight->getId()), 'Second style with border set - different from first style - should have index 2');
$this->assertEquals(2, $styleRegistry->getBorderIdForStyleId($styleBorderRightBold->getId()), 'Style with border already set should have the same index');
$this->assertEquals(0, $styleRegistry->getBorderIdForStyleId($styleNoBorder->getId()), 'Style with no border should have index 0');
}
/**
* @return void
*/
public function testRegisterStyleAlsoRegistersFormats()
{
$styleRegistry = $this->getStyleRegistry();
$styleBuiltinFormat = (new StyleBuilder())
->setFontBold()
->setFormat('0.00')//Builtin format
->build();
$styleUserFormat = (new StyleBuilder())
->setFontBold()
->setFormat('0.000')
->build();
$styleNoFormat = (new StyleBuilder())->setFontItalic()->build();
$styleRegistry->registerStyle($styleBuiltinFormat);
$styleRegistry->registerStyle($styleUserFormat);
$styleRegistry->registerStyle($styleNoFormat);
$this->assertCount(2, $styleRegistry->getRegisteredFormats(), 'There should be 2 registered formats');
$this->assertEquals(2, $styleRegistry->getFormatIdForStyleId($styleBuiltinFormat->getId()), 'First style with builtin format set should have index 2 (0 is for the default style)');
$this->assertEquals(164, $styleRegistry->getFormatIdForStyleId($styleUserFormat->getId()), 'Second style with user format set should have index 164 (0 is for the default style)');
$this->assertEquals(0, $styleRegistry->getFormatIdForStyleId($styleNoFormat->getId()), 'Style with no format should have index 0');
}
}
+18
View File
@@ -2,6 +2,7 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Entity\Cell;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException;
@@ -375,6 +376,23 @@ class WriterTest extends TestCase
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 10.2);
}
/**
* @return void
*/
public function testAddRowShouldSupportCellInError()
{
$fileName = 'test_add_row_should_support_cell_in_error.xlsx';
$cell = WriterEntityFactory::createCell('#DIV/0');
$cell->setType(Cell::TYPE_ERROR);
$row = WriterEntityFactory::createRow([$cell]);
$this->writeToXLSXFile([$row], $fileName);
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 't="e"><v>#DIV/0</v>');
}
/**
* @return void
*/
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Reader\Wrapper\XMLReader;
@@ -210,6 +211,43 @@ class WriterWithStyleTest extends TestCase
$this->assertEquals('1', $cellDomElements[1]->getAttribute('s'));
}
/**
* @return void
*/
public function testAddRowWithNumFmtStyles()
{
$fileName = 'test_add_row_with_numfmt.xlsx';
$style = (new StyleBuilder())
->setFontBold()
->setFormat('0.00')//Builtin format
->build();
$style2 = (new StyleBuilder())
->setFontBold()
->setFormat('0.000')
->build();
$dataRows = [
$this->createStyledRowFromValues([1.123456789], $style),
$this->createStyledRowFromValues([12.1], $style2),
];
$this->writeToXLSXFile($dataRows, $fileName);
$formatsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'numFmts');
$this->assertEquals(
1,
$formatsDomElement->getAttribute('count'),
'There should be 2 formats, including the default one'
);
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
foreach ([2, 164] as $index => $expected) {
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item($index + 1);
$this->assertEquals($expected, $xfElement->getAttribute('numFmtId'));
}
}
/**
* @return void
*/
@@ -250,6 +288,24 @@ class WriterWithStyleTest extends TestCase
$this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText');
}
/**
* @return void
*/
public function testAddRowShouldApplyCellAlignment()
{
$fileName = 'test_add_row_should_apply_cell_alignment.xlsx';
$rightAlignedStyle = (new StyleBuilder())->setCellAlignment(CellAlignment::RIGHT)->build();
$dataRows = $this->createStyledRowsFromValues([['xlsx--11']], $rightAlignedStyle);
$this->writeToXLSXFile($dataRows, $fileName);
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
$this->assertFirstChildHasAttributeEquals(CellAlignment::RIGHT, $xfElement, 'alignment', 'horizontal');
}
/**
* @return void
*/