Skip to content

Commit 9839222

Browse files
committed
QA: Additional unit tests
1 parent 269def4 commit 9839222

File tree

22 files changed

+254
-44
lines changed

22 files changed

+254
-44
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,29 @@ protected function addElement($elementName)
6060
if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) {
6161
$args[3] = null; // Remove paragraph style for texts in textrun
6262
}
63+
$source = '';
64+
if (count($args) > 1) {
65+
$source = $args[1];
66+
}
6367

6468
// Create element using reflection
6569
$reflection = new \ReflectionClass($elementClass);
6670
$elementArgs = $args;
67-
array_shift($elementArgs); // Shift an element off the beginning of array: the $elementName
71+
array_shift($elementArgs); // Shift the $elementName off the beginning of array
6872

6973
/** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
7074
$element = $reflection->newInstanceArgs($elementArgs);
7175

72-
// Set nested level
73-
if ($this->container == 'Cell') {
74-
$element->setNestedLevel($this->getNestedLevel() + 1);
75-
} else {
76-
$element->setNestedLevel($this->getNestedLevel());
77-
}
78-
79-
80-
// Set relation Id for media collection
81-
$mediaContainer = $this->getMediaContainer();
82-
if (in_array($elementName, array('Link', 'Image', 'Object'))) {
83-
/** @var \PhpOffice\PhpWord\Element\Image $element Type hint */
84-
$image = ($elementName == 'Image') ? $element : null;
85-
$rId = Media::addElement($mediaContainer, strtolower($elementName), $args[1], $image);
86-
$element->setRelationId($rId);
87-
}
88-
if ($elementName == 'Object') {
89-
/** @var \PhpOffice\PhpWord\Element\Object $element Type hint */
90-
$rIdIcon = Media::addElement($mediaContainer, 'image', $element->getIcon(), new Image($element->getIcon()));
91-
$element->setImageRelationId($rIdIcon);
92-
}
93-
94-
// Set relation Id for other collection
95-
if (in_array($elementName, array('Footnote', 'Endnote', 'Title')) && $this->phpWord instanceof PhpWord) {
96-
$addMethod = "add{$elementName}";
97-
$rId = $this->phpWord->$addMethod($element);
98-
$element->setRelationId($rId);
99-
}
76+
// Set nested level and relation Id
77+
$this->setElementNestedLevel($element);
78+
$this->setElementRelationId($element, $elementName, $source);
10079

10180
// Set other properties and add element into collection
10281
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
10382
$element->setElementIndex($this->countElements() + 1);
10483
$element->setElementId();
10584
$element->setPhpWord($this->phpWord);
85+
10686
$this->elements[] = $element;
10787

10888
return $element;
@@ -128,6 +108,54 @@ public function countElements()
128108
return count($this->elements);
129109
}
130110

111+
/**
112+
* Set element nested level based on container; add one when it's inside a cell
113+
*/
114+
private function setElementNestedLevel(AbstractElement $element)
115+
{
116+
if ($this->container == 'Cell') {
117+
$element->setNestedLevel($this->getNestedLevel() + 1);
118+
} else {
119+
$element->setNestedLevel($this->getNestedLevel());
120+
}
121+
}
122+
123+
/**
124+
* Set relation Id
125+
*
126+
* @param string $elementName
127+
* @param string $source
128+
*/
129+
private function setElementRelationId(AbstractElement $element, $elementName, $source)
130+
{
131+
$mediaContainer = $this->getMediaContainer();
132+
$hasMediaRelation = in_array($elementName, array('Link', 'Image', 'Object'));
133+
$hasOtherRelation = in_array($elementName, array('Footnote', 'Endnote', 'Title'));
134+
135+
// Set relation Id for media elements (link, image, object; legacy of OOXML)
136+
// Only Image that needs to be passed to Media class
137+
if ($hasMediaRelation) {
138+
/** @var \PhpOffice\PhpWord\Element\Image $element Type hint */
139+
$image = ($elementName == 'Image') ? $element : null;
140+
$rId = Media::addElement($mediaContainer, strtolower($elementName), $source, $image);
141+
$element->setRelationId($rId);
142+
}
143+
144+
// Set relation Id for icon of object element
145+
if ($elementName == 'Object') {
146+
/** @var \PhpOffice\PhpWord\Element\Object $element Type hint */
147+
$rIdIcon = Media::addElement($mediaContainer, 'image', $element->getIcon(), new Image($element->getIcon()));
148+
$element->setImageRelationId($rIdIcon);
149+
}
150+
151+
// Set relation Id for elements that will be registered in the Collection subnamespaces
152+
if ($hasOtherRelation && $this->phpWord instanceof PhpWord) {
153+
$addMethod = "add{$elementName}";
154+
$rId = $this->phpWord->$addMethod($element);
155+
$element->setRelationId($rId);
156+
}
157+
}
158+
131159
/**
132160
* Add text/preservetext element
133161
*

src/PhpWord/Reader/ODText/AbstractPart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Abstract part reader
2424
*
2525
* @since 0.10.0
26-
* @codeCoverageIgnore Nothing in here yet
26+
* @codeCoverageIgnore
2727
*/
2828
abstract class AbstractPart extends Word2007AbstractPart
2929
{

src/PhpWord/Shared/XMLWriter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder =
6969
$this->tempFile = @tempnam($tempFolder, 'xml');
7070

7171
// Fallback to memory when temporary file cannot be used
72+
// @codeCoverageIgnoreStart
73+
// Can't find any test case. Uncomment when found.
7274
if ($this->xmlWriter->openUri($this->tempFile) === false) {
7375
$this->xmlWriter->openMemory();
7476
}
77+
// @codeCoverageIgnoreEnd
7578
}
7679

7780
// Set xml Compatibility

src/PhpWord/Shared/ZipArchive.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public function open($filename, $flags = null)
152152
*
153153
* @return bool
154154
* @throws \PhpOffice\PhpWord\Exception\Exception
155+
* @codeCoverageIgnore Can't find any test case. Uncomment when found.
155156
*/
156157
public function close()
157158
{

src/PhpWord/Writer/AbstractWriter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ protected function getTempFile($filename)
219219
$this->originalFilename = $filename;
220220
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
221221
$filename = @tempnam(sys_get_temp_dir(), 'phpword_');
222+
// @codeCoverageIgnoreStart
223+
// Can't find any test case. Uncomment when found.
222224
if ($filename == '') {
223225
$filename = $this->originalFilename;
224226
}
227+
// @codeCoverageIgnoreEnd
225228
}
226229
$this->tempFilename = $filename;
227230

@@ -234,9 +237,12 @@ protected function getTempFile($filename)
234237
protected function cleanupTempFile()
235238
{
236239
if ($this->originalFilename != $this->tempFilename) {
240+
// @codeCoverageIgnoreStart
241+
// Can't find any test case. Uncomment when found.
237242
if (copy($this->tempFilename, $this->originalFilename) === false) {
238243
throw new Exception("Could not copy temporary zip file.");
239244
}
245+
// @codeCoverageIgnoreEnd
240246
@unlink($this->tempFilename);
241247
}
242248

@@ -269,11 +275,15 @@ protected function getZipArchive($filename)
269275

270276
// Try opening the ZIP file
271277
$zip = new ZipArchive();
278+
279+
// @codeCoverageIgnoreStart
280+
// Can't find any test case. Uncomment when found.
272281
if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {
273282
if ($zip->open($filename, ZipArchive::CREATE) !== true) {
274283
throw new \Exception("Could not open '{$filename}' for writing.");
275284
}
276285
}
286+
// @codeCoverageIgnoreEnd
277287

278288
return $zip;
279289
}
@@ -290,9 +300,12 @@ protected function openFile($filename)
290300
{
291301
$filename = $this->getTempFile($filename);
292302
$fileHandle = fopen($filename, 'w');
303+
// @codeCoverageIgnoreStart
304+
// Can't find any test case. Uncomment when found.
293305
if ($fileHandle === false) {
294306
throw new \Exception("Could not open '{$filename}' for writing.");
295307
}
308+
// @codeCoverageIgnoreEnd
296309

297310
return $fileHandle;
298311
}

src/PhpWord/Writer/HTML/Element/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function write()
7070

7171
$content = '';
7272
$content .= $this->writeOpening();
73+
$content .= $this->openingText;
7374
$content .= $this->openingTags;
7475
$content .= htmlspecialchars($element->getText());
7576
$content .= $this->closingTags;
@@ -113,7 +114,6 @@ protected function writeOpening()
113114
$style = $this->getParagraphStyle();
114115
}
115116
$content .= "<p{$style}>";
116-
$content .= $this->openingText;
117117
}
118118

119119
return $content;

src/PhpWord/Writer/HTML/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function write()
4949

5050
$css['font-family'] = $this->getValueIf($font !== null, "'{$font}'");
5151
$css['font-size'] = $this->getValueIf($size !== null, "{$size}pt");
52-
$css['color'] = $this->getValueIf($color != Settings::DEFAULT_FONT_COLOR, "#{$color}");
52+
$css['color'] = $this->getValueIf($color !== null, "#{$color}");
5353
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
5454
$css['font-weight'] = $this->getValueIf($style->isBold(), 'bold');
5555
$css['font-style'] = $this->getValueIf($style->isItalic(), 'italic');

src/PhpWord/Writer/ODText/Part/Meta.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,16 @@ public function write()
8989
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
9090
* @param string $property
9191
* @param string $value
92-
* @param string $type string (default/null)
9392
*
94-
* @todo Handle other `$type`: double|date|dateTime|duration|boolean
93+
* @todo Handle other `$type`: double|date|dateTime|duration|boolean (4th arguments)
9594
*/
96-
private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value, $type = null)
95+
private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value)
9796
{
9897
$xmlWriter->startElement('meta:user-defined');
9998
$xmlWriter->writeAttribute('meta:name', $property);
100-
if ($type !== null) {
101-
$xmlWriter->writeAttribute('meta:value-type', $type);
102-
}
99+
// if ($type !== null) {
100+
// $xmlWriter->writeAttribute('meta:value-type', $type);
101+
// }
103102
$xmlWriter->writeRaw($value);
104103
$xmlWriter->endElement(); // meta:user-defined
105104
}

src/PhpWord/Writer/PDF/AbstractRenderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ public function __construct(PhpWord $phpWord)
8787
/** @noinspection PhpIncludeInspection Dynamic includes */
8888
require_once $includeFile;
8989
} else {
90+
// @codeCoverageIgnoreStart
91+
// Can't find any test case. Uncomment when found.
9092
throw new Exception('Unable to load PDF Rendering library');
93+
// @codeCoverageIgnoreEnd
9194
}
9295
}
9396

@@ -172,9 +175,12 @@ public function setOrientation($value = 'default')
172175
protected function prepareForSave($filename = null)
173176
{
174177
$fileHandle = fopen($filename, 'w');
178+
// @codeCoverageIgnoreStart
179+
// Can't find any test case. Uncomment when found.
175180
if ($fileHandle === false) {
176181
throw new Exception("Could not open file $filename for writing.");
177182
}
183+
// @codeCoverageIgnoreEnd
178184
$this->isPdf = true;
179185

180186
return $fileHandle;

tests/PhpWord/Tests/Element/FieldTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testConstructNull()
4343
public function testConstructWithType()
4444
{
4545
$oField = new Field('DATE');
46-
46+
4747
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
4848
$this->assertEquals($oField->getType(), 'DATE');
4949
}
@@ -54,7 +54,7 @@ public function testConstructWithType()
5454
public function testConstructWithTypeProperties()
5555
{
5656
$oField = new Field('DATE', array('dateformat'=>'d-M-yyyy'));
57-
57+
5858
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
5959
$this->assertEquals($oField->getType(), 'DATE');
6060
$this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy'));
@@ -66,10 +66,46 @@ public function testConstructWithTypeProperties()
6666
public function testConstructWithTypePropertiesOptions()
6767
{
6868
$oField = new Field('DATE', array('dateformat'=>'d-M-yyyy'), array('SakaEraCalendar', 'PreserveFormat'));
69-
69+
7070
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
7171
$this->assertEquals($oField->getType(), 'DATE');
7272
$this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy'));
7373
$this->assertEquals($oField->getOptions(), array('SakaEraCalendar', 'PreserveFormat'));
7474
}
75+
76+
/**
77+
* Test setType exception
78+
*
79+
* @expectedException \InvalidArgumentException
80+
* @expectedExceptionMessage Invalid type
81+
*/
82+
public function testSetTypeException()
83+
{
84+
$object = new Field();
85+
$object->setType('foo');
86+
}
87+
88+
/**
89+
* Test setProperties exception
90+
*
91+
* @expectedException \InvalidArgumentException
92+
* @expectedExceptionMessage Invalid property
93+
*/
94+
public function testSetPropertiesException()
95+
{
96+
$object = new Field('PAGE');
97+
$object->setProperties(array('foo' => 'bar'));
98+
}
99+
100+
/**
101+
* Test setOptions exception
102+
*
103+
* @expectedException \InvalidArgumentException
104+
* @expectedExceptionMessage Invalid option
105+
*/
106+
public function testSetOptionsException()
107+
{
108+
$object = new Field('PAGE');
109+
$object->setOptions(array('foo' => 'bar'));
110+
}
75111
}

tests/PhpWord/Tests/Shared/XMLReaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
*/
2828
class XMLReaderTest extends \PHPUnit_Framework_TestCase
2929
{
30+
/**
31+
* Test get DOMDocument from ZipArchive exception
32+
*
33+
* @expectedException \PhpOffice\PhpWord\Exception\Exception
34+
* @expectedExceptionMessage Cannot find archive file.
35+
*/
36+
public function testGetDomFromZipException()
37+
{
38+
$filename = __DIR__ . "/../_files/documents/foo.zip";
39+
$object = new XMLReader();
40+
$object->getDomFromZip($filename, 'yadayadaya');
41+
}
42+
3043
/**
3144
* Test get DOMDocument from ZipArchive returns false
3245
*/

tests/PhpWord/Tests/Style/NumberingLevelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testSetGetNormal()
3838
'start' => 1,
3939
'format' => 'decimal',
4040
'restart' => 1,
41+
'pStyle' => 'pStyle',
4142
'suffix' => 'space',
4243
'text' => '%1.',
4344
'align' => 'left',

tests/PhpWord/Tests/Style/ParagraphTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function testSetStyleValueNormal()
7575
'spacing' => 120,
7676
'basedOn' => 'Normal',
7777
'next' => 'Normal',
78+
'numStyle' => 'numStyle',
79+
'numLevel' => 1,
7880
'widowControl' => false,
7981
'keepNext' => true,
8082
'keepLines' => true,

0 commit comments

Comments
 (0)