A plugin for working with popular money libraries in Pest

Overview

pest-plugin-money

This package is a plugin for Pest PHP. It allows you to write tests against monetary values provided by either brick/money or moneyphp/money using the same declarative syntax you're used to with Pest's expectation syntax.

Installation

To get started, install the plugin using composer:

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

This package requires the following:

  • Pest PHP
  • Either the Brick Money or MoneyPHP Money libraries
  • PHP 7.4 or greater

Usage

Using the plugin is simple! Here are examples of the expectations made available by this plugin. We will use Brick Money for all of our examples, but they work exactly the same with MoneyPHP.

toBeMoney

To simply assert that an object is a monetary value, use the toBeMoney method:

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

toCost

To check that a monetary value is equal to a certain amount, use the toCost method:

toCost(150, 'GBP'); expect(Money::of(150, "GBP"))->toCost($anotherMoneyObject); expect(Money::of(150, "GBP"))->not->toCost(100, 'GBP'); ">
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

To check that a monetary value is less than a certain amount, use the toCostLessThan method:

toCostLessThan(160, 'GBP'); expect(Money::of(150, "GBP"))->toCostLessThan($anotherMoneyObject); expect(Money::of(150, "GBP"))->not->toCostLessThan(140, 'GBP'); ">
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

To check that a monetary value is more than a certain amount, use the toCostMoreThan method:

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

Choosing a money library

This package uses Brick Money as a default, but you can change that using the useMoneyLibrary function. Pass the class name of the relevant money package:

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

Setting a default currency

If your application primarily uses a single currency, it can be annoying having to declare it as the second argument for each expectation. By setting a default, you can omit the currency and just provide the amount:

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

useCurrency('USD'); 
expect($money)->toCost('100'); // Uses US Dollars
You might also like...
A privacy respecting free as in freedom meta search engine for Google and popular torrent sites
A privacy respecting free as in freedom meta search engine for Google and popular torrent sites

A privacy respecting free as in freedom meta search engine for Google and popular torrent sites

This project is very diverse and based upon many languages and libraries such as C++, Python, JavaScript, PHP and MQTT

ADMS-Real-time-project This project is very diverse and based upon many languages and libraries such as C++, Python, JavaScript, PHP and MQTT Advance_

Thin assertion library for use in libraries and business-model

Assert A simple php library which contains assertions and guard methods for input validation (not filtering!) in business-model, libraries and applica

Foundation 3 Framework for Magento 1.7. Foundation styles and libraries. Magento Responsive theme. Off-canvas Left-Right sidebar columns for mobile.

Magento Foundation 3 Framework Zurb Foundation 3 framework for Magento 1.7. Magento Foundation 3 Version 1.3.0. Demo page: http://magendation.internet

This library implements a fuzzer for PHP, which can be used to find bugs in libraries

PHP Fuzzer This library implements a fuzzer for PHP, which can be used to find bugs in libraries (particularly parsing libraries) by feeding them "ran

Hoa is a modular, extensible and structured set of PHP libraries

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds. Hoa\Ust

Android libraries and/or signatures with classification (type, tags, anti-features)

Android Libraries A list of Android libraries and/or trackers along with classification such as type, categories and ant-features. Development Status:

A set of utilities for working with vk api!

vk-utils Документация на русском языке Installation composer require labile/vk-utils How to use it? Simple example use Astaroth\VkUtils\Client; $api

A PHPStan package that supports working with Extbase

PHPStan for Extbase This package provides a couple of stubs and services to make your life easier when working with PHPStan and Extbase. Examples clas

Releases(v0.1.0)
Owner
Luke Downing
Luke Downing
CPAY is a sdk that encapsulates the Orange Money api with an intuitive syntax allowing you to integrate the Orange Money payment into your PHP project.

CPAY CHOCO PAY is a sdk that encapsulates the Orange Money api with an intuitive syntax allowing you to integrate the Orange Money payment into your P

faso-dev 1 Oct 26, 2022
A simple package for working with money.

Money A simple package for working with money. Main features: Simple API Livewire integration Custom currency support Highly customizable formatting R

ARCHTECH 143 Nov 18, 2022
A pool for players to mine money

MoneyPool v1.0.2 A pool for players to mine money

Toby 4 Jun 22, 2021
The stock market inflation adjusted for the US-money supply

Inflation Chart Inflation Chart is a project to find the intrinsic value of stock markets, stock prices, goods and services by adjusting them to the a

levelsio 96 Apr 14, 2022
Laravel package to convert English numbers to Bangla number or Bangla text, Bangla month name and Bangla Money Format

Number to Bangla Number, Word or Month Name in Laravel | Get Wordpress Plugin Laravel package to convert English numbers to Bangla number or Bangla te

Md. Rakibul Islam 50 Dec 26, 2022
> Create e-wallet, send money, withdraw and check balance all via USSD protocol

Mobile Money USSD solution Create e-wallet, send money, withdraw and check balance all via USSD protocol Create e-wallet Step 1 Step 2 Step 3 Step 4 S

Emmanuel HAKORIMANA 1 Nov 3, 2021
A money and currency library for PHP

Brick\Money A money and currency library for PHP. Introduction Working with financial data is a serious matter, and small rounding mistakes in an appl

Brick 1.3k Jan 5, 2023
Standardized wrapper for popular currency rate APIs. Currently supports FixerIO, CurrencyLayer, Open Exchange Rates and Exchange Rates API.

?? Wrapper for popular Currency Exchange Rate APIs A PHP API Wrapper to offer a unified programming interface for popular Currency Rate APIs. Dont wor

Alexander Graf 24 Nov 21, 2022
The tool converts different error reporting standards for deep compatibility with popular CI systems (TeamCity, IntelliJ IDEA, GitHub Actions, etc).

JBZoo / CI-Report-Converter Why? Installing Using as GitHub Action Example GitHub Action workflow Available Directions Help description in terminal Co

JBZoo Toolbox 17 Jun 16, 2022
PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

forceutf8 PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strin

Sebastián Grignoli 1.6k Dec 22, 2022