An alternative solution for anyone who is still using older versions of PHPUnit but still wants to be able to directly call method() is to override the default object layout class template.
Copy MockObject/Generator/mocked_class.tpl.dist and name the copy mocked_class.tpl . Then just add the method() method code to the template:
public function method() { $any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount; $expects = $this->expects($any); $args = func_get_args(); return call_user_func_array(array($expects, 'method'), $args); }
This will allow you to directly call $mock->method() . However, you need to use ->will($this->returnValue(0)) instead of ->willReturn(0) . To do this, you need to enter a custom call builder and mocker invocation:
class My_MockObject_Builder_InvocationMocker extends PHPUnit_Framework_MockObject_Builder_InvocationMocker { public function willReturn( $value ) { return $this->will( new PHPUnit_Framework_MockObject_Stub_Return( $value ) ); } } class My_MockObject_InvocationMocker extends PHPUnit_Framework_MockObject_InvocationMocker { public function expects( PHPUnit_Framework_MockObject_Matcher_Invocation $matcher ) { return new My_MockObject_Builder_InvocationMocker($this, $matcher); } }
And update your template again to use My_MockObject_InvocationMocker instead of PHPUnit_Framework_MockObject_InvocationMocker .
The full template will look like this:
{prologue}{class_declaration} { protected static $staticInvocationMocker; protected $invocationMocker; {clone}{mocked_methods} public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher) { return $this->__phpunit_getInvocationMocker()->expects($matcher); } public function method() { $any = new PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount; $expects = $this->expects($any); $args = func_get_args(); return call_user_func_array(array($expects, 'method'), $args ); } public static function staticExpects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher) { return self::__phpunit_getStaticInvocationMocker()->expects($matcher); } public function __phpunit_getInvocationMocker() { if ($this->invocationMocker === NULL) { $this->invocationMocker = new My_MockObject_InvocationMocker; } return $this->invocationMocker; } public static function __phpunit_getStaticInvocationMocker() { if (self::$staticInvocationMocker === NULL) { self::$staticInvocationMocker = new My_MockObject_InvocationMocker; } return self::$staticInvocationMocker; } public function __phpunit_hasMatchers() { return self::__phpunit_getStaticInvocationMocker()->hasMatchers() || $this->__phpunit_getInvocationMocker()->hasMatchers(); } public function __phpunit_verify() { self::__phpunit_getStaticInvocationMocker()->verify(); $this->__phpunit_getInvocationMocker()->verify(); } public function __phpunit_cleanup() { self::$staticInvocationMocker = NULL; $this->invocationMocker = NULL; } }{epilogue}
source share