Skip to content

Commit acb2e37

Browse files
committed
Coverage Tweaks
1 parent c775a2e commit acb2e37

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

src/PhpWord/Style/ListItem.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,8 @@ public function getNumStyle()
123123
public function getNumbering()
124124
{
125125
$numStyleObject = Style::getStyle($this->numStyle);
126-
if ($numStyleObject instanceof Numbering) {
127-
return $numStyleObject;
128-
}
129126

130-
return null;
127+
return ($numStyleObject instanceof Numbering) ? $numStyleObject : null;
131128
}
132129

133130
/**

src/PhpWord/Writer/RTF/Element/ListItem.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\RTF\Element;
2020

21+
use PhpOffice\PhpWord\Element\ListItem as Li;
22+
2123
/**
2224
* ListItem element RTF writer; extends from text.
2325
*
@@ -30,12 +32,16 @@ class ListItem extends Text
3032
*/
3133
public function write()
3234
{
33-
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
3435
$element = $this->element;
35-
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItem) {
36-
return '';
37-
}
3836

37+
return ($element instanceof Li) ? $this->writeElement($element) : '';
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
private function writeElement(Li $element)
44+
{
3945
$this->getStyles();
4046

4147
$depth = (int) $element->getDepth();
@@ -58,7 +64,7 @@ public function write()
5864
$content .= '\li' . $levels[$depth]->getLeft();
5965
$content .= '\lin' . $levels[$depth]->getLeft();
6066
}
61-
$content .= $this->writeFontStyle(); // Doesn't work. Don't know why. Probalby something to do with \PphOffice\PhpWord\Element\ListItem storing styles in a textObject type \PphOffice\PhpWord\Element\Text rather than within the Element itself
67+
$content .= $this->writeFontStyle(); // Doesn't work. Don't know why. Probably something to do with \PhpOffice\PhpWord\Element\ListItem storing styles in a textObject type \PhpOffice\PhpWord\Element\Text rather than within the Element itself
6268
$content .= PHP_EOL;
6369
/* $content .= '{\listtext\f2 \\\'b7\tab }'; // Not sure if needed for listItemRun
6470
$content .= PHP_EOL; */

src/PhpWord/Writer/RTF/Element/ListItemRun.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\RTF\Element;
2020

21+
use PhpOffice\PhpWord\Element\ListItemRun as Lir;
22+
2123
/**
2224
* ListItem element HTML writer.
2325
*
@@ -33,10 +35,15 @@ class ListItemRun extends TextRun
3335
public function write()
3436
{
3537
$element = $this->element;
36-
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
37-
return '';
38-
}
3938

39+
return ($element instanceof Lir) ? $this->writeElement($element) : '';
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
private function writeElement(Lir $element)
46+
{
4047
$writer = new Container($this->parentWriter, $element);
4148
$this->getStyles();
4249

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public function write()
138138

139139
// noProof
140140
// This is also specified above as \\langnp{$langId}
141-
// Not sure why, or if, both are needed.
141+
// RTF spec suggests using this for backwards compatibility.
142+
// So perhaps earlier can be omitted, but it seems harmless regardless.
142143
$content .= $this->getValueIf($style->isNoProof(), '\noproof\lang1024');
143144

144145
// Background-Color

tests/PhpWordTests/Writer/RTF/Element/ListItemTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,45 @@ public function testListItemBasic(): void
5050
$expected = implode("\n", $expectedArray);
5151
self::assertStringContainsString($expected, $content);
5252
}
53+
54+
public function testListItemSingleLevel(): void
55+
{
56+
$phpWord = new PhpWord();
57+
$singlelevelNumberingStyleName = 'singleLevel';
58+
$phpWord->addNumberingStyle(
59+
$singlelevelNumberingStyleName,
60+
[
61+
'type' => 'singleLevel',
62+
'levels' => [
63+
['format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360, 'restart' => 1],
64+
],
65+
]
66+
);
67+
$section = $phpWord->addSection();
68+
$section->addText('SingleLevel formatted list.');
69+
$section->addListItem('List Item 1', 0, null, $singlelevelNumberingStyleName);
70+
$section->addListItem('List Item 2', 0, null, $singlelevelNumberingStyleName);
71+
$section->addListItem('List Item 3', 0, null, $singlelevelNumberingStyleName);
72+
$writer = new RtfWriter($phpWord);
73+
$content = $writer->getContent();
74+
$content = str_replace("\r\n", "\n", $content);
75+
$expectedArray = [
76+
'\pard\nowidctlpar {SingleLevel formatted list.}\par',
77+
'\ilvl0\ls1\tx360\fi-360\li360\lin360',
78+
'{List Item 1}',
79+
'\par',
80+
'\ilvl0\ls1\tx360\fi-360\li360\lin360',
81+
'{List Item 2}',
82+
'\par',
83+
'\ilvl0\ls1\tx360\fi-360\li360\lin360',
84+
'{List Item 3}',
85+
'\par',
86+
];
87+
$expected = implode("\n", $expectedArray);
88+
self::assertStringContainsString($expected, $content);
89+
self::assertStringContainsString('\levelnorestart1', $content);
90+
$table = $writer->getListTable();
91+
self::assertCount(1, $table);
92+
self::assertSame('singleLevel', $table[0]['type']);
93+
}
5394
}

0 commit comments

Comments
 (0)