add unit tests

This commit is contained in:
troosan 2017-11-06 21:47:02 +01:00
parent 603bb9b02b
commit 1e9203adc9
5 changed files with 37 additions and 9 deletions

View File

@ -143,7 +143,7 @@ class SDT extends Text
/** /**
* Get tag * Get tag
* *
* @return string * @return string
*/ */
public function getTag() public function getTag()

View File

@ -50,10 +50,10 @@ class SDT extends Text
// Properties // Properties
$xmlWriter->startElement('w:sdtPr'); $xmlWriter->startElement('w:sdtPr');
$xmlWriter->writeElementBlock('w:alias', 'w:val', $alias); $xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias);
$xmlWriter->writeElementBlock('w:tag', 'w:val', $tag);
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
$xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked'); $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
$xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag);
$this->$writeFormField($xmlWriter, $element); $this->$writeFormField($xmlWriter, $element);
$xmlWriter->endElement(); // w:sdtPr $xmlWriter->endElement(); // w:sdtPr

View File

@ -32,14 +32,20 @@ class SDTTest extends \PHPUnit_Framework_TestCase
$types = array('comboBox', 'dropDownList', 'date'); $types = array('comboBox', 'dropDownList', 'date');
$type = $types[rand(0, 2)]; $type = $types[rand(0, 2)];
$value = rand(0, 100); $value = rand(0, 100);
$alias = 'alias';
$tag = 'my_tag';
$object = new SDT($type); $object = new SDT($type);
$object->setValue($value); $object->setValue($value);
$object->setListItems($types); $object->setListItems($types);
$object->setAlias($alias);
$object->setTag($tag);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object);
$this->assertEquals($type, $object->getType()); $this->assertEquals($type, $object->getType());
$this->assertEquals($types, $object->getListItems()); $this->assertEquals($types, $object->getListItems());
$this->assertEquals($value, $object->getValue()); $this->assertEquals($value, $object->getValue());
$this->assertEquals($alias, $object->getAlias());
$this->assertEquals($tag, $object->getTag());
} }
/** /**

View File

@ -269,13 +269,17 @@ class ElementTest extends \PHPUnit_Framework_TestCase
$section->addSDT('comboBox'); $section->addSDT('comboBox');
$section->addSDT('dropDownList'); $section->addSDT('dropDownList');
$section->addSDT('date'); $section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$path = '/w:document/w:body/w:p/w:sdt/w:sdtPr'; $path = '/w:document/w:body/w:p';
$this->assertTrue($doc->elementExists($path . '/w:comboBox'));
$this->assertTrue($doc->elementExists($path . '/w:dropDownList')); $this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
$this->assertTrue($doc->elementExists($path . '/w:date')); $this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
} }
} }

View File

@ -162,4 +162,22 @@ class XmlDocument
return !($nodeList->length == 0); return !($nodeList->length == 0);
} }
/**
* Returns the xml, or part of it as a formatted string
*
* @param string $path
* @param string $file
* @return string
*/
public function printXml($path = '/w:document', $file = 'word/document.xml')
{
$newdoc = new \DOMDocument();
$newdoc->formatOutput = true;
$newdoc->preserveWhiteSpace = false;
$node = $newdoc->importNode($this->getElement($path, $file), true);
$newdoc->appendChild($node);
return $newdoc->saveXML($node);
}
} }