Skip to content

Instantly share code, notes, and snippets.

@gquemener
Created May 5, 2014 15:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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()
}
}
@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