Skip to content

Commit 29cfaaf

Browse files
committed
More Coverage
1 parent 5ace17f commit 29cfaaf

File tree

11 files changed

+122
-75
lines changed

11 files changed

+122
-75
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,26 +1530,6 @@ parameters:
15301530
count: 3
15311531
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
15321532

1533-
-
1534-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1535-
count: 1
1536-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1537-
1538-
-
1539-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1540-
count: 1
1541-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1542-
1543-
-
1544-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1545-
count: 2
1546-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1547-
1548-
-
1549-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1550-
count: 1
1551-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1552-
15531533
-
15541534
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
15551535
count: 1

phpstan-baseline.php7.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,26 +1500,6 @@ parameters:
15001500
count: 3
15011501
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
15021502

1503-
-
1504-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1505-
count: 1
1506-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1507-
1508-
-
1509-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1510-
count: 1
1511-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1512-
1513-
-
1514-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1515-
count: 2
1516-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1517-
1518-
-
1519-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1520-
count: 1
1521-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1522-
15231503
-
15241504
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
15251505
count: 1

phpstan-baseline.php73.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,26 +1500,6 @@ parameters:
15001500
count: 3
15011501
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
15021502

1503-
-
1504-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1505-
count: 1
1506-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1507-
1508-
-
1509-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1510-
count: 1
1511-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
1512-
1513-
-
1514-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
1515-
count: 2
1516-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1517-
1518-
-
1519-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
1520-
count: 1
1521-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
1522-
15231503
-
15241504
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
15251505
count: 1

src/PhpWord/Shared/Microsoft/PasswordEncoder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ class PasswordEncoder
9999

100100
private static $passwordMaxLength = 15;
101101

102+
/**
103+
* @param string $password
104+
* @param string $to
105+
* @param string $from
106+
*
107+
* @return false|string
108+
*/
109+
protected static function mbConvertEncoding($password, $to, $from)
110+
{
111+
return mb_convert_encoding($password, $to, $from);
112+
}
113+
102114
/**
103115
* Create a hashed password that MS Word will be able to work with.
104116
*
@@ -120,7 +132,7 @@ public static function hashPassword($password, $algorithmName = self::ALGORITHM_
120132

121133
// Get the single-byte values by iterating through the Unicode characters of the truncated password.
122134
// For each character, if the low byte is not equal to 0, take it. Otherwise, take the high byte.
123-
$passUtf8 = mb_convert_encoding($password, 'UCS-2LE', 'UTF-8');
135+
$passUtf8 = static::mbConvertEncoding($password, 'UCS-2LE', 'UTF-8');
124136
if (!is_string($passUtf8)) {
125137
throw new Exception('Failed to convert password to UCS-2LE');
126138
}

src/PhpWord/Writer/ODText.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,14 @@ public function save(string $filename): void
8383

8484
// Write parts
8585
foreach ($this->parts as $partName => $fileName) {
86-
if ($fileName === '') {
87-
continue;
88-
}
89-
$part = $this->getWriterPart($partName);
90-
if (!$part instanceof AbstractPart) {
91-
continue;
86+
if ($fileName !== '') {
87+
$part = $this->getWriterPart($partName);
88+
if ($part instanceof AbstractPart) {
89+
$part->setObjects($this->objects);
90+
$zip->addFromString($fileName, $part->write());
91+
$this->objects = $part->getObjects();
92+
}
9293
}
93-
94-
$part->setObjects($this->objects);
95-
96-
$zip->addFromString($fileName, $part->write());
97-
98-
$this->objects = $part->getObjects();
9994
}
10095

10196
// Write objects charts
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Microsoft;
20+
21+
/**
22+
* Password encoder for microsoft office applications.
23+
*/
24+
class PasswordEncoder2 extends \PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder
25+
{
26+
/**
27+
* @param string $password
28+
* @param string $to
29+
* @param string $from
30+
*
31+
* @return false|string
32+
*/
33+
protected static function mbConvertEncoding($password, $to, $from)
34+
{
35+
return false;
36+
}
37+
}

tests/PhpWordTests/Shared/Microsoft/PasswordEncoderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace PhpOffice\PhpWordTests\Shared\Microsoft;
2020

21+
use PhpOffice\PhpWord\Exception\Exception as WordException;
2122
use PhpOffice\PhpWord\Shared\Microsoft\PasswordEncoder;
2223

2324
/**
@@ -87,4 +88,16 @@ public function testEncodePasswordWithNullAsciiCodeInPassword(): void
8788
//then
8889
self::assertEquals('rDV9sgdDsztoCQlvRCb1lF2wxNg=', $hashPassword);
8990
}
91+
92+
/**
93+
* Simulate failing mb_convert_encoding.
94+
*/
95+
public function testFailWithNonUtf8Password(): void
96+
{
97+
$this->expectException(WordException::class);
98+
$this->expectExceptionMessage('Failed to convert password to UCS-2LE');
99+
$password = 'hello';
100+
$salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
101+
PasswordEncoder2::hashPassword($password, PasswordEncoder::ALGORITHM_MAC, $salt, 1);
102+
}
90103
}

tests/PhpWordTests/Style/LanguageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ public function testShortLanguage(): void
8989
//then
9090
Assert::assertEquals('fr-FR', $language->getLatin());
9191
}
92+
93+
public function testUndefined(): void
94+
{
95+
//when
96+
$language = new Language();
97+
$language->setLatin('und');
98+
99+
//then
100+
Assert::assertEquals('en-EN', $language->getLatin());
101+
}
92102
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpWordTests\Writer\EPub3\Part;
4+
5+
use PhpOffice\PhpWord\Exception\Exception as WordException;
6+
use PhpOffice\PhpWord\Writer\EPub3\Part\ContentXhtml;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ContentXhtmlTest extends TestCase
10+
{
11+
public function testExceptionIfNoParentDoc(): void
12+
{
13+
$this->expectException(WordException::class);
14+
$this->expectExceptionMessage('No PhpWord assigned.');
15+
$object = new ContentXhtml();
16+
$object->write();
17+
}
18+
}

tests/PhpWordTests/Writer/PDF/MPDFTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testConstruct(): void
4848
$section->addText('Section 2 - landscape');
4949

5050
$writer = new MPDF($phpWord);
51+
$writer->setFont('xyz');
5152
$writer->save($file);
5253

5354
self::assertFileExists($file);
@@ -106,11 +107,12 @@ public function testSetGetAbstractRendererOptions(): void
106107
{
107108
$rendererName = Settings::PDF_RENDERER_MPDF;
108109
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf');
110+
self::assertNotFalse($rendererLibraryPath);
109111
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
110112
Settings::setPdfRendererOptions([
111113
'font' => 'Arial',
112114
]);
113115
$writer = new PDF(new PhpWord());
114-
self::assertEquals('Arial', $writer->getFont());
116+
self::assertEquals('Arial', $writer->getFont()); //* @phpstan-ignore-line
115117
}
116118
}

0 commit comments

Comments
 (0)