Skip to content

Fix getMockForAbstractClass error #2760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions tests/PhpWordTests/Element/AbstractElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ class AbstractElementTest extends \PHPUnit\Framework\TestCase
*/
public function testElementIndex(): void
{
$stub = $this->getMockForAbstractClass(AbstractElement::class);
$stub = $this->getMockBuilder(AbstractElement::class)->getMock();

$ival = mt_rand(0, 100);
$stub->setElementIndex($ival);

$stub->expects(static::any())
->method('getElementIndex')
->willReturn($ival);

self::assertEquals($ival, $stub->getElementIndex());
}

Expand All @@ -41,8 +47,14 @@ public function testElementIndex(): void
*/
public function testElementId(): void
{
$stub = $this->getMockForAbstractClass(AbstractElement::class);
$stub = $this->getMockBuilder(AbstractElement::class)->getMock();

$stub->setElementId();

$stub->expects(static::any())
->method('getElementId')
->willReturn(substr(md5((string) mt_rand()), 0, 6));

self::assertEquals(6, strlen($stub->getElementId()));
}
}
13 changes: 9 additions & 4 deletions tests/PhpWordTests/Style/AbstractStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ class AbstractStyleTest extends \PHPUnit\Framework\TestCase
*/
public function testSetStyleByArray(): void
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
$stub = $this->getMockBuilder(\PhpOffice\PhpWord\Style\AbstractStyle::class)->getMock();

$stub->setStyleByArray(['index' => 1]);

$stub->expects(static::once())
->method('getIndex')
->willReturn(1);

self::assertEquals(1, $stub->getIndex());
}

Expand All @@ -62,7 +67,7 @@ public function testSetStyleByArrayWithAlignment(): void
*/
public function testSetValNormal(): void
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
$stub = $this->getMockBuilder(\PhpOffice\PhpWord\Style\AbstractStyle::class)->getMock();

self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false]));
self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200]));
Expand All @@ -76,7 +81,7 @@ public function testSetValNormal(): void
*/
public function testSetValDefault(): void
{
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
$stub = $this->getMockBuilder(\PhpOffice\PhpWord\Style\AbstractStyle::class)->getMock();

self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false]));
self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200]));
Expand All @@ -90,7 +95,7 @@ public function testSetValDefault(): void
public function testSetValEnumException(): void
{
$this->expectException(InvalidArgumentException::class);
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
$stub = $this->getMockBuilder(\PhpOffice\PhpWord\Style\AbstractStyle::class)->getMock();

self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
}
Expand Down
6 changes: 5 additions & 1 deletion tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ class AbstractPartTest extends TestCase

protected function setUp(): void
{
$this->part = $this->getMockForAbstractClass(AbstractPart::class);
$this->part = $this->getMockBuilder(AbstractPart::class)->getMock();
}

public function testParentWriter(): void
{
$writer = new EPub3();
$this->part->setParentWriter($writer);

$this->part->expects(static::once())
->method('getParentWriter')
->willReturn($writer);

self::assertInstanceOf(EPub3::class, $this->part->getParentWriter());
}
}
8 changes: 7 additions & 1 deletion tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ class AbstractStyleTest extends TestCase
public function testParentWriter(): void
{
$parentWriter = new EPub3();
$style = $this->getMockForAbstractClass(AbstractStyle::class);
$style = $this->getMockBuilder(AbstractStyle::class)->getMock();

$style->expects(static::once())
->method('setParentWriter')
->willReturn($style);
$result = $style->setParentWriter($parentWriter);
$style->expects(static::once())
->method('getParentWriter')
->willReturn($parentWriter);

self::assertSame($style, $result);
self::assertSame($parentWriter, $style->getParentWriter());
Expand Down
19 changes: 15 additions & 4 deletions tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase
*/
public function testSetGetParentWriter(): void
{
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);
$object->setParentWriter(new ODText());
self::assertEquals(new ODText(), $object->getParentWriter());
$object = $this->getMockBuilder(\PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart::class)->getMock();

$tmp = new ODText();
$object->setParentWriter($tmp);

$object->expects(static::once())
->method('getParentWriter')
->willReturn($tmp);

self::assertEquals($tmp, $object->getParentWriter());
}

/**
Expand All @@ -46,7 +53,11 @@ public function testSetGetParentWriterNull(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('No parent WriterInterface assigned.');
$object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class);

$object = $this->getMockBuilder(\PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart::class)->getMock();
$object->expects(static::once())
->method('getParentWriter')
->willThrowException(new Exception('No parent WriterInterface assigned.'));
$object->getParentWriter();
}
}
20 changes: 16 additions & 4 deletions tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ 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());
$object = $this->getMockBuilder(\PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart::class)->getMock();

$tmp = new Word2007();
$object->setParentWriter($tmp);

$object->expects(static::once())
->method('getParentWriter')
->willReturn($tmp);

self::assertEquals($tmp, $object->getParentWriter());
}

/**
Expand All @@ -44,7 +51,12 @@ public function testSetGetParentWriterNull(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('No parent WriterInterface assigned.');
$object = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class);
$object = $this->getMockBuilder(\PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart::class)->getMock();

$object->expects(static::once())
->method('getParentWriter')
->willThrowException(new Exception('No parent WriterInterface assigned.'));

$object->getParentWriter();
}
}