Skip to content

Instantly share code, notes, and snippets.

@gquemener
Created May 5, 2014 15:00
Show Gist options
  • Save gquemener/292e7c5a4bbb72fd48a8 to your computer and use it in GitHub Desktop.
Save gquemener/292e7c5a4bbb72fd48a8 to your computer and use it in GitHub Desktop.
Consecutive calls mocking /w phpspec
<?php
/**
* What I want to achieve
*/
class FooSpec
{
function it_does_stuff($collab)
{
$collab->doCollabStuf()->willReturn('foo'); // Returns foo the first time
$collab->doCollabStuf()->willReturn('bar'); // Returns bar the second time
// Returns null the third time
$this->doStuff()
}
}
/**
* What I usually do
*/
class FooSpec
{
function it_does_stuff($collab)
{
$returns = ['foo', 'bar']
$collab->doCollabStuf()->will(function() use (&$returns) {
return array_shift($returns);
});
$this->doStuff()
}
}
@docteurklein
Copy link

commands are things that permit to change state, while queries are here to read (eventually) current state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment