quizlet / hammock
Hammock 是为 Hacklang 设计的独立模拟库。
v1.1.0
2020-08-18 00:14 UTC
Requires
- hhvm: >=3.24
- hhvm/hhvm-autoload: ^1|^2|^3
- hhvm/hsl: >=1.4 <5
- quizlet/fb_intercept_polyfill: ^1|^2
This package is auto-updated.
Last update: 2024-09-24 01:00:29 UTC
README
Hammock 是为 Hack 语言设计的独立模拟库。其核心使用 fb_intercept
,可以拦截任何函数并改变其行为。Hammock 致力于提供方便的 API 来模拟公共方法和全局函数。虽然也可以模拟受保护和私有方法,但通常不建议这样做。以下是 Hammock 的一些关键特性:
- 块作用域模拟,在块的末尾自动恢复被模拟函数的原始行为。
- 跟踪拦截到的参数和被模拟函数的调用次数。
- 监控函数而不改变其行为。
安装
composer require --dev quizlet/hammock
使用方法
模拟对象的方法
$dog = new Dog(); $dog->fetch('ball') === 'ball'; // true using Hammock\mock_object_method($dog, 'fetch', $args ==> 'frisbee'); $dog->fetch('ball') === 'frisbee'; // true
使用块作用域进行相同的操作
$dog = new Dog(); using (Hammock\mock_object_method($dog, 'fetch', $args ==> 'frisbee')) { $dog->fetch('ball') === 'frisbee'; // true } // Original behavior is restored at the end of the `using` block. $dog->fetch('ball') === 'ball'; // true
使用拦截到的参数并获取调用次数
$dog = new Dog(); using $fetchMock = Hammock\mock_object_method($dog, 'fetch', $args ==> { // Each intercepted argument must be type-asserted. // We recommend https://github.com/hhvm/type-assert for this. $arg = strval($args[0]); if ($arg === 'ball') { return 'frisbee'; } return $arg; }); $dog->fetch('ball') === 'frisbee'; // true $dog->fetch('bone') === 'bone'; // true // The arguments for each intercepted call can be retrieved later. $fetchMock->getArgsForCall(0) === vec['ball']; // true $fetchMock->getArgsForCall(1) === vec['bone']; // true // The number of calls from the time the function was mocked. $fetchMock->getNumCalls() === 2; // true
在不改变行为的情况下监控对象的方法
$dog = new Dog(); using $fetchSpy = Hammock\spy_object_method($dog, 'fetch'); $dog->fetch('ball') === 'ball'; // true $fetchSpy->getNumCalls() === 1; // true
当没有实例时模拟类的的方法
using Hammock\mock_class_method(Ball::class, 'bounce', $args ==> false); Ball::bounce() === false; // true
完整的 API 文档可以在 MAIN_API.md 中找到。
高级用法可以在 ADVANCED.md 中找到。
贡献
如果你曾经想要为开源项目做出贡献,现在是机会!请阅读 CONTRIBUTING.md 以了解提交 pull request 的流程。
致谢
感谢以下为 Hammock 做出贡献的人:
- Tyron Jung
- Riya Dashoriya
- Lexidor
- Karoun Kasraie
- Andrew Sutherland
- Josh Rai
- Shaobo Sun
- Turadg Aleahmad
- Sean Young
- Chris Opperwall
联系方式
如果你有任何问题,请联系 riya.dashoriya@quizlet.com。