Fork me on GitHub

Kris Wallsmith

Symfony Guru at opensky.com.
Discussing web development, Symfony and fatherhood.

Oct 18

RFC: Mocking Fluent Interfaces in PHPUnit

I sent a pair of pull requests to Sebastian this morning for a very simple change that will make mocking fluent interfaces much easier.

This is how you might mock a fluent interface using PHPUnit 3.5.1:

$mock = $this->getMock('Person');
$mock
  ->expects($this->any())
  ->method('setName')
  ->will($this->returnValue($mock));

I’ve proposed a new returnSelf() method, which would cleanup this code, just a bit:

$mock = $this->getMock('Person');
$mock
  ->expects($this->any())
  ->method('setName')
  ->will($this->returnSelf());

What do you think?