From 5ad68e0ba63cfcd3d8fef2adc9148bcedc3f8afc Mon Sep 17 00:00:00 2001 From: troosan Date: Wed, 15 Nov 2017 22:58:28 +0100 Subject: [PATCH] add tests, improve code coverage --- src/PhpWord/Metadata/DocInfo.php | 2 ++ .../Writer/Word2007/Element/CheckBox.php | 8 +----- .../Writer/Word2007/Element/FormField.php | 7 +---- src/PhpWord/Writer/Word2007/Element/Link.php | 8 +----- .../Writer/Word2007/Element/PreserveText.php | 14 ++-------- src/PhpWord/Writer/Word2007/Element/TOC.php | 11 +++----- src/PhpWord/Writer/Word2007/Element/Text.php | 8 +----- src/PhpWord/Writer/Word2007/Element/Title.php | 14 +++------- src/PhpWord/Writer/Word2007/Part/Chart.php | 10 +++---- .../Writer/Word2007/Part/DocPropsCustom.php | 6 ++++- tests/PhpWord/Metadata/DocInfoTest.php | 6 ++--- tests/PhpWord/Writer/Word2007/ElementTest.php | 10 ++++--- .../Writer/Word2007/Part/DocumentTest.php | 26 +++++++++++++++++++ 13 files changed, 57 insertions(+), 73 deletions(-) diff --git a/src/PhpWord/Metadata/DocInfo.php b/src/PhpWord/Metadata/DocInfo.php index 0508dcd0..e5dee659 100644 --- a/src/PhpWord/Metadata/DocInfo.php +++ b/src/PhpWord/Metadata/DocInfo.php @@ -467,6 +467,8 @@ class DocInfo $propertyType = self::PROPERTY_TYPE_INTEGER; } elseif (is_bool($propertyValue)) { $propertyType = self::PROPERTY_TYPE_BOOLEAN; + } elseif ($propertyValue instanceof \DateTime) { + $propertyType = self::PROPERTY_TYPE_DATE; } else { $propertyType = self::PROPERTY_TYPE_STRING; } diff --git a/src/PhpWord/Writer/Word2007/Element/CheckBox.php b/src/PhpWord/Writer/Word2007/Element/CheckBox.php index 31dcb867..83e8af81 100644 --- a/src/PhpWord/Writer/Word2007/Element/CheckBox.php +++ b/src/PhpWord/Writer/Word2007/Element/CheckBox.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * CheckBox element writer * @@ -83,11 +81,7 @@ class CheckBox extends Text $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($this->getText($element->getText())); - } else { - $xmlWriter->writeRaw($this->getText($element->getText())); - } + $xmlWriter->writeText($this->getText($element->getText())); $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r diff --git a/src/PhpWord/Writer/Word2007/Element/FormField.php b/src/PhpWord/Writer/Word2007/Element/FormField.php index 91fb28ab..d4afdec1 100644 --- a/src/PhpWord/Writer/Word2007/Element/FormField.php +++ b/src/PhpWord/Writer/Word2007/Element/FormField.php @@ -19,7 +19,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpWord\Element\FormField as FormFieldElement; -use PhpOffice\PhpWord\Settings; /** * FormField element writer @@ -90,11 +89,7 @@ class FormField extends Text $this->writeFontStyle(); $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($value); - } else { - $xmlWriter->writeRaw($value); - } + $xmlWriter->writeText($value); $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r diff --git a/src/PhpWord/Writer/Word2007/Element/Link.php b/src/PhpWord/Writer/Word2007/Element/Link.php index 8ea3f53c..072d665e 100644 --- a/src/PhpWord/Writer/Word2007/Element/Link.php +++ b/src/PhpWord/Writer/Word2007/Element/Link.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * Link element writer * @@ -54,11 +52,7 @@ class Link extends Text $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($element->getText()); - } else { - $xmlWriter->writeRaw($element->getText()); - } + $xmlWriter->writeText($element->getText()); $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:hyperlink diff --git a/src/PhpWord/Writer/Word2007/Element/PreserveText.php b/src/PhpWord/Writer/Word2007/Element/PreserveText.php index 92b9ea40..cf26a587 100644 --- a/src/PhpWord/Writer/Word2007/Element/PreserveText.php +++ b/src/PhpWord/Writer/Word2007/Element/PreserveText.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * PreserveText element writer * @@ -60,11 +58,7 @@ class PreserveText extends Text $xmlWriter->startElement('w:instrText'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($text); - } else { - $xmlWriter->writeRaw($text); - } + $xmlWriter->writeText($text); $xmlWriter->endElement(); $xmlWriter->endElement(); @@ -86,11 +80,7 @@ class PreserveText extends Text $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($this->getText($text)); - } else { - $xmlWriter->writeRaw($this->getText($text)); - } + $xmlWriter->writeText($this->getText($text)); $xmlWriter->endElement(); $xmlWriter->endElement(); } diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index a679188f..18a9399a 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -19,7 +19,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpWord\Element\TOC as TOCElement; -use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Writer\Word2007\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\Word2007\Style\Paragraph as ParagraphStyleWriter; @@ -97,13 +96,9 @@ class TOC extends AbstractElement $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle); $styleWriter->write(); } - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->writeElement('w:t', $title->getText()); - } else { - $xmlWriter->startElement('w:t'); - $xmlWriter->writeRaw($title->getText()); - $xmlWriter->endElement(); - } + $xmlWriter->startElement('w:t'); + $xmlWriter->writeText($title->getText()); + $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r $xmlWriter->startElement('w:r'); diff --git a/src/PhpWord/Writer/Word2007/Element/Text.php b/src/PhpWord/Writer/Word2007/Element/Text.php index 694a834a..85052226 100644 --- a/src/PhpWord/Writer/Word2007/Element/Text.php +++ b/src/PhpWord/Writer/Word2007/Element/Text.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * Text element writer * @@ -45,11 +43,7 @@ class Text extends AbstractElement $xmlWriter->startElement('w:t'); $xmlWriter->writeAttribute('xml:space', 'preserve'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->text($this->getText($element->getText())); - } else { - $xmlWriter->writeRaw($this->getText($element->getText())); - } + $xmlWriter->writeText($this->getText($element->getText())); $xmlWriter->endElement(); $xmlWriter->endElement(); // w:r diff --git a/src/PhpWord/Writer/Word2007/Element/Title.php b/src/PhpWord/Writer/Word2007/Element/Title.php index 63ed94de..f2a1d1ca 100644 --- a/src/PhpWord/Writer/Word2007/Element/Title.php +++ b/src/PhpWord/Writer/Word2007/Element/Title.php @@ -17,8 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; -use PhpOffice\PhpWord\Settings; - /** * TextRun element writer * @@ -60,14 +58,10 @@ class Title extends AbstractElement // Actual text $xmlWriter->startElement('w:r'); - if (Settings::isOutputEscapingEnabled()) { - $xmlWriter->writeElement('w:t', $this->getText($element->getText())); - } else { - $xmlWriter->startElement('w:t'); - $xmlWriter->writeRaw($this->getText($element->getText())); - $xmlWriter->endElement(); - } - $xmlWriter->endElement(); + $xmlWriter->startElement('w:t'); + $xmlWriter->writeText($this->getText($element->getText())); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r // Bookmark end $xmlWriter->startElement('w:bookmarkEnd'); diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 2f162108..2d8f618d 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -235,13 +235,9 @@ class Chart extends AbstractPart foreach ($values as $value) { $xmlWriter->startElement('c:pt'); $xmlWriter->writeAttribute('idx', $index); - if (\PhpOffice\PhpWord\Settings::isOutputEscapingEnabled()) { - $xmlWriter->writeElement('c:v', $value); - } else { - $xmlWriter->startElement('c:v'); - $xmlWriter->writeRaw($value); - $xmlWriter->endElement(); - } + $xmlWriter->startElement('c:v'); + $xmlWriter->writeText($value); + $xmlWriter->endElement(); // c:v $xmlWriter->endElement(); // c:pt $index++; } diff --git a/src/PhpWord/Writer/Word2007/Part/DocPropsCustom.php b/src/PhpWord/Writer/Word2007/Part/DocPropsCustom.php index 212e9d27..8ee2f028 100644 --- a/src/PhpWord/Writer/Word2007/Part/DocPropsCustom.php +++ b/src/PhpWord/Writer/Word2007/Part/DocPropsCustom.php @@ -60,7 +60,11 @@ class DocPropsCustom extends AbstractPart $xmlWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false'); break; case 'd': - $xmlWriter->writeElement('vt:filetime', date($this->dateFormat, $propertyValue)); + if ($propertyValue instanceof \DateTime) { + $xmlWriter->writeElement('vt:filetime', $propertyValue->format($this->dateFormat)); + } else { + $xmlWriter->writeElement('vt:filetime', date($this->dateFormat, $propertyValue)); + } break; default: $xmlWriter->writeElement('vt:lpwstr', $propertyValue); diff --git a/tests/PhpWord/Metadata/DocInfoTest.php b/tests/PhpWord/Metadata/DocInfoTest.php index 01659773..d9b44dc6 100644 --- a/tests/PhpWord/Metadata/DocInfoTest.php +++ b/tests/PhpWord/Metadata/DocInfoTest.php @@ -193,8 +193,7 @@ class DocInfoTest extends \PHPUnit\Framework\TestCase $this->assertEquals('value5', $oProperties->getCustomPropertyValue('key5')); $this->assertNull($oProperties->getCustomPropertyValue('key6')); $this->assertTrue($oProperties->isCustomPropertySet('key5')); - // todo: change to assertNotTrue when got upgraded to PHPUnit 4.x - $this->assertEquals(false, $oProperties->isCustomPropertySet('key6')); + $this->assertNotTrue($oProperties->isCustomPropertySet('key6')); $this->assertEquals(array('key1', 'key2', 'key3', 'key4', 'key5'), $oProperties->getCustomProperties()); } @@ -211,8 +210,7 @@ class DocInfoTest extends \PHPUnit\Framework\TestCase $this->assertEquals('8.3', DocInfo::convertProperty('8.3', 'lpstr')); $this->assertEquals(strtotime('10/11/2013'), DocInfo::convertProperty('10/11/2013', 'date')); $this->assertTrue(DocInfo::convertProperty('true', 'bool')); - // todo: change to assertNotTrue when got upgraded to PHPUnit 4.x - $this->assertEquals(false, DocInfo::convertProperty('1', 'bool')); + $this->assertNotTrue(DocInfo::convertProperty('1', 'bool')); $this->assertEquals('1', DocInfo::convertProperty('1', 'array')); $this->assertEquals('1', DocInfo::convertProperty('1', '')); diff --git a/tests/PhpWord/Writer/Word2007/ElementTest.php b/tests/PhpWord/Writer/Word2007/ElementTest.php index aeceaebc..f3c0d553 100644 --- a/tests/PhpWord/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Writer/Word2007/ElementTest.php @@ -313,14 +313,16 @@ class ElementTest extends \PHPUnit\Framework\TestCase $section->addFormField('textinput')->setName('MyTextBox'); $section->addFormField('checkbox')->setDefault(true)->setValue('Your name'); + $section->addFormField('checkbox')->setDefault(true); $section->addFormField('dropdown')->setEntries(array('Choice 1', 'Choice 2', 'Choice 3')); $doc = TestHelperDOCX::getDocument($phpWord); - $path = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData'; - $this->assertTrue($doc->elementExists($path . '/w:textInput')); - $this->assertTrue($doc->elementExists($path . '/w:checkBox')); - $this->assertTrue($doc->elementExists($path . '/w:ddList')); + $path = '/w:document/w:body/w:p[%d]/w:r/w:fldChar/w:ffData'; + $this->assertTrue($doc->elementExists(sprintf($path, 1) . '/w:textInput')); + $this->assertTrue($doc->elementExists(sprintf($path, 2) . '/w:checkBox')); + $this->assertTrue($doc->elementExists(sprintf($path, 3) . '/w:checkBox')); + $this->assertTrue($doc->elementExists(sprintf($path, 4) . '/w:ddList')); } /** diff --git a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php index 9bad19fe..bc3a2aa8 100644 --- a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\ComplexType\FootnoteProperties; +use PhpOffice\PhpWord\Metadata\DocInfo; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\NumberFormat; @@ -39,6 +40,31 @@ class DocumentTest extends \PHPUnit\Framework\TestCase TestHelperDOCX::clear(); } + /** + * Write custom properties + */ + public function testWriteCustomProps() + { + $phpWord = new PhpWord(); + $docInfo = $phpWord->getDocInfo(); + + $docInfo->setCustomProperty('key1', null); + $docInfo->setCustomProperty('key2', true); + $docInfo->setCustomProperty('key3', 3); + $docInfo->setCustomProperty('key4', 4.4); + $docInfo->setCustomProperty('key5', 'value5'); + $docInfo->setCustomProperty('key6', new \DateTime()); + $docInfo->setCustomProperty('key7', time(), DocInfo::PROPERTY_TYPE_DATE); + + $doc = TestHelperDOCX::getDocument($phpWord); + +// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr')); +// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool')); +// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4')); +// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8')); +// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr')); + } + /** * Write end section page numbering */