robier/mock-global-php-functions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robier/mock-global-php-functions

Composer 安装命令:

composer require robier/mock-global-php-functions

包简介

Mock any function that exists in global namespace

README 文档

README

Build Status MIT

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 namespace foo is 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 function
  • FreezeClock - 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 0
  • atTime - stops time at provided time
  • atMicrotime - stops time at provided microtime
  • atNow - stops at current time

Functions affected:

  • date - returns freeze formatted time
  • time - return freeze time
  • microtime - return freeze microtime
  • sleep - increase freeze time and it's not blocking like regular sleep function
  • usleep - 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 robier/mock-global-php-functions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 17.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-18