Thursday, 15 April 2010

unit testing - How do I access overloaded properties on a PHPUnit mock object? -



unit testing - How do I access overloaded properties on a PHPUnit mock object? -

when mocking object phpunit, how access properties of object accessed via overloading, i.e. via __get()?

for example, in code below testing post object. each post has author, of type role. each role has signature property.

$author = $this->getmockbuilder('app_model_domain_role') ->disableoriginalconstructor() ->getmock(); $author->expects($this->any()) ->method('__get') ->will($this->returnvalue('authorname'));

as can see mock role object, configure homecoming string ('authorname') when __get() called. post object testing refers $this->author->signature. expecting homecoming 'authorname', instead test errors out saying $signature undefined property.

i tried configuring mock above without method() phone call (thinking expects() , will() calls apply mock's methods) still no success.

any ideas?

also, if know of tutorial on phpunit mocks i'd keen see - manual seems assume prior testing knowledge in particular area.

i tried same code , working me. phpunit version 3.7.12. version using? if running older version seek upgrading.

public function testmockingmagicgetter() { $mymock = $this->getmockbuilder('myclass') ->disableoriginalconstructor() ->getmock(); $mymock ->expects($this->any()) ->method('__get') ->will($this->returnvalue('authorname')); var_dump($mymock->signature); var_dump($mymock->dummy); var_dump($mymock->someprop); }

results in:

string(10) "authorname" string(10) "authorname" string(10) "authorname"

unit-testing mocking phpunit

No comments:

Post a Comment