Function mocking with Patchwork 10

Here be mocking meta.

The flow “on paper”

I’ve worked to wrap PHPUnit mock objects and be able to add my interface methods to them. Supposing a class like

class SomeClass{ public function instanceMethod(){} } 

getting a mock of the class using PHPUnit getMock method

$mock = $this->getMock('SomeClass'); 

will make PHPUnit generate and eval code like

class Mock_SomeClass_a35f4fca extends SomeClass implements PHPUnit_Framework_MockObject_MockObject { private $__phpunit_invocationMocker; private $__phpunit_originalObject; public function __clone() { $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker(); } public function __construct() { $arguments = array(); $count = func_num_args(); if ($count > 0) { $_arguments = func_get_args(); for ($i = 0; $i < $count; $i++) { $arguments[] = $_arguments[$i]; } } $result = $this->__phpunit_getInvocationMocker()->invoke( new PHPUnit_Framework_MockObject_Invocation_Object( 'tad\FunctionMocker\Tests\SomeClass', '__construct', $arguments, $this, FALSE ) ); return $result; } public function instanceMethod() { $arguments = array(); $count = func_num_args(); if ($count > 0) { $_arguments = func_get_args(); for ($i = 0; $i < $count; $i++) { $arguments[] = $_arguments[$i]; } } $result = $this->__phpunit_getInvocationMocker()->invoke( new PHPUnit_Framework_MockObject_Invocation_Object( 'tad\FunctionMocker\Tests\SomeClass', 'instanceMethod', $arguments, $this, FALSE ) ); return $result; } 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); return call_user_func_array(array($expects, 'method'), func_get_args()); } public function __phpunit_setOriginalObject($originalObject) { $this->__phpunit_originalObject = $originalObject; } public function __phpunit_getInvocationMocker() { if ($this->__phpunit_invocationMocker === NULL) { $this->__phpunit_invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker; } return $this->__phpunit_invocationMocker; } public function __phpunit_hasMatchers() { return $this->__phpunit_getInvocationMocker()->hasMatchers(); } public function __phpunit_verify() { $this->__phpunit_getInvocationMocker()->verify(); $this->__phpunit_invocationMocker = NULL; } } 

where the original SomeClass method instanceMethod is present but overridden. Keeping on the same line I want my mock object wrapper, an extension of the Mock_SomeClass_a35f4fca class created by PHPUnit, to have code like

class Mock_Extender_11223344 extends Mock_SomeClass_a35f4fca implements \tad\FunctionMocker\InstanceMock { private $__functionMocker_mockCallLogger; private $__functionMocker_originalMockObject; public function __set_functionMocker_mockCallLogger(\tad\FunctionMocker\MockCallLogger $logger){ $this->__functionMocker_mockCallLogger = $logger; } public function __set_functionMocker_originalMockObject(\PHPUnit_Framework_MockObject_MockObject $mockObject){ $this->__functionMocker_originalMockObject = $mockObject; } public function shouldBeCalledTimes($times){ $this->__functionMocker_mockCallLogger->shouldBeCalledTimes($times); } public function instanceMethod(){ return $this->__functionMocker_originalMockObject->instanceMethod(); } } 

and that’s what I’m coding to. Some templating is in order and that’s not ready yet but I needed to get my duck in a row before going on.

##Next
The flow but at run-time.

I appreciate your input