5.6. Actions

5.6.1. andDo(callback)

$this->mock()
     ->stub('myMethod')->andDo(function() {
         echo "myMethod() was called.";
     }))
     ->get();

This can also be used as a way to handle state that might be too complicated for the mocking engine:

$calledOddTimes = false;
$this->mock()
     ->stub('myMethod')->andDo(function() use (&$calledOddTimes) {
         $calledOddTimes = !$calledOddTimes;
     }))
     ->get();
$this->assert($calledOddTimes)->isTrue;

andDo will pass through arguments:

$mock = $this->mock('MyClass')
             ->expect('foo')->andDo(function ($a, $b) {
                 echo $a + $b;
             })
             ->get();

$mock->foo(3, 5);

// prints:
// 8

5.6.2. andReturn(value)

Return value where value can be of any type.

You may also provide more than one argument to specify multiple resturn values:

$mock = $this->mock()
             ->stub('myMethod')->andReturn('foo', 123)
             ->get();
$mock->myMethod(); // 'foo'
$mock->myMethod(); // 123

When using multiple return values the method can not be called more times than you have return values for - otherwise an exception is thrown.

5.6.3. andReturnCallback(callback)

Return the value returned by a callback function.

$mock = $this->mock()
             ->stub('myMethod')->andReturnCallback(function () {
                return 'foo';
             })
             ->get();
$mock->myMethod(); // 'foo'

The return value is evaluated when the invocation is made, so you can return different values for each invocation.

An optional Concise\Mock\InvocationInterface is passed through as the first and only argument to gain insight about the invocation:

$mock = $this->mock()
             ->stub('myMethod')->andReturnCallback(
                 function (InvocationInterface $invoke) {
                     return $invoke->getInvokeCount();
                 }
             )
             ->get();
$mock->myMethod(); // 1
$mock->myMethod(); // 2

You can also access the invocation arguments:

$mock = $this->mock()
             ->stub('myMethod')->andReturnCallback(
                 function (InvocationInterface $invoke) {
                     return $invoke->getArgument(1);
                 }
             )
             ->get();
$mock->myMethod('foo', 'bar'); // bar

5.6.4. andReturnProperty(propertyName)

To return the value of a property (of any visibility) when a method is invoked you can use andReturnProperty():

class MyClass
{
    protected $hidden = 'foo';

    public function myMethod()
    {
        return 'bar';
    }
}

$mock = $this->mock()
             ->stub('myMethod')->andReturnProperty('hidden')
             ->get();
$mock->myMethod(); // foo

5.6.5. andReturnSelf()

Return the mock instance (return $this). This is useful when you are mocking classes that using the chaining principle with methods.

5.6.6. andThrowException(exception)

Throw the exception when the method is called.

$this->mock()
     ->stub('myMethod')->andThrow(new \Exception('Uh-oh!'))
     ->get();