tmarsteel/mockery-callable-mock
Create Mockery-friendly callable/function mocks in PHP. Set expectations (with args, counts), return values, stubs, and verify calls or non-calls. Can also wrap a real callable to spy while still invoking it. Requires PHP 7+.
<?php
use Akamon\MockeryCallableMock\MockeryCallableMock;
// creating a mockery function/callable
$mock = new MockeryCallableMock();
// call it normally
$mock('foo', 'bar');
call_user_func($mock, 'foo', 'bar');
// add it expectations
$mock->shouldBeCalled(); // returns a mockery expectation, so you can use it normally
$mock->shouldBeCalled()->with('foo')->once();
$mock('foo');
$mock->shouldBeCalled()->withNoArgs()->twice();
$mock();
$mock();
// returned values
$mock->shouldBeCalled()->andReturn('foo');
$retval = $mock();
// creating stubs
$stub = new MockeryCallableMock();
$stub->canBeCalled()->with('foo')->andReturn('bar');
// verifying calls
$mock = new MockeryCallableMock();
$mock('bar');
$mock->shouldHaveBeenCalled()->with('bar');
$mock->shouldNotHaveBeenCalled('foo');
// spying
$realAction = function($arg1, $arg2) { /* ... */ }; // or
$realAction = [$this, 'methodToInvoke'];
$callableSpy = new MockeryCallableMock($realAction);
$callableSpy(); // $realAction gets invoked
$callableSpy()->shouldHaveBeenCalled();
PHP 7.0+
Akamon Mockery Callable Mock is licensed under the MIT License. See the LICENSE file for full details.
How can I help you explore Laravel packages today?