Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit mock - will return object

Tags:

phpunit

Using phpunit mock object, I have a method that returns an object.

How do you code this using the expects / method / will methods?

i.e.

 ->will($this->returnValue('Class_Name'));
like image 227
Marty Wallace Avatar asked Jan 25 '13 21:01

Marty Wallace


1 Answers

Create the object, and return it with the returnValue() function. For example:

$myObject = new RandomObject();
$myFactory = $this->getMock('ObjectFactory', array('getRandomObject'));
$myFactory->expects($this->any())->method('getRandomObject')->will($this->returnValue($myObject);

$this->assertInstanceOf('RandomObject', $myFactory->getRandomObject());

This will pass.

You can also create that object as a mock itself and pass the mock.

like image 95
Samuel Kordik Avatar answered Jan 03 '23 11:01

Samuel Kordik