Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1ff8e69

Browse files
committedFeb 20, 2025·
Fixed PHPStan
1 parent a58a808 commit 1ff8e69

File tree

6 files changed

+111
-15
lines changed

6 files changed

+111
-15
lines changed
 

‎tests/PhpWordTests/Element/AbstractElementTest.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ class AbstractElementTest extends \PHPUnit\Framework\TestCase
3030
*/
3131
public function testElementIndex(): void
3232
{
33-
$stub = $this->getMockForAbstractClass(AbstractElement::class);
33+
if (method_exists($this, 'getMockForAbstractClass')) {
34+
$stub = $this->getMockForAbstractClass(AbstractElement::class);
35+
} else {
36+
/** @var AbstractElement $stub */
37+
$stub = new class() extends AbstractElement {
38+
};
39+
}
3440
$ival = mt_rand(0, 100);
3541
$stub->setElementIndex($ival);
3642
self::assertEquals($ival, $stub->getElementIndex());
@@ -41,7 +47,13 @@ public function testElementIndex(): void
4147
*/
4248
public function testElementId(): void
4349
{
44-
$stub = $this->getMockForAbstractClass(AbstractElement::class);
50+
if (method_exists($this, 'getMockForAbstractClass')) {
51+
$stub = $this->getMockForAbstractClass(AbstractElement::class);
52+
} else {
53+
/** @var AbstractElement $stub */
54+
$stub = new class() extends AbstractElement {
55+
};
56+
}
4557
$stub->setElementId();
4658
self::assertEquals(6, strlen($stub->getElementId()));
4759
}

‎tests/PhpWordTests/Style/AbstractStyleTest.php

+29-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use InvalidArgumentException;
2222
use PhpOffice\PhpWord\SimpleType\Jc;
23+
use PhpOffice\PhpWord\Style\AbstractStyle;
2324
use PhpOffice\PhpWord\Style\Paragraph;
2425
use ReflectionClass;
2526

@@ -35,7 +36,13 @@ class AbstractStyleTest extends \PHPUnit\Framework\TestCase
3536
*/
3637
public function testSetStyleByArray(): void
3738
{
38-
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
39+
if (method_exists($this, 'getMockForAbstractClass')) {
40+
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
41+
} else {
42+
/** @var AbstractStyle $stub */
43+
$stub = new class() extends AbstractStyle {
44+
};
45+
}
3946
$stub->setStyleByArray(['index' => 1]);
4047

4148
self::assertEquals(1, $stub->getIndex());
@@ -62,7 +69,13 @@ public function testSetStyleByArrayWithAlignment(): void
6269
*/
6370
public function testSetValNormal(): void
6471
{
65-
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
72+
if (method_exists($this, 'getMockForAbstractClass')) {
73+
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
74+
} else {
75+
/** @var AbstractStyle $stub */
76+
$stub = new class() extends AbstractStyle {
77+
};
78+
}
6679

6780
self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false]));
6881
self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200]));
@@ -76,7 +89,13 @@ public function testSetValNormal(): void
7689
*/
7790
public function testSetValDefault(): void
7891
{
79-
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
92+
if (method_exists($this, 'getMockForAbstractClass')) {
93+
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
94+
} else {
95+
/** @var AbstractStyle $stub */
96+
$stub = new class() extends AbstractStyle {
97+
};
98+
}
8099

81100
self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false]));
82101
self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200]));
@@ -90,7 +109,13 @@ public function testSetValDefault(): void
90109
public function testSetValEnumException(): void
91110
{
92111
$this->expectException(InvalidArgumentException::class);
93-
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
112+
if (method_exists($this, 'getMockForAbstractClass')) {
113+
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
114+
} else {
115+
/** @var AbstractStyle $stub */
116+
$stub = new class() extends AbstractStyle {
117+
};
118+
}
94119

95120
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
96121
}

‎tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ class AbstractPartTest extends TestCase
1515

1616
protected function setUp(): void
1717
{
18-
$this->part = $this->getMockForAbstractClass(AbstractPart::class);
18+
if (method_exists($this, 'getMockForAbstractClass')) {
19+
$this->part = $this->getMockForAbstractClass(AbstractPart::class);
20+
} else {
21+
$this->part = new class() extends AbstractPart {
22+
public function write(): string
23+
{
24+
return '';
25+
}
26+
};
27+
}
1928
}
2029

2130
public function testParentWriter(): void

‎tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ class AbstractStyleTest extends TestCase
1414
public function testParentWriter(): void
1515
{
1616
$parentWriter = new EPub3();
17-
$style = $this->getMockForAbstractClass(AbstractStyle::class);
17+
if (method_exists($this, 'getMockForAbstractClass')) {
18+
$style = $this->getMockForAbstractClass(AbstractStyle::class);
19+
} else {
20+
/** @var AbstractStyle $style */
21+
$style = new class() extends AbstractStyle {
22+
public function write(): string
23+
{
24+
return '';
25+
}
26+
};
27+
}
1828

1929
$result = $style->setParentWriter($parentWriter);
2030

‎tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase
3434
*/
3535
public function testSetGetParentWriter(): void
3636
{
37-
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);
37+
if (method_exists($this, 'getMockForAbstractClass')) {
38+
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);
39+
} else {
40+
/** @var ODText\Part\AbstractPart $object */
41+
$object = new class() extends ODText\Part\AbstractPart {
42+
public function write(): string
43+
{
44+
return '';
45+
}
46+
};
47+
}
3848
$object->setParentWriter(new ODText());
3949
self::assertEquals(new ODText(), $object->getParentWriter());
4050
}
@@ -46,7 +56,17 @@ public function testSetGetParentWriterNull(): void
4656
{
4757
$this->expectException(Exception::class);
4858
$this->expectExceptionMessage('No parent WriterInterface assigned.');
49-
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);
59+
if (method_exists($this, 'getMockForAbstractClass')) {
60+
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);
61+
} else {
62+
/** @var ODText\Part\AbstractPart $object */
63+
$object = new class() extends ODText\Part\AbstractPart {
64+
public function write(): string
65+
{
66+
return '';
67+
}
68+
};
69+
}
5070
$object->getParentWriter();
5171
}
5272
}

‎tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php

+25-5
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase
3232
*/
3333
public function testSetGetParentWriter(): void
3434
{
35-
$object = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class);
36-
$object->setParentWriter(new Word2007());
37-
self::assertEquals(new Word2007(), $object->getParentWriter());
35+
if (method_exists($this, 'getMockForAbstractClass')) {
36+
$stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class);
37+
} else {
38+
/** @var Word2007\Part\AbstractPart $stub */
39+
$stub = new class() extends Word2007\Part\AbstractPart {
40+
public function write(): string
41+
{
42+
return '';
43+
}
44+
};
45+
}
46+
$stub->setParentWriter(new Word2007());
47+
self::assertEquals(new Word2007(), $stub->getParentWriter());
3848
}
3949

4050
/**
@@ -44,7 +54,17 @@ public function testSetGetParentWriterNull(): void
4454
{
4555
$this->expectException(Exception::class);
4656
$this->expectExceptionMessage('No parent WriterInterface assigned.');
47-
$object = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class);
48-
$object->getParentWriter();
57+
if (method_exists($this, 'getMockForAbstractClass')) {
58+
$stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class);
59+
} else {
60+
/** @var Word2007\Part\AbstractPart $stub */
61+
$stub = new class() extends Word2007\Part\AbstractPart {
62+
public function write(): string
63+
{
64+
return '';
65+
}
66+
};
67+
}
68+
$stub->getParentWriter();
4969
}
5070
}

0 commit comments

Comments
 (0)
Please sign in to comment.