php - Calling a method of a mock object -
i testing service phpunit.
i using setup()
create mock objects of dependencies service accepts, this:
public function setup() { $this->foomanagermock = $this->getfoomanagermock(); $this->barmanagermock = $this->getbarmanagermock(); $this->treemanagermock = $this->gettreemanagermock(); $this->loggermock = $this->getloggermock(); $this->myservice = new myservice($this->foomanagermock, $this->barmanagermock, $this->treemanagermock, $this->loggermock); }
bundle\tests\service\mytest::testservicewithvaliddata
/** * @dataprovider getservicevalidcasedata */ public function testservicewithvaliddata($case) { $this->asserttrue($this->myservice->serve($fooarray)); }
it turns out entity manager in serve()
function, having this:
return $this->foomanager->find($fooid) !== null;
in case $this->foomanager instance of mock_foomanager_4038382a
, whole test fails.
any suggestions more welcome.
update
/** * @return \phpunit_framework_mockobject_mockbuilder */ private function getfoomanagermock() { return $this->getmockbuilder( '\mybundle\entity\manager\foomanager' )->disableoriginalconstructor()->getmock(); }
example @matteo
i expecting $foomanager->find($id)
return me specific instance of foo, such as:
\mybundle\manager\foomanager{ "id" : 234234, "status" : "pending". "color" : "yellow", "tree" : [ "id" : 2345, "height" :160 ] }
you must specify in test case behaviour mocked entity must do, try adding code in testservicewithvaliddata
method, before call tested service:
$foomockobj = $this->getmock('\mybundle\entity\foo'); // mock method // ps can return concrete instance // $foomockobj= new \mybundle\entity\foo(); $this->foomanager ->expects($this->any()) ->method('find') // ->with($anoptionalinputid) ->will($this->returnvalue($foomockobj));
hope help
Comments
Post a Comment