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? } }
PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.
Stub. Stubs are used with query like methods - methods that return things, but it's not important if they're actually called. $stub = $this->createMock(SomeClass::class); $stub->method('getSomething') ->willReturn('foo'); $sut->action($stub);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With