ODS writer
Added ODS writer Refactored XLSX writer to abstract some pieces into an abstract multi-sheets writer Created an abstract style helper Moved shared components around
This commit is contained in:
1 parent
a8f1fba854
commit
5949cb2442
39 files changed
+2924
-533
No files matched your search
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Helper;
|
||||
|
||||
/**
|
||||
* Class CellHelperTest
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX\Helper
|
||||
*/
|
||||
class CellHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestGetCellIndexFromColumnIndex()
|
||||
{
|
||||
return [
|
||||
[0, 'A'],
|
||||
[1, 'B'],
|
||||
[25, 'Z'],
|
||||
[26, 'AA'],
|
||||
[28, 'AC'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestGetCellIndexFromColumnIndex
|
||||
*
|
||||
* @param int $columnIndex
|
||||
* @param string $expectedCellIndex
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCellIndexFromColumnIndex($columnIndex, $expectedCellIndex)
|
||||
{
|
||||
$this->assertEquals($expectedCellIndex, CellHelper::getCellIndexFromColumnIndex($columnIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsNonEmptyString()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isNonEmptyString("string"));
|
||||
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(""));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(0));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(1));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(true));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(false));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(["string"]));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isNonEmptyString(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsNumeric()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isNumeric(0));
|
||||
$this->assertTrue(CellHelper::isNumeric(10));
|
||||
$this->assertTrue(CellHelper::isNumeric(10.1));
|
||||
$this->assertTrue(CellHelper::isNumeric(10.10000000000000000000001));
|
||||
$this->assertTrue(CellHelper::isNumeric(0x539));
|
||||
$this->assertTrue(CellHelper::isNumeric(02471));
|
||||
$this->assertTrue(CellHelper::isNumeric(0b10100111001));
|
||||
$this->assertTrue(CellHelper::isNumeric(1337e0));
|
||||
|
||||
$this->assertFalse(CellHelper::isNumeric("0"));
|
||||
$this->assertFalse(CellHelper::isNumeric("42"));
|
||||
$this->assertFalse(CellHelper::isNumeric(true));
|
||||
$this->assertFalse(CellHelper::isNumeric([2]));
|
||||
$this->assertFalse(CellHelper::isNumeric(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isNumeric(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function testIsBoolean()
|
||||
{
|
||||
$this->assertTrue(CellHelper::isBoolean(true));
|
||||
$this->assertTrue(CellHelper::isBoolean(false));
|
||||
|
||||
$this->assertFalse(CellHelper::isBoolean(0));
|
||||
$this->assertFalse(CellHelper::isBoolean(1));
|
||||
$this->assertFalse(CellHelper::isBoolean("0"));
|
||||
$this->assertFalse(CellHelper::isBoolean("1"));
|
||||
$this->assertFalse(CellHelper::isBoolean("true"));
|
||||
$this->assertFalse(CellHelper::isBoolean("false"));
|
||||
$this->assertFalse(CellHelper::isBoolean([true]));
|
||||
$this->assertFalse(CellHelper::isBoolean(new \stdClass()));
|
||||
$this->assertFalse(CellHelper::isBoolean(null));
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\XLSX;
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Sheet;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
/**
|
||||
@@ -51,64 +52,6 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSheetNameEquals($customSheetName, $fileName, "The sheet name should have been changed to '$customSheetName'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForInvalidSheetNames()
|
||||
{
|
||||
return [
|
||||
[null],
|
||||
[21],
|
||||
[''],
|
||||
['this title exceeds the 31 characters limit'],
|
||||
['Illegal \\'],
|
||||
['Illegal /'],
|
||||
['Illegal ?'],
|
||||
['Illegal *'],
|
||||
['Illegal :'],
|
||||
['Illegal ['],
|
||||
['Illegal ]'],
|
||||
['\'Illegal start'],
|
||||
['Illegal end\''],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForInvalidSheetNames
|
||||
* @expectedException \Box\Spout\Writer\Exception\InvalidSheetNameException
|
||||
*
|
||||
* @param string $customSheetName
|
||||
* @return void
|
||||
*/
|
||||
public function testSetSheetNameShouldThrowOnInvalidName($customSheetName)
|
||||
{
|
||||
$fileName = 'test_set_name_with_invalid_name_should_throw_exception.xlsx';
|
||||
$this->writeDataAndReturnSheetWithCustomName($fileName, $customSheetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSetSheetNameShouldNotThrowWhenSettingSameNameAsCurrentOne()
|
||||
{
|
||||
$fileName = 'test_set_name_with_same_as_current.xlsx';
|
||||
$this->createGeneratedFolderIfNeeded($fileName);
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer->openToFile($resourcePath);
|
||||
|
||||
$customSheetName = 'Sheet name';
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
$sheet->setName($customSheetName);
|
||||
$sheet->setName($customSheetName);
|
||||
|
||||
$writer->addRow(['xlsx--11', 'xlsx--12']);
|
||||
$writer->close();
|
||||
|
||||
$this->assertSheetNameEquals($customSheetName, $fileName, "The sheet name should have been changed to '$customSheetName'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Writer\Exception\InvalidSheetNameException
|
||||
* @return void
|
||||
@@ -186,6 +129,6 @@ class SheetTest extends \PHPUnit_Framework_TestCase
|
||||
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
|
||||
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
|
||||
|
||||
$this->assertContains('<sheet name="' . $expectedName . '"', $xmlContents, $message);
|
||||
$this->assertContains("<sheet name=\"$expectedName\"", $xmlContents, $message);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
/**
|
||||
* Class XLSXTest
|
||||
* Class WriterTest
|
||||
*
|
||||
* @package Box\Spout\Writer\XLSX
|
||||
*/
|
||||
@@ -169,7 +169,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetUsingInlineStrings()
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingInlineStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_inline_strings.xlsx';
|
||||
$dataRows = [
|
||||
@@ -212,7 +212,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetUsingSharedStrings()
|
||||
public function testAddRowShouldWriteGivenDataToTwoSheetsUsingSharedStrings()
|
||||
{
|
||||
$fileName = 'test_add_row_should_write_given_data_to_two_sheets_using_shared_strings.xlsx';
|
||||
$dataRows = [
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
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\Color;
|
||||
use Box\Spout\Writer\Style\Style;
|
||||
@@ -96,7 +97,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowWithStyleShouldListAllUsedFontsInCreateStylesXmlFile()
|
||||
public function testAddRowWithStyleShouldListAllUsedFontsInCreatedStylesXmlFile()
|
||||
{
|
||||
$fileName = 'test_add_row_with_style_should_list_all_used_fonts.xlsx';
|
||||
$dataRows = [
|
||||
@@ -113,7 +114,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$style2 = (new StyleBuilder())
|
||||
->setFontSize(15)
|
||||
->setFontColor(Color::RED)
|
||||
->setFontName('Arial')
|
||||
->setFontName('Font')
|
||||
->build();
|
||||
|
||||
$this->writeToXLSXFileWithMultipleStyles($dataRows, $fileName, [$style, $style2]);
|
||||
@@ -128,7 +129,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$defaultFontElement = $fontElements->item(0);
|
||||
$this->assertChildrenNumEquals(3, $defaultFontElement, 'The default font should only have 3 properties.');
|
||||
$this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $defaultFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Style::DEFAULT_FONT_COLOR, $defaultFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $defaultFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $defaultFontElement, 'name', 'val');
|
||||
|
||||
// Second font should contain data from the first created style
|
||||
@@ -139,34 +140,15 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertChildExists($secondFontElement, 'u');
|
||||
$this->assertChildExists($secondFontElement, 'strike');
|
||||
$this->assertFirstChildHasAttributeEquals((string) Writer::DEFAULT_FONT_SIZE, $secondFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Style::DEFAULT_FONT_COLOR, $secondFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Color::toARGB(Style::DEFAULT_FONT_COLOR), $secondFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals(Writer::DEFAULT_FONT_NAME, $secondFontElement, 'name', 'val');
|
||||
|
||||
// Third font should contain data from the second created style
|
||||
$thirdFontElement = $fontElements->item(2);
|
||||
$this->assertChildrenNumEquals(3, $thirdFontElement, 'The font should only have 3 properties.');
|
||||
$this->assertFirstChildHasAttributeEquals('15', $thirdFontElement, 'sz', 'val');
|
||||
$this->assertFirstChildHasAttributeEquals(Color::RED, $thirdFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals('Arial', $thirdFontElement, 'name', 'val');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
|
||||
{
|
||||
$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);
|
||||
|
||||
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
|
||||
$this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText');
|
||||
$this->assertFirstChildHasAttributeEquals(Color::toARGB(Color::RED), $thirdFontElement, 'color', 'rgb');
|
||||
$this->assertFirstChildHasAttributeEquals('Font', $thirdFontElement, 'name', 'val');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,6 +194,25 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('1', $cellDomElements[1]->getAttribute('s'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRowWithStyleShouldAddWrapTextAlignmentInfoInStylesXmlFileIfSpecified()
|
||||
{
|
||||
$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);
|
||||
|
||||
$cellXfsDomElement = $this->getXmlSectionFromStylesXmlFile($fileName, 'cellXfs');
|
||||
$xfElement = $cellXfsDomElement->getElementsByTagName('xf')->item(1);
|
||||
$this->assertEquals(1, $xfElement->getAttribute('applyAlignment'));
|
||||
$this->assertFirstChildHasAttributeEquals('1', $xfElement, 'alignment', 'wrapText');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -294,12 +295,9 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$pathToStylesXmlFile = $resourcePath . '#xl/styles.xml';
|
||||
|
||||
$xmlReader = new \XMLReader();
|
||||
$xmlReader = new XMLReader();
|
||||
$xmlReader->open('zip://' . $pathToStylesXmlFile);
|
||||
|
||||
while ($xmlReader->read() && ($xmlReader->nodeType !== \XMLReader::ELEMENT || $xmlReader->name !== $section)) {
|
||||
// do nothing
|
||||
}
|
||||
$xmlReader->readUntilNodeFound($section);
|
||||
|
||||
return $xmlReader->expand();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user