2
votes

I am getting Error: Call to undefined method Mock_SimpleInterface_8a93e777::mymethod() when I call the mymethod() on the Simple class mock.

class PlaygroundTest extends \PHPUnit_Framework_TestCase 
{
    public function testMock()
    {
        $class     = $this->getMockBuilder('\Playground\Simple')->getMock();

        $class->mymethod();
    }
}

The Simple class implementation

namespace Playground;

class Simple
{

    public function mymethod()
    {
        print "Hey!";
    }
}

According to PHPUnit docs (https://phpunit.de/manual/5.1/en/test-doubles.html), it states that "By default, all methods of the original class are replaced with a dummy implementation that just returns null (without calling the original method)."

Shouldn't I be able to call mymethod() and get a null return value? I want to avoid to specify all class methods. PHPUnit should be clever enough to know which methods can be called on the mock or not.

Is this a bug? I'm using PHPUnit 5.1.4

1
You are right. It is not a bug, I used this plenty of times. There has to be a different reason. You are assigning the mock to $class but calling mymethod() on $interface. Is it really like this or a mistake in your question? - ArSeN
It's a mistake from copying my code into stackoverflow. It really has been called on $class in the actual code. (I did tests on mocking an interface too, that's where I mistakenly copied my code). I will edit. - Lunfel
So I just copy&pasted your code, as I could not see any error. And I can tell you: There is none. I am getting no errors at all: OK (1 test, 0 assertions) is the result. The problem has to be elsehwere. Do you have a call stack, with which you could verify where the call is actually coming from? - ArSeN
Also: Which PHP version do you use? - ArSeN
It was on PHP7. But according to the docs, PHPUnit 5 is compatible with PHP 7 though. I'll check more in details on monday for the call stack. - Lunfel

1 Answers

2
votes

Your assumptions are correct, so you have an error somewhere else or did not show the real code.

The mock class name Mock_SimpleInterface_8a93e777 suggests that you don't actually mock \Playground\Simple but rather \Playground\SimpleInterface, which probably does not contain mymethod()