yunbuye / thinkphp-facade
ThinkPHP 方便被 mock 的 facade
0.8.2
2020-01-08 13:12 UTC
Requires
- php: >=5.6.0
- topthink/framework: 5.1.*
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2025-03-07 02:51:29 UTC
README
安装
非开发依赖,不要加--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