PHP code example of tmarsteel / mockery-callable-mock

1. Go to this page and download the library: Download tmarsteel/mockery-callable-mock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tmarsteel / mockery-callable-mock example snippets




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();