add tests, improve code coverage

This commit is contained in:
troosan 2017-11-15 22:58:28 +01:00
parent 8eb72c976a
commit 5ad68e0ba6
13 changed files with 57 additions and 73 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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();
}

View File

@ -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');

View File

@ -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

View File

@ -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');

View File

@ -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++;
}

View File

@ -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);

View File

@ -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', ''));

View File

@ -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'));
}
/**

View File

@ -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
*/