pristavu / pest-plugin-test-time
一个用于控制测试中时间流的 Pest 插件
3.0
2024-09-13 06:24 UTC
Requires
- php: ^8.1
- nesbot/carbon: ^2.65|^3
- pestphp/pest: ^2.0|^3.0
- spatie/test-time: ^1.3.2
Requires (Dev)
- spatie/ray: ^1.36
README
此 Pest 插件提供了一个名为 testTime
的函数,允许你在测试中冻结和操作当前时间。
use function Spatie\PestPluginTestTime\testTime; testTime()->freeze(); // time will not change anymore testTime()->addMinute(); // move time forward one minute
它还包含一个名为 toBeCarbon
的自定义预期,以便轻松检查 Carbon
实例的值。
$carbon = Carbon::createFromFormat('Y-m-d H:i:s', '2022-05-31 01:02:03'); // make an expectation on the whole date, including time expect($carbon)->toBeCarbon('2022-05-31 01:02:03'); // make an expectation on only the date part expect($carbon)->toBeCarbon('2022-05-31'); // explicitly pass in a format expect($carbon)->toBeCarbon('2022', 'Y');
支持我们
我们投入了大量资源来创建 一流的开放源代码包。你可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢你从家乡寄来明信片,说明你正在使用我们的哪个包。你可以在 我们的联系页面 上找到我们的地址。我们在 我们的虚拟明信片墙上 发布所有收到的明信片。
需求
此包是围绕 Carbon 的 setTestNow()
函数的包装。因此,你只能在使用 Carbon 的项目中使用此 Pest 插件。
安装
您可以通过 composer 安装此包
composer require spatie/pest-plugin-test-time --dev
使用
您可以通过调用 testTime
函数上的 freeze
来冻结当前时间。
use Carbon\Carbon; use function Spatie\PestPluginTestTime\testTime; testTime()->freeze(); // the current time will not change anymore Carbon::now(); // returns the time sleep(2); Carbon::now(); // will return the same time as above
在特定时间点冻结
您也可以通过传递 Y-m-d H:i:s
格式的日期来在特定点冻结时间。
testTime()->freeze('2021-01-02 12:34:56'); \Carbon\Carbon::now()->format('Y-m-d H:i:s') // returns '2021-01-02 12:34:56';
更改时间
您可以通过调用 Carbon
上可用的任何 add
和 sub
函数来更改时间。
testTime()->freeze('2021-01-02 12:34:56'); testTime()->addHour(); // time is now at '2021-01-02 13:34:56' // you can even chain method calls testTime()->subMinute()->addSeconds(2); // time is now at '2021-01-02 13:33:58'
期望一个 Carbon 值
此包提供了一个名为 toBeCarbon
的自定义预期,以便轻松检查 Carbon
实例的值。
$carbon = Carbon::createFromFormat('Y-m-d H:i:s', '2022-05-31 01:02:03'); // make an expectation on the whole date, including time expect($carbon)->toBeCarbon('2022-05-31 01:02:03'); // make an expectation on only the date part expect($carbon)->toBeCarbon('2022-05-31'); // explicitly pass in a format expect($carbon)->toBeCarbon('2022', 'Y');
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。