Laravel Mockery: mock with return logic
<p>I want to mock (or stub?) a class method that will return a dice result. I want the mock to return an expected value, lets say 2. But I also want my mock to return a 6 sometimes; like for example after 3:rd dice role.</p>

<p>To clarify, this is an example. The user has decides to role 4 dices, and I want the mock to always return 2 for every role - except for the 3:rd one which should return a 6.</p>

<p><strong>Code</strong></p>

<p>I am using PHP Laravel and I hope to make use of the Mockery library. This is how far I have come. My code is somewhat simplified for this example. I still have not figured out how to make a mock give different returning value depending on method arguments. Any idea how to do that?</p>

<pre><code>class DiceHelper{
    protected $diceClass;
    __construct($diceClass) // I set property diceClass in constructor...

   public function roleDices($nr_of_throws){
      for($x=0; $x &lt; count($nr_of_throws); $x++) {
         $result = $diceClass-&gt;roleOneDice($x);
       }
   }
}

class diceClass
{
   public function roleOneDice($dice_order){
      return rand(1, 6);
   }
}
</code></pre>

<p><em>Testfile</em></p>

<pre><code>class diceLogicTest extends TestCase
{
    /** @test */
    public function role_a_dice(){
        $mock = \Mockery::mock('diceClass[roleOneDice]');
        $mock-&gt;shouldReceive("roleOneDice")-&gt;andReturn(2);

        $theHelper = new DiceHelper($mock);
        $result = $theHelper-&gt;roleDices(2);

        $this-&gt;assertEquals(4,$result ); // Returns the expected 4.
    }
}
</code></pre>

<p><strong>Improvements</strong>
It would be nice if there was a way that the mock could count how many times it has been called, before returning a value. That way my DiceHelper method RoleDices dont have to send the argument $x (the current dice throw order). I guess the method should not be built to make tests work.</p>