robier/mock-global-php-functions
Composer 安装命令:
composer require robier/mock-global-php-functions
包简介
Mock any function that exists in global namespace
README 文档
README
Mock global php functions
This library allows you to mock any global PHP functions to return predefined values.
This library was created after I saw implementation of Symfony's ClockMock component for mocking PHP native functions related to time. I was curious how to make a mechanism for mocking any PHP global function, so I created this small library.
This is achieved by PHP's namespace fallback policy:
PHP will fall back to global functions […] if a namespaced function […] does not exist.
Library uses mentioned feature to dynamically create namespaced function. For this library to work your code that you are testing should be in non global namespace context and call function unqualified. For example:
namespace foo; $time = time(); // This call can be mocked, a call to \time() can't.
Known limitations
- Only unqualified function calls in a namespace context can be mocked. For example a call for
time()in the namespacefoois mockable, a call for\time()is not. - The mock has to be defined before the first call to the unqualified function in the tested class. This is documented in Bug #68541. In most cases, you can ignore this limitation as you only need to define a mock before any testing in your tests. It's also recommend that you run your tests as isolated process.
Api
At the moment there are 2 mocking objects that developers can use:
MockFunction- mock any given function with your own anonymous functionFreezeClock- mock time/sleep/date related functions, so they are in sync
MockFunction
Object that can mock any function from global namespace. Mock is active as long as magic method __destruct
is not called on that mock, or it's disabled manually via disable method. In that way you do not need to manually turn off mock
while writing tests, because as soon as current scope exists, mock is disabled, and you have clean slate.
Note: When creating mock use static callback instead of regular one. This prevents them from having the current class automatically bound to them.
Example:
// mock is active as soon it's defined $mock = new MockFunction('app', 'rand', static function(){ return 5; }); namespace app { // some logic $randomNumber = rand(); // random number will be always 5 // some logic }
Example of mocking sleep function:
$mock = new MockFunction('app', 'sleep', static function(){ return 0; }); namespace app { // some logic sleep(100); // sleep will not wait for 100 seconds as function is mocked // some logic }
FreezeClock
Handy object that can freeze time in given namespace.
Handy factory methods:
atZero- stops time at timestamp 0atTime- stops time at provided timeatMicrotime- stops time at provided microtimeatNow- stops at current time
Functions affected:
date- returns freeze formatted timetime- return freeze timemicrotime- return freeze microtimesleep- increase freeze time and it's not blocking like regular sleep functionusleep- increase freeze time and it's not blocking like regular usleep function
use Robier\MockGlobalFunction\FreezeClock; $mock = new FreezeClock::atZero('app/testNamespace'); \app\testNamespace\sleep(15); \app\testNamespace\time(); // would return 15 \app\testNamespace\microtime(true); // would return 15.0 also
Tests
First run docker/build to build a container and then docker/run composer run test for running all tests.
Contribution
Feel free to contribute!
robier/mock-global-php-functions 适用场景与选型建议
robier/mock-global-php-functions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.86k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mock」 「function」 「global」 「mock time」 「mock sleep」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 robier/mock-global-php-functions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 robier/mock-global-php-functions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 robier/mock-global-php-functions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
PHPUnit extension to Mock PHP internal functions using Runkit.
A lite function pack for PHP.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
PostgreSQL ROUND() function for Doctrine ORM
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 17.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-18