Skip to content

Instantly share code, notes, and snippets.

@robertbasic
Created September 13, 2012 21:19
Show Gist options
  • Select an option

  • Save robertbasic/3717752 to your computer and use it in GitHub Desktop.

Select an option

Save robertbasic/3717752 to your computer and use it in GitHub Desktop.
Chained returns of mocked objects
<?php
/**
* Ad-hoc unit tests for various scenarios reported by users
*/
class Mockery_AdhocTest extends PHPUnit_Framework_TestCase
{
public function setup ()
{
$this->container = new \Mockery\Container;
}
public function teardown()
{
$this->container->mockery_close();
}
public function testChainReturns()
{
$m = $this->container->mock('MockeryTest_MockedClass');
$real = new MockeryTest_RealClass;
$real->setMocked($m);
$m->shouldReceive('foo')
->once()
->with(1)
->andReturn($m);
$m->shouldReceive('bar')
->once()
->with(1)
->andReturn($m);
$real->doIt();
$this->assertTrue($real->getMocked() instanceof MockeryTest_MockedClass);
}
}
class MockeryTest_RealClass {
protected $mocked;
public function doIt()
{
$m = $this->getMocked();
$m->foo(1);
return 2;
}
public function setMocked($mocked)
{
$this->mocked = $mocked;
return $this;
}
public function getMocked()
{
if ($this->mocked === null) {
$this->setMocked(new MockeryTest_MockedClass);
}
return $this->mocked;
}
}
class MockeryTest_MockedClass {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment