Moving a static call to a separate method, which is then mocked
Implemented Code:
class SomeModule { public function processFoo() { $foo = $this->getFoo(); $foo['hoopla'] = 'doo'; return $foo; } protected function getFoo() { return FooFactory::getFoo(); } }
Test code:
function testSomeModule() { // Whatever we want to simulate FooFactory::getFoo returning $foo = array('woo' => 'yay') // Create a copy of the class which mocks the method getFoo $module = $this->getMockBuilder('SomeModule') ->setMethods(array('getFoo')) ->getMock(); // Rig the mock method to return our prepared sample $module->expects($this->once()) ->method('getFoo') ->will($this->returnValue($foo)); $result = $module->processFoo(); $this->assertEquals('yay', $result['woo']); $this->assertEquals('doo', $result['hoopla']); }
source share