slope-it/clock-mock 问题修复 & 功能扩展

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

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

slope-it/clock-mock

Composer 安装命令:

composer require slope-it/clock-mock

包简介

A library for mocking current date and time in tests.

README 文档

README

Slope s.r.l.

Latest Stable Version Total Downloads License

ClockMock provides a way for mocking the current timestamp used by PHP for \DateTime(Immutable) objects and date/time related functions. It requires the uopz extension (version >= 6.1.1).

This library is meant for development and testing only. It does not aim to propose a clock service to be used in production code, as we believe that you shouldn't need to do that when your only purpose is to mock the current time in testing code.

Why we built it

Here is an article that explains in depth how and why this library was built: link to the article.

TL;DR

  • We were looking for a way to mock the native php date and time functions and classes without having to change our production code for it, and without having ot use any 3rd party library for handling dates/clocks.
  • For this purpose, we were previously using the php-timecop extension but that never implemented support for PHP 7.4 onward.

Installation

You can install the library using Composer. Run the following command to install the latest version from Packagist:

composer require --dev slope-it/clock-mock

Note that, as this is not a tool intended for production, it should be required only for development (--dev flag).

Mocked functions/methods

  • date()
  • date_create()
  • date_create_from_format()
  • date_create_immutable()
  • date_create_immutable_from_format()
  • getdate()
  • gettimeofday()
  • gmdate()
  • gmmktime()
  • gmstrftime() (DEPRECATED starting from PHP 8.1)
  • idate()
  • localtime()
  • microtime()
  • mktime()
  • strftime() (DEPRECATED starting from PHP 8.1)
  • strtotime()
  • time()
  • unixtojd()
  • DateTime::__construct
  • DateTime::createFromFormat
  • DateTimeImmutable::__construct
  • DateTimeImmutable::createFromFormat
  • $_SERVER['REQUEST_TIME']
  • $_SERVER['REQUEST_TIME_FLOAT']

Usage

1. Stateful API

You can call ClockMock::freeze with a \DateTime or \DateTimeImmutable. Any code executed after it will use that specific date and time as the current timestamp. Call ClockMock::reset when done to restore real, current time.

Example:

<?php

use PHPUnit\Framework\TestCase;
use SlopeIt\ClockMock\ClockMock;

class MyTestCase extends TestCase
{
    public function test_something_using_stateful_mocking_api()
    {
        ClockMock::freeze(new \DateTime('1986-06-05'));
        
        // Code executed in here, until ::reset is called, will use the above date and time as "current"
        $nowYmd = date('Y-m-d');
        
        ClockMock::reset();
        
        $this->assertEquals('1986-06-05', $nowYmd);
    }
}

2. Stateless API

The library also provides a closure-based API that will execute the provided code at a specific point in time. This API does not need manually freezing or re-setting time, so it can be less error prone in some circumstances.

Example:

<?php

use PHPUnit\Framework\TestCase;
use SlopeIt\ClockMock\ClockMock;

class MyTestCase extends TestCase
{
    public function test_something_using_stateless_mocking_api()
    {
        $nowYmd = ClockMock::executeAtFrozenDateTime(new \DateTime('1986-06-05'), function () {
            // Code executed in here will use the above date and time as "current"
            return date('Y-m-d');
        });
        
        $this->assertEquals('1986-06-05', $nowYmd);
    }
}

How to contribute

  • Did you find and fix any bugs in the existing code?
  • Do you want to contribute a new feature, or a missing mock?
  • Do you think documentation can be improved?

Under any of these circumstances, please fork this repo and create a pull request. We are more than happy to accept contributions!

Credits

  • php-timecop, as ClockMock was inspired by it.
  • ext-uopz, as ClockMock is just a very thin layer on top of the amazing uopz extension, which provides a very convenient way to mock any function or method, including the ones of the php stdlib, at runtime.

slope-it/clock-mock 适用场景与选型建议

slope-it/clock-mock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.96M 次下载、GitHub Stars 达 77, 最近一次更新时间为 2021 年 05 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mock」 「clock」 「slope」 「slope.it」 「ClockMock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 slope-it/clock-mock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 slope-it/clock-mock 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.96M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 77
  • 点击次数: 25
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 77
  • Watchers: 6
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-23