james
james

Reputation: 3583

In PHPUnit, how do I mock parent methods?

I want to test a class method that calls upon a parent method with the same name. Is there a way to do this?

class Parent {

    function foo() {
        echo 'bar';
    }
}

class Child {

    function foo() {
            $foo = parent::foo();
            return $foo;
    }
}

class ChildTest extend PHPUnit_TestCase {

    function testFoo() {
        $mock = $this->getMock('Child', array('foo'));

        //how do i mock parent methods and simulate responses?
    }
}

Upvotes: 36

Views: 33181

Answers (5)

physicalattraction
physicalattraction

Reputation: 6858

A satisfying solution in my opinion is to create a class that inherits from your class under test and override the implementation of the method you want to give another implementation. This has its flaws: it does not always work, e.g. for already overridden methods and for private methods.

class Parent
{
    function bar()
    {
        echo 'bar';
    }
}

class Child extends Parent
{
    function foo()
    {
        parent::bar();
        echo 'foo';
    }
}

class mockChild extends Child
{
    function bar()
    {
        echo 'baz';
    }
}

class ChildTest extends PHPUnit_TestCase 
{
    function testFoo() {
        $sut = new mockChild();
        $sut->foo();
    }
} 

Upvotes: 0

Manish Trivedi
Manish Trivedi

Reputation: 3559

I'm totally agree with @Gordon. I have same issue but I have tried few tricky concept.

My scenario is like

class Parent { // Actual-Parent Class
    function save() {
        // do something
        return $this
    }
}

class Child extends Parent {
   // Subject under test
    function save() {
          // do something
          return parent::save();
    }
}

I have created another parent class with same name "Parent" and treat as a stub and include my stub class(parent) and ignore to actual parent (Actual parent class set into auto-load and stub-parent must be be included )

class Parent { //Stub-Parent class
    function save() {
        return $this
    }
}

Now I create mock object of Child class (via Mock-builder) and complete my test cases with end of assertSame. :-)

$this->assertSame($mock, $mock->save());

Upvotes: 2

user1029767
user1029767

Reputation: 119

An approach that works to my is the implementation of a wrap to the parent call on the child class, and finally mock those wrap.

You code modified:

class Parent {

    function foo() {
        echo 'bar';
    }
}

class Child {

    function foo() {
            $foo = $this->parentFooCall();
            return $foo;
    }
    function parentFooCall() {
            return parent::foo();
    }
}

class ChildTest extend PHPUnit_TestCase {

    function testFoo() {
        $mock = $this->getMock('Child', array('foo', 'parentFooCall'));

        //how do i mock parent methods and simulate responses?
    }
 }

Upvotes: 11

Dallas Caley
Dallas Caley

Reputation: 5888

Here is how I did it, I have no idea if this is correct but it works:

class parentClass {
    public function whatever() {
        $this->doSomething();
    }
}

class childClass extends parentClass {
    public $variable;
    public function subjectUnderTest() {
        $this->variable = 'whocares';
        parent::whatever();
    }
}

now in the test i do:

public function testSubjectUnderTest() {
    $ChildClass = $this->getMock('childClass', array('doSomething'))
    $ChildClass->expects($this->once())
               ->method('doSomething');
    $ChildClass->subjectUnderTest();
    $this->assertEquals('whocares', $ChildClass->variable);
}

what the ?

My reasoning here is that all i really want to test is whether or not my variable got set. i don't really care about what happens in the parent method but since you can't prevent the parent method from being called what i do is mock the dependent methods of the parent method.

now go ahead and tell me i'm wrong :)

Upvotes: 3

Gordon
Gordon

Reputation: 317197

You dont mock or stub methods in the Subject-under-Test (SUT). If you feel you have the need to mock or stub a method in the parent of the SUT, it likely means you shouldnt have used inheritance, but aggregation.

You mock dependencies of the Subject-under-Test. That means any other objects the SUT requires to do work.

Upvotes: 23

Related Questions