lukeraymonddowning/pest-plugin-money

一个让测试流行的Money包变得简单的Pest PHP插件。

v1.1.0 2021-11-18 11:47 UTC

This package is auto-updated.

Last update: 2024-09-18 17:47:58 UTC


README

此包是Pest PHP的一个插件。它允许您使用与Pest的期望语法相同的声明性语法,对由brick/moneymoneyphp/moneyarchtechx/money提供的货币值编写测试。

安装

要开始使用,请使用composer安装插件

composer require lukeraymonddowning/pest-plugin-money --dev

此包需要以下内容

  • Pest PHP
  • Brick Money、MoneyPHP或Archtech Money库中的任何一个
  • PHP 7.3或更高版本

使用方法

使用此插件非常简单!以下是此插件提供的期望示例。我们将使用Brick Money作为所有示例,但它们与MoneyPHP完全相同。

toBeMoney

要简单地断言一个对象是货币值,请使用toBeMoney方法

expect(Money::of(100, "GBP"))->toBeMoney();
expect("Hello World")->not->toBeMoney();

toCost

要检查货币值是否等于某个金额,请使用toCost方法

expect(Money::of(150, "GBP"))->toCost(150, 'GBP');
expect(Money::of(150, "GBP"))->toCost($anotherMoneyObject);
expect(Money::of(150, "GBP"))->not->toCost(100, 'GBP');

toCostLessThan

要检查货币值是否小于某个金额,请使用toCostLessThan方法

expect(Money::of(150, "GBP"))->toCostLessThan(160, 'GBP');
expect(Money::of(150, "GBP"))->toCostLessThan($anotherMoneyObject);
expect(Money::of(150, "GBP"))->not->toCostLessThan(140, 'GBP');

toCostMoreThan

要检查货币值是否大于某个金额,请使用toCostMoreThan方法

expect(Money::of(150, "GBP"))->toCostMoreThan(140, 'GBP');
expect(Money::of(150, "GBP"))->toCostMoreThan($anotherMoneyObject);
expect(Money::of(150, "GBP"))->not->toCostMoreThan(160, 'GBP');

选择货币库

此包将尝试自动检测您已安装的受支持的货币库中的哪一个。如果您想强制使用某个库,可以调用useMoneyLibrary函数。传递相关货币包的类名

useMoneyLibrary(\Money\Money::class); // Use the MoneyPHP library
useMoneyLibrary(\Brick\Money\Money::class); // Use the Brick Money library
useMoneyLibrary(\ArchTech\Money\Money::class); // Use the Archtech Money library

设置默认货币

如果您的应用程序主要使用单一货币,每次期望都需要将其作为第二个参数声明可能会很烦人。通过设置默认值,您可以省略货币并仅提供金额。

useCurrency('GBP'); 
expect($money)->toCost('100'); // Uses Great British Pounds

useCurrency('USD'); 
expect($money)->toCost('100'); // Uses US Dollars