PHP code example of mockery / mockery

1. Go to this page and download the library: Download mockery/mockery 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/ */

    

mockery / mockery example snippets

 php
$double = Mockery::mock();
 php
class Book {}

interface BookRepository {
    function find($id): Book;
    function findAll(): array;
    function add(Book $book): void;
}

$double = Mockery::mock(BookRepository::class);
 php
$double->allows()->find(123)->andReturns(new Book());

$book = $double->find(123);
 php
$double->shouldReceive('find')->with(123)->andReturn(new Book());
$book = $double->find(123);
 php

public function tearDown()
{
    Mockery::close();
}
 php
$double->expects()->add($book)->twice();
 php
$double->shouldReceive('find')
    ->with(123)
    ->once()
    ->andReturn(new Book());
$book = $double->find(123);
 php
// $double = Mockery::mock()->shouldIgnoreMissing();
$double = Mockery::spy();

$double->foo(); // null
$double->bar(); // null
 php
trait Foo {
    function foo() {
        return $this->doFoo();
    }

    abstract function doFoo();
}

$double = Mockery::mock(Foo::class);
$double->allows()->doFoo()->andReturns(123);
$double->foo(); // int(123)