Skip to content

Commit

Permalink
add tests, improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
troosan committed Nov 15, 2017
1 parent 8eb72c9 commit 5ad68e0
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 73 deletions.
2 changes: 2 additions & 0 deletions src/PhpWord/Metadata/DocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ public function setCustomProperty($propertyName, $propertyValue = '', $propertyT
$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;
}
Expand Down
8 changes: 1 addition & 7 deletions src/PhpWord/Writer/Word2007/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Settings;

/**
* CheckBox element writer
*
Expand Down Expand Up @@ -83,11 +81,7 @@ public function write()

$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

Expand Down
7 changes: 1 addition & 6 deletions src/PhpWord/Writer/Word2007/Element/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpWord\Element\FormField as FormFieldElement;
use PhpOffice\PhpWord\Settings;

/**
* FormField element writer
Expand Down Expand Up @@ -90,11 +89,7 @@ public function write()
$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

Expand Down
8 changes: 1 addition & 7 deletions src/PhpWord/Writer/Word2007/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Settings;

/**
* Link element writer
*
Expand Down Expand Up @@ -54,11 +52,7 @@ public function write()

$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
Expand Down
14 changes: 2 additions & 12 deletions src/PhpWord/Writer/Word2007/Element/PreserveText.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Settings;

/**
* PreserveText element writer
*
Expand Down Expand Up @@ -60,11 +58,7 @@ public function write()

$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();

Expand All @@ -86,11 +80,7 @@ public function write()

$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();
}
Expand Down
11 changes: 3 additions & 8 deletions src/PhpWord/Writer/Word2007/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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;
Expand Down Expand Up @@ -97,13 +96,9 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, $title, $
$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');
Expand Down
8 changes: 1 addition & 7 deletions src/PhpWord/Writer/Word2007/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Settings;

/**
* Text element writer
*
Expand All @@ -45,11 +43,7 @@ public function write()

$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

Expand Down
14 changes: 4 additions & 10 deletions src/PhpWord/Writer/Word2007/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

use PhpOffice\PhpWord\Settings;

/**
* TextRun element writer
*
Expand Down Expand Up @@ -60,14 +58,10 @@ public function write()

// 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');
Expand Down
10 changes: 3 additions & 7 deletions src/PhpWord/Writer/Word2007/Part/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,9 @@ private function writeSeriesItem(XMLWriter $xmlWriter, $type, $values)
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++;
}
Expand Down
6 changes: 5 additions & 1 deletion src/PhpWord/Writer/Word2007/Part/DocPropsCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public function write()
$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);
Expand Down
6 changes: 2 additions & 4 deletions tests/PhpWord/Metadata/DocInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ public function testCustomProperty()
$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());
}

Expand All @@ -211,8 +210,7 @@ public function testConvertProperty()
$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', ''));

Expand Down
10 changes: 6 additions & 4 deletions tests/PhpWord/Writer/Word2007/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,16 @@ public function testFormFieldElements()

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

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpWord/Writer/Word2007/Part/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,31 @@ public function tearDown()
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
*/
Expand Down

0 comments on commit 5ad68e0

Please sign in to comment.