Skip to content

Commit

Permalink
Fixed PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Feb 20, 2025
1 parent a58a808 commit 1ff8e69
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 15 deletions.
16 changes: 14 additions & 2 deletions tests/PhpWordTests/Element/AbstractElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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()));
}
Expand Down
33 changes: 29 additions & 4 deletions tests/PhpWordTests/Style/AbstractStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use InvalidArgumentException;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\AbstractStyle;
use PhpOffice\PhpWord\Style\Paragraph;
use ReflectionClass;

Expand All @@ -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());
Expand All @@ -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]));
Expand All @@ -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]));
Expand All @@ -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']));
}
Expand Down
11 changes: 10 additions & 1 deletion tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 22 additions & 2 deletions tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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();
}
}
30 changes: 25 additions & 5 deletions tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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();
}
}

0 comments on commit 1ff8e69

Please sign in to comment.