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()
}
}
@everzet
Copy link

everzet commented May 5, 2014

@stof my last comment is the reason why it's not documented :)

@gquemener
Copy link
Author

Allright, thanks!

We won't use this feature too much, promise!

🍻

@everzet
Copy link

everzet commented May 5, 2014

@gquemener the general idea of CQRS is very simple - you have queries and commands. Query should always have same behaviour - called with the same arguments should always produce same result. The way to change your query behaviour is by using commands.

Prophecy is opinionated towards CQRS.

@gquemener
Copy link
Author

Thanks for the explanation @everzet.

However, I'm not sure to fully understand what you mean by "command", I've found an article of Martin Fowler about CQRS (http://martinfowler.com/bliki/CQRS.html).
I'll read it when I have enough time.

@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