Created
September 13, 2012 21:19
-
-
Save robertbasic/3717752 to your computer and use it in GitHub Desktop.
Chained returns of mocked objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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