Skip to content

Commit 3ace770

Browse files
committed
Improve Test Coverage
1 parent f82eb47 commit 3ace770

File tree

3 files changed

+145
-10
lines changed

3 files changed

+145
-10
lines changed

src/PhpWord/Shared/Html.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use PhpOffice\PhpWord\SimpleType\NumberFormat;
3737
use PhpOffice\PhpWord\SimpleType\TextDirection;
3838
use PhpOffice\PhpWord\Style\Paragraph;
39+
use Throwable;
3940

4041
/**
4142
* Common Html functions.
@@ -114,20 +115,20 @@ public static function addHtml($element, $html, $fullHTML = false, $preserveWhit
114115
$dom->preserveWhiteSpace = $preserveWhiteSpace;
115116

116117
try {
117-
$result = $dom->loadHTML($html, LIBXML_NOWARNING | LIBXML_NOERROR);
118+
$result = @$dom->loadHTML($html);
118119
$exceptionMessage = 'DOM loadHTML failed';
119-
} catch (Exception $e) {
120+
} catch (Throwable $e) {
120121
$result = false;
121122
$exceptionMessage = $e->getMessage();
122123
}
123124
if ($result === false) {
124-
throw new Exception($exceptionMessage); // @codeCoverageIgnore
125+
throw new Exception($exceptionMessage);
125126
}
126127
self::removeAnnoyingWhitespaceTextNodes($dom);
127128
static::$xpath = new DOMXPath($dom);
128129
$node = $dom->getElementsByTagName('html');
129130
if (count($node) === 0 || $node->item(0) === null) {
130-
$node = $dom->getElementsByTagName('body');
131+
$node = $dom->getElementsByTagName('body'); // @codeCoverageIgnore
131132
}
132133

133134
static::parseNode($node->item(0), $element);
@@ -1319,7 +1320,6 @@ protected static function mapListType($cssListType)
13191320
return NumberFormat::LOWER_ROMAN; // i, ii, iii, iv, ..
13201321
case 'I':
13211322
return NumberFormat::UPPER_ROMAN; // I, II, III, IV, ..
1322-
case '1':
13231323
default:
13241324
return NumberFormat::DECIMAL; // 1, 2, 3, ..
13251325
}

src/PhpWord/Shared/HtmlColours.php

-5
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,6 @@ class HtmlColours
524524
'yellowgreen' => '9acd32',
525525
];
526526

527-
public static function colourNameLookup(string $colorName): string
528-
{
529-
return self::COLOUR_MAP[$colorName] ?? '';
530-
}
531-
532527
public static function convertColour(string $colorName): string
533528
{
534529
$colorName = trim($colorName);
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Shared;
20+
21+
use Exception;
22+
use PhpOffice\PhpWord\PhpWord;
23+
use PhpOffice\PhpWord\Shared\Html;
24+
use PhpOffice\PhpWordTests\AbstractWebServerEmbedded;
25+
use PhpOffice\PhpWordTests\TestHelperDOCX;
26+
27+
/**
28+
* Test class for PhpOffice\PhpWord\Shared\Html.
29+
*
30+
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
31+
*/
32+
class Html2Test extends AbstractWebServerEmbedded
33+
{
34+
/**
35+
* Tear down after each test.
36+
*/
37+
protected function tearDown(): void
38+
{
39+
TestHelperDOCX::clear();
40+
}
41+
42+
public function testException(): void
43+
{
44+
$this->expectException(Exception::class);
45+
$this->expectExceptionMessage('loadHTML');
46+
$phpWord = new PhpWord();
47+
$section = $phpWord->addSection();
48+
Html::addHtml($section, '');
49+
}
50+
51+
public function testCssOnIdElement(): void
52+
{
53+
$phpWord = new PhpWord();
54+
$section = $phpWord->addSection();
55+
$html = '<html>'
56+
. '<head>'
57+
. '<title>Id Test</title>'
58+
. '<style>#bold1 {font-weight: bold; margin: 10px;}</style>'
59+
. '</head><body>'
60+
. '<p id="bold1">test1.</p>'
61+
. '</body></html>';
62+
Html::addHtml($section, $html);
63+
$doc = TestHelperDOCX::getDocument($phpWord);
64+
$marginPath = '/w:document/w:body/w:p/w:pPr/w:spacing';
65+
self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:before'));
66+
self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:after'));
67+
$path = '/w:document/w:body/w:p/w:r';
68+
self::assertSame('test1.', $doc->getElement($path)->nodeValue);
69+
$boldPath = $path . '/w:rPr/w:b';
70+
self::assertSame('1', $doc->getElement($boldPath)->getAttribute('w:val'));
71+
}
72+
73+
public function testListTypes(): void
74+
{
75+
$phpWord = new PhpWord();
76+
$section = $phpWord->addSection();
77+
$html = '<ol type="1"><li>Decimal number first</li><li>second</li></ol>'
78+
. '<ol type="a"><li>Lowercase first</li><li>second</li></ol>'
79+
. '<ol type="A"><li>Uppercase first</li><li>second</li></ol>'
80+
. '<ol type="i"><li>Lower roman first</li><li>second</li></ol>'
81+
. '<ol type="I"><li>Upper roman first</li><li>second</li></ol>';
82+
Html::addHtml($section, $html);
83+
$doc = TestHelperDOCX::getDocument($phpWord);
84+
85+
$item = 1;
86+
$expected = '1';
87+
$path = "/w:document/w:body/w:p[$item]";
88+
self::assertSame('Decimal number first', $doc->getElement("$path/w:r")->nodeValue);
89+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
90+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
91+
++$item;
92+
$path = "/w:document/w:body/w:p[$item]";
93+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
94+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
95+
96+
++$item;
97+
$expected = '2';
98+
$path = "/w:document/w:body/w:p[$item]";
99+
self::assertSame('Lowercase first', $doc->getElement("$path/w:r")->nodeValue);
100+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
101+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
102+
++$item;
103+
$path = "/w:document/w:body/w:p[$item]";
104+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
105+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
106+
107+
++$item;
108+
$expected = '3';
109+
$path = "/w:document/w:body/w:p[$item]";
110+
self::assertSame('Uppercase first', $doc->getElement("$path/w:r")->nodeValue);
111+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
112+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
113+
++$item;
114+
$path = "/w:document/w:body/w:p[$item]";
115+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
116+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
117+
118+
++$item;
119+
$expected = '4';
120+
$path = "/w:document/w:body/w:p[$item]";
121+
self::assertSame('Lower roman first', $doc->getElement("$path/w:r")->nodeValue);
122+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
123+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
124+
++$item;
125+
$path = "/w:document/w:body/w:p[$item]";
126+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
127+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
128+
129+
++$item;
130+
$expected = '5';
131+
$path = "/w:document/w:body/w:p[$item]";
132+
self::assertSame('Upper roman first', $doc->getElement("$path/w:r")->nodeValue);
133+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
134+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
135+
++$item;
136+
$path = "/w:document/w:body/w:p[$item]";
137+
$numIdPath = $path . '/w:pPr/w:numPr/w:numId';
138+
self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val'));
139+
}
140+
}

0 commit comments

Comments
 (0)