composer地址:mockery/mockery - Packagist
github地址:地址
文档地址:Mockery — Mockery Docs 1.0-alpha documentation
根据文档介绍,mockery是php mock对象框架。根据js的mock框架的作用,估计mockery也是通过创建对象获取数值,所以以便用于测试。
一、安装
composer安装
composer require mockery/mockery
二、使用
翻译文档:php mockery 中文文档 · 看云
class Book {}interface BookRepository {function find($id): Book;function findAll(): array;function add(Book $book): void;
}$double = Mockery::mock(BookRepository::class);
$double->find(11);#设置预期内容
#仅执行一次
$double->expects()->add($book);
#执行两次
$double->expects()->add($book)->twice();#忽略验证预期内容
// $double = Mockery::mock()->shouldIgnoreMissing();
$double = Mockery::spy();$double->foo(); // null
$double->bar(); // null#全局辅助方法 全局方法存在则会覆盖
Mockery::globalHelpers();$mock = mock(Some::class);
$spy = spy(Some::class);#trait测试
trait Foo {function foo() {return $this->doFoo();}abstract function doFoo();
}$double = Mockery::mock(Foo::class);
$double->allows()->doFoo()->andReturns(123);
$double->foo(); // int(123)