You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing some code which generates word documents using PhpWord, and I want to unit test that the right API calls are made on the PhpWord APIs.
The problem is that PhpWord has a class \PhpOffice\PhpWord\Element\AbstractContainer which has some magic methods via __call() and is the parent of several other classes, for example, \PhpOffice\PhpWord\Element\Section. The magic methods on AbstractContainer are correctly documented using @method and mocking these methods right on AbstractContainer works as expected, but doesn't when mocking any of its child classes like Section.
So, for example, I want to use code like this in my PHPUnit tests:
$section = $this->prophesize(\PhpOffice\PhpWord\Element\Section::class);
// One of the magic methods from parent. Throws exception!$section->addTextRun()->shouldBeCalled();
... but this throws MethodNotFoundException:
Prophecy\Exception\Doubler\MethodNotFoundException: Method `Double\PhpOffice\PhpWord\Element\Section\P1::addTextRun()` is not defined.
... however, doing the same on AbstractContainer works fine:
// Mocking parent directly:$section = $this->prophesize(\PhpOffice\PhpWord\Element\AbstractContainer::class);
// One of the magic methods from class itself. Works fine!$section->addTextRun()->shouldBeCalled();
So, whatever code in Prophecy that's finding magic methods via @method is only working when directly mocking a class, not when mocking a child class.
Unfortunately, in my case, I need to mock both methods from the parent and the class itself, otherwise, maybe I could have just mocked parent.
The text was updated successfully, but these errors were encountered:
dsnopek
added a commit
to dsnopek/prophecy
that referenced
this issue
Jun 19, 2017
I'm writing some code which generates word documents using PhpWord, and I want to unit test that the right API calls are made on the PhpWord APIs.
The problem is that PhpWord has a class \PhpOffice\PhpWord\Element\AbstractContainer which has some magic methods via
__call()
and is the parent of several other classes, for example, \PhpOffice\PhpWord\Element\Section. The magic methods onAbstractContainer
are correctly documented using@method
and mocking these methods right onAbstractContainer
works as expected, but doesn't when mocking any of its child classes likeSection
.So, for example, I want to use code like this in my PHPUnit tests:
... but this throws
MethodNotFoundException
:... however, doing the same on
AbstractContainer
works fine:So, whatever code in Prophecy that's finding magic methods via
@method
is only working when directly mocking a class, not when mocking a child class.Unfortunately, in my case, I need to mock both methods from the parent and the class itself, otherwise, maybe I could have just mocked parent.
The text was updated successfully, but these errors were encountered: