Writer\Ods\Content: basic unit tests

This commit is contained in:
Paolo Agostinetto 2017-03-04 15:22:20 +01:00
parent e60897ae5f
commit 5620510991
3 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods\Content;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
use PhpOffice\PhpSpreadsheetTests\Worksheet\WorksheetColumnTest;
class ContentTest extends \PHPUnit_Framework_TestCase
{
public $samplesPath = __DIR__ . '/../../../data/Writer/Ods';
public function testWriteEmptySpreadsheet()
{
$content = new Content();
$content->setParentWriter(new Ods(new Spreadsheet()));
$xml = $content->write();
$this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-empty.xml", $xml);
}
public function testWriteSpreadsheet()
{
$workbook = new Spreadsheet();
// Worksheet 1
$worksheet1 = $workbook->getActiveSheet();
$worksheet1->setCellValue('A1', 1); // Number
$worksheet1->setCellValue('B1', 12345.6789); // Number
$worksheet1->setCellValue('C1', "1"); // Number without cast
$worksheet1->setCellValueExplicit('D1', "01234", DataType::TYPE_STRING); // Number casted to string
$worksheet1->setCellValue('E1', "Lorem ipsum"); // String
$worksheet1->setCellValue('A2', true); // Boolean
$worksheet1->setCellValue('B2', false); // Boolean
$worksheet1->setCellValue('C2', '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'); // Formula
$worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date
$worksheet1->getStyle('D2')
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
// Worksheet 2
$worksheet2 = $workbook->createSheet();
$worksheet2->setTitle('New Worksheet');
$worksheet2->setCellValue('A1', 2);
// Write
$content = new Content();
$content->setParentWriter(new Ods($workbook));
$xml = $content->write();
$this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-with-data.xml", $xml);
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
<office:scripts />
<office:font-face-decls />
<office:automatic-styles />
<office:body>
<office:spreadsheet>
<table:calculation-settings />
<table:table table:name="Worksheet">
<office:forms />
<table:table-column table:number-columns-repeated="1024" />
<table:table-row>
<table:table-cell />
<table:table-cell table:number-columns-repeated="1023" />
</table:table-row>
</table:table>
<table:named-expressions />
</office:spreadsheet>
</office:body>
</office:document-content>

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
<office:scripts />
<office:font-face-decls />
<office:automatic-styles />
<office:body>
<office:spreadsheet>
<table:calculation-settings />
<table:table table:name="Worksheet">
<office:forms />
<table:table-column table:number-columns-repeated="1024" />
<table:table-row>
<table:table-cell office:value-type="float" office:value="1">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell office:value-type="float" office:value="12345.6789">
<text:p>12345.6789</text:p>
</table:table-cell>
<table:table-cell office:value-type="float" office:value="1">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell office:value-type="string">
<text:p>01234</text:p>
</table:table-cell>
<table:table-cell office:value-type="string">
<text:p>Lorem ipsum</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1019" />
</table:table-row>
<table:table-row>
<table:table-cell office:value-type="boolean" office:value="1">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell office:value-type="boolean" office:value="">
<text:p></text:p>
</table:table-cell>
<table:table-cell table:formula="of:=IF(A3, CONCATENATE(A1, &quot; &quot;, A2), CONCATENATE(A2, &quot; &quot;, A1))" office:value-type="string" office:value="1 TRUE">
<text:p>1 TRUE</text:p>
</table:table-cell>
<table:table-cell office:value-type="float" office:value="42798.572060185">
<text:p>42798.572060185</text:p>
</table:table-cell>
<table:table-cell />
<table:table-cell table:number-columns-repeated="1019" />
</table:table-row>
</table:table>
<table:table table:name="New Worksheet">
<office:forms />
<table:table-column table:number-columns-repeated="1024" />
<table:table-row>
<table:table-cell office:value-type="float" office:value="2">
<text:p>2</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023" />
</table:table-row>
</table:table>
<table:named-expressions />
</office:spreadsheet>
</office:body>
</office:document-content>