From 9ba9aa276d2a9466c249e8b1c8c66bce53bc55cc Mon Sep 17 00:00:00 2001 From: ling Date: Mon, 2 Sep 2024 10:14:49 +0800 Subject: [PATCH 1/5] Add support for the firstLineChars style in Word2007. --- src/PhpWord/Style/Indentation.php | 30 +++++++++++++++++++ .../Writer/Word2007/Style/Indentation.php | 3 ++ 2 files changed, 33 insertions(+) diff --git a/src/PhpWord/Style/Indentation.php b/src/PhpWord/Style/Indentation.php index c2032dd22f..8bf9a668a3 100644 --- a/src/PhpWord/Style/Indentation.php +++ b/src/PhpWord/Style/Indentation.php @@ -47,6 +47,13 @@ class Indentation extends AbstractStyle */ private $firstLine = 0; + /** + * Additional first line chars indentation (twip). + * + * @var float|int + */ + private $firstLineChars = 0; + /** * Indentation removed from first line (twip). * @@ -118,6 +125,29 @@ public function setFirstLine(?float $value): self return $this; } + /** + * Get first line chars. + * + * @return float|int + */ + public function getFirstLineChars() + { + return $this->firstLineChars; + } + + /** + * Set first line chars. + * + * @param float|int $value + * + * @return $this + */ + public function setFirstLineChars($value) + { + $this->firstLineChars = $this->setNumericVal($value, $this->firstLineChars); + return $this; + } + /** * Get hanging. */ diff --git a/src/PhpWord/Writer/Word2007/Style/Indentation.php b/src/PhpWord/Writer/Word2007/Style/Indentation.php index 85880d4c22..7a4a2ea51c 100644 --- a/src/PhpWord/Writer/Word2007/Style/Indentation.php +++ b/src/PhpWord/Writer/Word2007/Style/Indentation.php @@ -44,6 +44,9 @@ public function write(): void $firstLine = $style->getFirstLine(); $xmlWriter->writeAttributeIf(null !== $firstLine, 'w:firstLine', $this->convertTwip($firstLine)); + $firstLineChars = $style->getFirstLineChars(); + $xmlWriter->writeAttributeIf(null !== $firstLineChars, 'w:firstLineChars', $this->convertTwip($firstLineChars)); + $hanging = $style->getHanging(); $xmlWriter->writeAttributeIf(null !== $hanging, 'w:hanging', $this->convertTwip($hanging)); From 0b45103052b280a0752bff106c0f08299a8b758a Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 20 Feb 2025 12:23:12 +0100 Subject: [PATCH 2/5] Writer Word2007: Added support for the firstLineChars for Indentation --- docs/changes/1.x/1.4.0.md | 3 +- docs/usage/styles/paragraph.md | 2 +- src/PhpWord/Reader/Word2007/AbstractPart.php | 1 + src/PhpWord/Style/Indentation.php | 12 +--- src/PhpWord/Style/Paragraph.php | 8 +++ .../Writer/Word2007/Style/Indentation.php | 2 +- .../Reader/Word2007/StyleTest.php | 57 ++++++++++++++-- tests/PhpWordTests/Style/IndentationTest.php | 1 + .../Writer/Word2007/Style/IndentationTest.php | 67 +++++++++++++++++++ 9 files changed, 134 insertions(+), 19 deletions(-) create mode 100644 tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 6fdef4e74f..0cf9d88add 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -17,7 +17,8 @@ - Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727) - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) -- Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) +- Writer Word2007: Added support for background and border color transparency in Text Box element by [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) +- Writer Word2007: Added support for the firstLineChars for Indentation by [@liuzhilinux](https://github.com/liuzhilinux) & [@Progi1984](https://github.com/Progi1984) in [#2753](https://github.com/PHPOffice/PHPWord/pull/2753) ### Bug fixes diff --git a/docs/usage/styles/paragraph.md b/docs/usage/styles/paragraph.md index e9ca155005..f51d5ba030 100644 --- a/docs/usage/styles/paragraph.md +++ b/docs/usage/styles/paragraph.md @@ -7,7 +7,7 @@ Available Paragraph style options: - ``basedOn``. Parent style. - ``hanging``. Hanging indentation in *half inches*. - ``indent``. Indent (left indentation) in *half inches*. -- ``indentation``. An array of indentation key => value pairs in *twip*. Supports *left*, *right*, *firstLine* and *hanging* indentation. +- ``indentation``. An array of indentation key => value pairs in *twip*. Supports *left*, *right*, *firstLine*, *firstLineChars* and *hanging* indentation. See ``\PhpOffice\PhpWord\Style\Indentation`` for possible identation types. - ``keepLines``. Keep all lines on one page, *true* or *false*. - ``keepNext``. Keep paragraph with next paragraph, *true* or *false*. diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 96d4a46f8a..9d49573d69 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -708,6 +708,7 @@ protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode) 'indentRight' => [self::READ_VALUE, 'w:ind', 'w:right'], 'indentHanging' => [self::READ_VALUE, 'w:ind', 'w:hanging'], 'indentFirstLine' => [self::READ_VALUE, 'w:ind', 'w:firstLine'], + 'indentFirstLineChars' => [self::READ_VALUE, 'w:ind', 'w:firstLineChars'], 'spaceAfter' => [self::READ_VALUE, 'w:spacing', 'w:after'], 'spaceBefore' => [self::READ_VALUE, 'w:spacing', 'w:before'], 'widowControl' => [self::READ_FALSE, 'w:widowControl'], diff --git a/src/PhpWord/Style/Indentation.php b/src/PhpWord/Style/Indentation.php index 8bf9a668a3..aed1f67b9f 100644 --- a/src/PhpWord/Style/Indentation.php +++ b/src/PhpWord/Style/Indentation.php @@ -50,7 +50,7 @@ class Indentation extends AbstractStyle /** * Additional first line chars indentation (twip). * - * @var float|int + * @var int */ private $firstLineChars = 0; @@ -127,22 +127,16 @@ public function setFirstLine(?float $value): self /** * Get first line chars. - * - * @return float|int */ - public function getFirstLineChars() + public function getFirstLineChars(): int { return $this->firstLineChars; } /** * Set first line chars. - * - * @param float|int $value - * - * @return $this */ - public function setFirstLineChars($value) + public function setFirstLineChars(int $value): self { $this->firstLineChars = $this->setNumericVal($value, $this->firstLineChars); return $this; diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index 5dda985fe5..5ab7ade673 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -432,6 +432,14 @@ public function setIndentFirstLine(?float $value = null): self return $this->setIndentation(['firstLine' => $value]); } + /** + * Set firstlineChars indentation. + */ + public function setIndentFirstLineChars(int $value = 0): self + { + return $this->setIndentation(['firstLineChars' => $value]); + } + /** * Set left indentation. */ diff --git a/src/PhpWord/Writer/Word2007/Style/Indentation.php b/src/PhpWord/Writer/Word2007/Style/Indentation.php index 7a4a2ea51c..3588683cd9 100644 --- a/src/PhpWord/Writer/Word2007/Style/Indentation.php +++ b/src/PhpWord/Writer/Word2007/Style/Indentation.php @@ -45,7 +45,7 @@ public function write(): void $xmlWriter->writeAttributeIf(null !== $firstLine, 'w:firstLine', $this->convertTwip($firstLine)); $firstLineChars = $style->getFirstLineChars(); - $xmlWriter->writeAttributeIf(null !== $firstLineChars, 'w:firstLineChars', $this->convertTwip($firstLineChars)); + $xmlWriter->writeAttributeIf(0 !== $firstLineChars, 'w:firstLineChars', $this->convertTwip($firstLineChars)); $hanging = $style->getHanging(); $xmlWriter->writeAttributeIf(null !== $hanging, 'w:hanging', $this->convertTwip($hanging)); diff --git a/tests/PhpWordTests/Reader/Word2007/StyleTest.php b/tests/PhpWordTests/Reader/Word2007/StyleTest.php index 347c0350f9..91f86eb40e 100644 --- a/tests/PhpWordTests/Reader/Word2007/StyleTest.php +++ b/tests/PhpWordTests/Reader/Word2007/StyleTest.php @@ -334,8 +334,14 @@ public function testPageVerticalAlign(): void /** * @dataProvider providerIndentation */ - public function testIndentation(string $indent, float $left, float $right, ?float $hanging, float $firstLine): void - { + public function testIndentation( + string $indent, + float $left, + float $right, + ?float $hanging, + float $firstLine, + int $firstLineChars + ): void { $documentXml = " $indent @@ -359,16 +365,53 @@ public function testIndentation(string $indent, float $left, float $right, ?floa self::assertSame($right, $indentation->getRight()); self::assertSame($hanging, $indentation->getHanging()); self::assertSame($firstLine, $indentation->getFirstLine()); + self::assertSame($firstLineChars, $indentation->getFirstLineChars()); } /** - * @return Generator + * @return Generator */ public static function providerIndentation() { - yield ['', 709.00, 488.00, 10.0, 490.00]; - yield ['', 0, 0, 10.0, 490.00]; - yield ['', 709.00, 0, 0, 0]; - yield ['', 0, 488.00, 0, 0]; + yield [ + '', + 709.00, + 488.00, + 10.0, + 490.00, + 140 + ]; + yield [ + '', + 709.00, + 488.00, + 10.0, + 490.00, + 0 + ]; + yield [ + '', + 0, + 0, + 10.0, + 490.00, + 0 + ]; + yield [ + '', + 709.00, + 0, + 0, + 0, + 0 + ]; + yield [ + '', + 0, + 488.00, + 0, + 0, + 0 + ]; } } diff --git a/tests/PhpWordTests/Style/IndentationTest.php b/tests/PhpWordTests/Style/IndentationTest.php index db13407ff3..76e3c74cb7 100644 --- a/tests/PhpWordTests/Style/IndentationTest.php +++ b/tests/PhpWordTests/Style/IndentationTest.php @@ -37,6 +37,7 @@ public function testGetSetProperties(): void 'left' => [0, 10], 'right' => [0, 10], 'firstLine' => [null, 20], + 'firstLineChars' => [0, 20], 'hanging' => [null, 20], ]; foreach ($properties as $property => $value) { diff --git a/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php b/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php new file mode 100644 index 0000000000..2a11e4d65d --- /dev/null +++ b/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php @@ -0,0 +1,67 @@ +addSection(); + $text = $section->addText('AA'); + $text->getParagraphStyle()->setIndentation([]); + $doc = TestHelperDOCX::getDocument($word, 'Word2007'); + + $path = '/w:document/w:body/w:p[1]/w:pPr/w:ind'; + self::assertTrue($doc->elementExists($path)); + self::assertFalse($doc->hasElementAttribute($path, 'w:firstLineChars')); + } + + public function testFirstLineChars(): void + { + $word = new PhpWord(); + Settings::setDefaultRtl(true); + $section = $word->addSection(); + $text = $section->addText('AA'); + $text->getParagraphStyle()->setIndentation([ + 'firstLineChars' => 1440, + ]); + $doc = TestHelperDOCX::getDocument($word, 'Word2007'); + + $path = '/w:document/w:body/w:p[1]/w:pPr/w:ind'; + self::assertTrue($doc->elementExists($path)); + self::assertTrue($doc->hasElementAttribute($path, 'w:firstLineChars')); + self::assertSame('1440', $doc->getElementAttribute($path, 'w:firstLineChars')); + } +} From 779aaffcdaf7beed6733af1f43787dcce8c3d17a Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 20 Feb 2025 12:23:36 +0100 Subject: [PATCH 3/5] Fixed Changelog --- docs/changes/1.x/1.4.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 0cf9d88add..711acf37e6 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -18,7 +18,7 @@ - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) - Writer Word2007: Added support for background and border color transparency in Text Box element by [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555) -- Writer Word2007: Added support for the firstLineChars for Indentation by [@liuzhilinux](https://github.com/liuzhilinux) & [@Progi1984](https://github.com/Progi1984) in [#2753](https://github.com/PHPOffice/PHPWord/pull/2753) +- Writer/Reader Word2007: Added support for the firstLineChars for Indentation by [@liuzhilinux](https://github.com/liuzhilinux) & [@Progi1984](https://github.com/Progi1984) in [#2753](https://github.com/PHPOffice/PHPWord/pull/2753) ### Bug fixes From a58a8088f9072fa940f883b40d86c9af8ee9bb96 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 20 Feb 2025 12:31:16 +0100 Subject: [PATCH 4/5] Fixed PHPStan & PHPCSFixer --- src/PhpWord/Style/Indentation.php | 3 ++- tests/PhpWordTests/Reader/Word2007/StyleTest.php | 10 +++++----- .../Writer/Word2007/Style/IndentationTest.php | 10 +++++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/Style/Indentation.php b/src/PhpWord/Style/Indentation.php index aed1f67b9f..f2b646f7b6 100644 --- a/src/PhpWord/Style/Indentation.php +++ b/src/PhpWord/Style/Indentation.php @@ -138,7 +138,8 @@ public function getFirstLineChars(): int */ public function setFirstLineChars(int $value): self { - $this->firstLineChars = $this->setNumericVal($value, $this->firstLineChars); + $this->firstLineChars = $this->setIntVal($value, $this->firstLineChars); + return $this; } diff --git a/tests/PhpWordTests/Reader/Word2007/StyleTest.php b/tests/PhpWordTests/Reader/Word2007/StyleTest.php index 91f86eb40e..7f4afde476 100644 --- a/tests/PhpWordTests/Reader/Word2007/StyleTest.php +++ b/tests/PhpWordTests/Reader/Word2007/StyleTest.php @@ -379,7 +379,7 @@ public static function providerIndentation() 488.00, 10.0, 490.00, - 140 + 140, ]; yield [ '', @@ -387,7 +387,7 @@ public static function providerIndentation() 488.00, 10.0, 490.00, - 0 + 0, ]; yield [ '', @@ -395,7 +395,7 @@ public static function providerIndentation() 0, 10.0, 490.00, - 0 + 0, ]; yield [ '', @@ -403,7 +403,7 @@ public static function providerIndentation() 0, 0, 0, - 0 + 0, ]; yield [ '', @@ -411,7 +411,7 @@ public static function providerIndentation() 488.00, 0, 0, - 0 + 0, ]; } } diff --git a/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php b/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php index 2a11e4d65d..44d071415a 100644 --- a/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Style/IndentationTest.php @@ -20,7 +20,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Settings; -use PhpOffice\PhpWord\Shared\Html as SharedHtml; +use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWordTests\TestHelperDOCX; class IndentationTest extends \PHPUnit\Framework\TestCase @@ -40,7 +40,9 @@ public function testDefault(): void Settings::setDefaultRtl(true); $section = $word->addSection(); $text = $section->addText('AA'); - $text->getParagraphStyle()->setIndentation([]); + $paragraphStyle = $text->getParagraphStyle(); + self::assertInstanceOf(Paragraph::class, $paragraphStyle); + $paragraphStyle->setIndentation([]); $doc = TestHelperDOCX::getDocument($word, 'Word2007'); $path = '/w:document/w:body/w:p[1]/w:pPr/w:ind'; @@ -54,7 +56,9 @@ public function testFirstLineChars(): void Settings::setDefaultRtl(true); $section = $word->addSection(); $text = $section->addText('AA'); - $text->getParagraphStyle()->setIndentation([ + $paragraphStyle = $text->getParagraphStyle(); + self::assertInstanceOf(Paragraph::class, $paragraphStyle); + $paragraphStyle->setIndentation([ 'firstLineChars' => 1440, ]); $doc = TestHelperDOCX::getDocument($word, 'Word2007'); From 1ff8e6963ddd71eb89277d4fc6c951bea56d993a Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 20 Feb 2025 12:41:37 +0100 Subject: [PATCH 5/5] Fixed PHPStan --- .../Element/AbstractElementTest.php | 16 +++++++-- .../PhpWordTests/Style/AbstractStyleTest.php | 33 ++++++++++++++++--- .../Writer/EPub3/Part/AbstractPartTest.php | 11 ++++++- .../Writer/EPub3/Style/AbstractStyleTest.php | 12 ++++++- .../Writer/ODText/Part/AbstractPartTest.php | 24 ++++++++++++-- .../Writer/Word2007/Part/AbstractPartTest.php | 30 ++++++++++++++--- 6 files changed, 111 insertions(+), 15 deletions(-) diff --git a/tests/PhpWordTests/Element/AbstractElementTest.php b/tests/PhpWordTests/Element/AbstractElementTest.php index 8219871f73..307225b8f4 100644 --- a/tests/PhpWordTests/Element/AbstractElementTest.php +++ b/tests/PhpWordTests/Element/AbstractElementTest.php @@ -30,7 +30,13 @@ class AbstractElementTest extends \PHPUnit\Framework\TestCase */ public function testElementIndex(): void { - $stub = $this->getMockForAbstractClass(AbstractElement::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractElement::class); + } else { + /** @var AbstractElement $stub */ + $stub = new class() extends AbstractElement { + }; + } $ival = mt_rand(0, 100); $stub->setElementIndex($ival); self::assertEquals($ival, $stub->getElementIndex()); @@ -41,7 +47,13 @@ public function testElementIndex(): void */ public function testElementId(): void { - $stub = $this->getMockForAbstractClass(AbstractElement::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractElement::class); + } else { + /** @var AbstractElement $stub */ + $stub = new class() extends AbstractElement { + }; + } $stub->setElementId(); self::assertEquals(6, strlen($stub->getElementId())); } diff --git a/tests/PhpWordTests/Style/AbstractStyleTest.php b/tests/PhpWordTests/Style/AbstractStyleTest.php index 2f1585fd5a..2b8d3d8d17 100644 --- a/tests/PhpWordTests/Style/AbstractStyleTest.php +++ b/tests/PhpWordTests/Style/AbstractStyleTest.php @@ -20,6 +20,7 @@ use InvalidArgumentException; use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\Style\AbstractStyle; use PhpOffice\PhpWord\Style\Paragraph; use ReflectionClass; @@ -35,7 +36,13 @@ class AbstractStyleTest extends \PHPUnit\Framework\TestCase */ public function testSetStyleByArray(): void { - $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractStyle::class); + } else { + /** @var AbstractStyle $stub */ + $stub = new class() extends AbstractStyle { + }; + } $stub->setStyleByArray(['index' => 1]); self::assertEquals(1, $stub->getIndex()); @@ -62,7 +69,13 @@ public function testSetStyleByArrayWithAlignment(): void */ public function testSetValNormal(): void { - $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractStyle::class); + } else { + /** @var AbstractStyle $stub */ + $stub = new class() extends AbstractStyle { + }; + } self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false])); self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200])); @@ -76,7 +89,13 @@ public function testSetValNormal(): void */ public function testSetValDefault(): void { - $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractStyle::class); + } else { + /** @var AbstractStyle $stub */ + $stub = new class() extends AbstractStyle { + }; + } self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false])); self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200])); @@ -90,7 +109,13 @@ public function testSetValDefault(): void public function testSetValEnumException(): void { $this->expectException(InvalidArgumentException::class); - $stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle'); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(AbstractStyle::class); + } else { + /** @var AbstractStyle $stub */ + $stub = new class() extends AbstractStyle { + }; + } self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b'])); } diff --git a/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php index 5151eea24c..c12980c7e2 100644 --- a/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php @@ -15,7 +15,16 @@ class AbstractPartTest extends TestCase protected function setUp(): void { - $this->part = $this->getMockForAbstractClass(AbstractPart::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $this->part = $this->getMockForAbstractClass(AbstractPart::class); + } else { + $this->part = new class() extends AbstractPart { + public function write(): string + { + return ''; + } + }; + } } public function testParentWriter(): void diff --git a/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php b/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php index fb9135f0cd..12be0f2e4f 100644 --- a/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php +++ b/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php @@ -14,7 +14,17 @@ class AbstractStyleTest extends TestCase public function testParentWriter(): void { $parentWriter = new EPub3(); - $style = $this->getMockForAbstractClass(AbstractStyle::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $style = $this->getMockForAbstractClass(AbstractStyle::class); + } else { + /** @var AbstractStyle $style */ + $style = new class() extends AbstractStyle { + public function write(): string + { + return ''; + } + }; + } $result = $style->setParentWriter($parentWriter); diff --git a/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php index 6742b20ad5..27008dae82 100644 --- a/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php @@ -34,7 +34,17 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase */ public function testSetGetParentWriter(): void { - $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); + } else { + /** @var ODText\Part\AbstractPart $object */ + $object = new class() extends ODText\Part\AbstractPart { + public function write(): string + { + return ''; + } + }; + } $object->setParentWriter(new ODText()); self::assertEquals(new ODText(), $object->getParentWriter()); } @@ -46,7 +56,17 @@ public function testSetGetParentWriterNull(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('No parent WriterInterface assigned.'); - $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); + if (method_exists($this, 'getMockForAbstractClass')) { + $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); + } else { + /** @var ODText\Part\AbstractPart $object */ + $object = new class() extends ODText\Part\AbstractPart { + public function write(): string + { + return ''; + } + }; + } $object->getParentWriter(); } } diff --git a/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php index 04cdb5ff52..ca38d5d3fc 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php @@ -32,9 +32,19 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase */ public function testSetGetParentWriter(): void { - $object = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); - $object->setParentWriter(new Word2007()); - self::assertEquals(new Word2007(), $object->getParentWriter()); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); + } else { + /** @var Word2007\Part\AbstractPart $stub */ + $stub = new class() extends Word2007\Part\AbstractPart { + public function write(): string + { + return ''; + } + }; + } + $stub->setParentWriter(new Word2007()); + self::assertEquals(new Word2007(), $stub->getParentWriter()); } /** @@ -44,7 +54,17 @@ public function testSetGetParentWriterNull(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('No parent WriterInterface assigned.'); - $object = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); - $object->getParentWriter(); + if (method_exists($this, 'getMockForAbstractClass')) { + $stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); + } else { + /** @var Word2007\Part\AbstractPart $stub */ + $stub = new class() extends Word2007\Part\AbstractPart { + public function write(): string + { + return ''; + } + }; + } + $stub->getParentWriter(); } }