Skip to content

Commit 1e9203a

Browse files
committed
add unit tests
1 parent 603bb9b commit 1e9203a

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

src/PhpWord/Element/SDT.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function setListItems($value)
143143

144144
/**
145145
* Get tag
146-
*
146+
*
147147
* @return string
148148
*/
149149
public function getTag()

src/PhpWord/Writer/Word2007/Element/SDT.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function write()
5050

5151
// Properties
5252
$xmlWriter->startElement('w:sdtPr');
53-
$xmlWriter->writeElementBlock('w:alias', 'w:val', $alias);
54-
$xmlWriter->writeElementBlock('w:tag', 'w:val', $tag);
55-
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
53+
$xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias);
5654
$xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
55+
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
56+
$xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag);
5757
$this->$writeFormField($xmlWriter, $element);
5858
$xmlWriter->endElement(); // w:sdtPr
5959

tests/PhpWord/Element/SDTTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ public function testConstruct()
3232
$types = array('comboBox', 'dropDownList', 'date');
3333
$type = $types[rand(0, 2)];
3434
$value = rand(0, 100);
35+
$alias = 'alias';
36+
$tag = 'my_tag';
3537
$object = new SDT($type);
3638
$object->setValue($value);
3739
$object->setListItems($types);
40+
$object->setAlias($alias);
41+
$object->setTag($tag);
3842

3943
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object);
4044
$this->assertEquals($type, $object->getType());
4145
$this->assertEquals($types, $object->getListItems());
4246
$this->assertEquals($value, $object->getValue());
47+
$this->assertEquals($alias, $object->getAlias());
48+
$this->assertEquals($tag, $object->getTag());
4349
}
4450

4551
/**

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,17 @@ public function testSDTElements()
269269

270270
$section->addSDT('comboBox');
271271
$section->addSDT('dropDownList');
272-
$section->addSDT('date');
272+
$section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
273273

274274
$doc = TestHelperDOCX::getDocument($phpWord);
275275

276-
$path = '/w:document/w:body/w:p/w:sdt/w:sdtPr';
277-
$this->assertTrue($doc->elementExists($path . '/w:comboBox'));
278-
$this->assertTrue($doc->elementExists($path . '/w:dropDownList'));
279-
$this->assertTrue($doc->elementExists($path . '/w:date'));
276+
$path = '/w:document/w:body/w:p';
277+
278+
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
279+
$this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
280+
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
281+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
282+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
283+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
280284
}
281285
}

tests/PhpWord/_includes/XmlDocument.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,22 @@ public function elementExists($path, $file = 'word/document.xml')
162162

163163
return !($nodeList->length == 0);
164164
}
165+
166+
/**
167+
* Returns the xml, or part of it as a formatted string
168+
*
169+
* @param string $path
170+
* @param string $file
171+
* @return string
172+
*/
173+
public function printXml($path = '/w:document', $file = 'word/document.xml')
174+
{
175+
$newdoc = new \DOMDocument();
176+
$newdoc->formatOutput = true;
177+
$newdoc->preserveWhiteSpace = false;
178+
$node = $newdoc->importNode($this->getElement($path, $file), true);
179+
$newdoc->appendChild($node);
180+
181+
return $newdoc->saveXML($node);
182+
}
165183
}

0 commit comments

Comments
 (0)