非开发依赖,不要加--dev
composer require yubuye/thinkphp-facade
自定义的所有Facade必须继承 Yunbuye\ThinkFacade\Facade。
- 安装开发依赖
composer require mockery/mockery --dev
- 使用此扩展包后 mock 方式
例:MyFacade
MyFacade::shouldReceive('get')
->once()
->with('key')
->andReturn('value');
MyFacade::get('key')=='value'//true
- 框架自带 Facade 及 其他没有继承 Yunbuye\ThinkFacade\Facade 的 Facade 的 mock 方式
例: 缓存Cache的模拟
use Mockery;
use Mockery\Mock;
//先找到 Cache 对应的绑定实现类 think\Cache ,并对其进行模拟
$mock=Mockery::mock('think\Cache', function ($mock) {
/**
* @var Mock $mock
*/
$return='return';
$key='key';
return $mock->shouldReceive('get')->with($key)->andReturn($return);
});
Container::getInstance()->bindTo('think\Cache',$mock);
//模拟后,即 Cache Facade 也被模拟
Cache::get('key')=='return'//true