Skip to content
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

Magic methods on parent class not detected and MethodNotFoundException thrown #338

Open
dsnopek opened this issue Jun 19, 2017 · 0 comments

Comments

@dsnopek
Copy link
Contributor

dsnopek commented 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 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.

dsnopek added a commit to dsnopek/prophecy that referenced this issue Jun 19, 2017
dsnopek added a commit to dsnopek/prophecy that referenced this issue Jun 19, 2017
dsnopek added a commit to dsnopek/prophecy that referenced this issue Jun 19, 2017
ciaranmcnulty added a commit that referenced this issue Aug 5, 2018
Issue #338: Detect magic methods on parent class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant