Skip to content

Commit

Permalink
PHPUnit 10 attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Mar 27, 2024
1 parent d88c3c6 commit 7038978
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 224 deletions.
10 changes: 4 additions & 6 deletions tests/Context/AutoContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

namespace Brick\Money\Tests\Context;

use Brick\Math\BigNumber;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\Context\CashContext;
use Brick\Money\Context\AutoContext;
use Brick\Money\Context\CashContext;
use Brick\Money\Currency;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigNumber;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class AutoContext.
*/
class AutoContextTest extends AbstractTestCase
{
/**
* @dataProvider providerApplyTo
*/
#[DataProvider('providerApplyTo')]
public function testApplyTo(string $amount, string $currency, RoundingMode $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
Expand Down
8 changes: 3 additions & 5 deletions tests/Context/CashContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace Brick\Money\Tests\Context;

use Brick\Math\BigNumber;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\Context\CashContext;
use Brick\Money\Currency;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigNumber;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class CashContext.
*/
class CashContextTest extends AbstractTestCase
{
/**
* @dataProvider providerApplyTo
*/
#[DataProvider('providerApplyTo')]
public function testApplyTo(int $step, string $amount, string $currency, RoundingMode $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
Expand Down
8 changes: 3 additions & 5 deletions tests/Context/CustomContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace Brick\Money\Tests\Context;

use Brick\Math\BigNumber;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\Context\CustomContext;
use Brick\Money\Currency;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigNumber;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class CustomContext.
*/
class CustomContextTest extends AbstractTestCase
{
/**
* @dataProvider providerApplyTo
*/
#[DataProvider('providerApplyTo')]
public function testApplyTo(int $scale, int $step, string $amount, string $currency, RoundingMode $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
Expand Down
8 changes: 3 additions & 5 deletions tests/Context/DefaultContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace Brick\Money\Tests\Context;

use Brick\Math\BigNumber;
use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Currency;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigNumber;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class DefaultContext.
*/
class DefaultContextTest extends AbstractTestCase
{
/**
* @dataProvider providerApplyTo
*/
#[DataProvider('providerApplyTo')]
public function testApplyTo(string $amount, string $currency, RoundingMode $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
Expand Down
20 changes: 8 additions & 12 deletions tests/CurrencyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace Brick\Money\Tests;

use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\Context;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Context\AutoContext;
use Brick\Money\Context\CustomContext;
use Brick\Money\Context\DefaultContext;
use Brick\Money\CurrencyConverter;
use Brick\Money\Exception\CurrencyConversionException;
use Brick\Money\ExchangeRateProvider\ConfigurableProvider;
use Brick\Money\Money;
use Brick\Money\MoneyBag;

use Brick\Math\Exception\RoundingNecessaryException;
use Brick\Math\RoundingMode;
use Brick\Money\RationalMoney;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class CurrencyConverter.
Expand All @@ -34,13 +34,12 @@ private function createCurrencyConverter() : CurrencyConverter
}

/**
* @dataProvider providerConvertMoney
*
* @param array $money The base money.
* @param string $toCurrency The currency code to convert to.
* @param RoundingMode $roundingMode The rounding mode to use.
* @param string $expectedResult The expected money's string representation, or an exception class name.
*/
#[DataProvider('providerConvertMoney')]
public function testConvertMoney(array $money, string $toCurrency, RoundingMode $roundingMode, string $expectedResult) : void
{
$money = Money::of(...$money);
Expand Down Expand Up @@ -76,14 +75,13 @@ public static function providerConvertMoney() : array
}

/**
* @dataProvider providerConvertMoneyBag
*
* @param array $monies The mixed currency monies to add.
* @param string $currency The target currency code.
* @param Context $context The target context.
* @param RoundingMode $roundingMode The rounding mode to use.
* @param string $total The expected total.
*/
#[DataProvider('providerConvertMoneyBag')]
public function testConvertMoneyBag(array $monies, string $currency, Context $context, RoundingMode $roundingMode, string $total) : void
{
$exchangeRateProvider = new ConfigurableProvider();
Expand Down Expand Up @@ -113,12 +111,11 @@ public static function providerConvertMoneyBag() : array
}

/**
* @dataProvider providerConvertMoneyBagToRational
*
* @param array $monies The mixed monies to add.
* @param string $currency The target currency code.
* @param string $expectedTotal The expected total.
*/
#[DataProvider('providerConvertMoneyBagToRational')]
public function testConvertMoneyBagToRational(array $monies, string $currency, string $expectedTotal) : void
{
$exchangeRateProvider = new ConfigurableProvider();
Expand Down Expand Up @@ -147,13 +144,12 @@ public static function providerConvertMoneyBagToRational() : array
}

/**
* @dataProvider providerConvertRationalMoney
*
* @param array $money The original amount and currency.
* @param string $toCurrency The currency code to convert to.
* @param RoundingMode $roundingMode The rounding mode to use.
* @param string $expectedResult The expected money's string representation, or an exception class name.
*/
#[DataProvider('providerConvertRationalMoney')]
public function testConvertRationalMoney(array $money, string $toCurrency, RoundingMode $roundingMode, string $expectedResult) : void
{
$currencyConverter = $this->createCurrencyConverter();
Expand Down
16 changes: 5 additions & 11 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

use Brick\Money\Currency;
use Brick\Money\Exception\UnknownCurrencyException;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Unit tests for class Currency.
*/
class CurrencyTest extends AbstractTestCase
{
/**
* @dataProvider providerOf
*
* @param string $currencyCode The currency code.
* @param int $numericCode The currency's numeric code.
* @param int $fractionDigits The currency's default fraction digits.
* @param string $name The currency's name.
*/
#[DataProvider('providerOf')]
public function testOf(string $currencyCode, int $numericCode, int $fractionDigits, string $name) : void
{
$currency = Currency::of($currencyCode);
Expand All @@ -40,9 +40,7 @@ public static function providerOf() : array
];
}

/**
* @dataProvider providerOfUnknownCurrencyCode
*/
#[DataProvider('providerOfUnknownCurrencyCode')]
public function testOfUnknownCurrencyCode(string|int $currencyCode) : void
{
$this->expectException(UnknownCurrencyException::class);
Expand All @@ -68,9 +66,7 @@ public function testOfReturnsSameInstance() : void
self::assertSame(Currency::of('EUR'), Currency::of('EUR'));
}

/**
* @dataProvider providerOfCountry
*/
#[DataProvider('providerOfCountry')]
public function testOfCountry(string $countryCode, string $expected) : void
{
if ($this->isExceptionClass($expected)) {
Expand Down Expand Up @@ -124,9 +120,7 @@ public function testIs() : void
self::assertTrue($clone->is($currency));
}

/**
* @dataProvider providerJsonSerialize
*/
#[DataProvider('providerJsonSerialize')]
public function testJsonSerialize(Currency $currency, string $expected): void
{
self::assertSame($expected, $currency->jsonSerialize());
Expand Down
12 changes: 4 additions & 8 deletions tests/ExchangeRateProvider/BaseCurrencyProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Brick\Money\Tests\ExchangeRateProvider;

use Brick\Math\BigNumber;
use Brick\Math\RoundingMode;
use Brick\Money\ExchangeRateProvider;
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
use Brick\Money\ExchangeRateProvider\ConfigurableProvider;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigRational;
use Brick\Math\RoundingMode;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class BaseCurrencyProvider.
Expand All @@ -30,12 +29,11 @@ private function getExchangeRateProvider() : ExchangeRateProvider
}

/**
* @dataProvider providerGetExchangeRate
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param string $exchangeRate The expected exchange rate, rounded DOWN to 6 decimals.
*/
#[DataProvider('providerGetExchangeRate')]
public function testGetExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode, string $exchangeRate) : void
{
$rate = $this->getExchangeRateProvider()->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
Expand All @@ -62,9 +60,7 @@ public static function providerGetExchangeRate() : array
];
}

/**
* @dataProvider providerReturnBigNumber
*/
#[DataProvider('providerReturnBigNumber')]
public function testReturnBigNumber(BigNumber|float|int|string $rate) : void
{
$configurableProvider = new ConfigurableProvider();
Expand Down
9 changes: 4 additions & 5 deletions tests/ExchangeRateProvider/ConfigurableProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Brick\Money\Tests\ExchangeRateProvider;

use Brick\Math\BigRational;
use Brick\Math\RoundingMode;
use Brick\Money\Exception\CurrencyConversionException;
use Brick\Money\ExchangeRateProvider;
use Brick\Money\ExchangeRateProvider\ConfigurableProvider;
use Brick\Money\Tests\AbstractTestCase;

use Brick\Math\BigRational;
use Brick\Math\RoundingMode;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Tests for class ConfigurableProvider.
Expand All @@ -29,12 +29,11 @@ private function getExchangeRateProvider() : ExchangeRateProvider
}

/**
* @dataProvider providerGetExchangeRate
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param string $exchangeRate The expected exchange rate, rounded DOWN to 3 decimals.
*/
#[DataProvider('providerGetExchangeRate')]
public function testGetExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode, string $exchangeRate) : void
{
$rate = $this->getExchangeRateProvider()->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
Expand Down
20 changes: 8 additions & 12 deletions tests/ExchangeRateProvider/PDOProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
use Brick\Money\Tests\AbstractTestCase;
use Closure;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

/**
* Tests for class PDOProvider.
*
* @requires extension pdo_sqlite
*/
#[RequiresPhpExtension('pdo_sqlite')]
class PDOProviderTest extends AbstractTestCase
{
/**
* @dataProvider providerConstructorWithInvalidConfiguration
*
* @param Closure(): PDOProviderConfiguration $getConfiguration
*/
#[DataProvider('providerConstructorWithInvalidConfiguration')]
public function testConfigurationConstructorThrows(Closure $getConfiguration, string $exceptionMessage) : void
{
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -67,12 +67,11 @@ public static function providerConstructorWithInvalidConfiguration() : array
}

/**
* @dataProvider providerGetExchangeRate
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param float|string $expectedResult The expected exchange rate, or an exception class if expected.
*/
#[DataProvider('providerGetExchangeRate')]
public function testGetExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode, float|string $expectedResult) : void
{
$pdo = new \PDO('sqlite::memory:');
Expand Down Expand Up @@ -123,12 +122,11 @@ public static function providerGetExchangeRate() : array
}

/**
* @dataProvider providerWithFixedSourceCurrency
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param float|string $expectedResult The expected exchange rate, or an exception class if expected.
*/
#[DataProvider('providerWithFixedSourceCurrency')]
public function testWithFixedSourceCurrency(string $sourceCurrencyCode, string $targetCurrencyCode, float|string $expectedResult) : void
{
$pdo = new \PDO('sqlite::memory:');
Expand Down Expand Up @@ -177,12 +175,11 @@ public static function providerWithFixedSourceCurrency() : array
}

/**
* @dataProvider providerWithFixedTargetCurrency
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param float|string $expectedResult The expected exchange rate, or an exception class if expected.
*/
#[DataProvider('providerWithFixedTargetCurrency')]
public function testWithFixedTargetCurrency(string $sourceCurrencyCode, string $targetCurrencyCode, float|string $expectedResult) : void
{
$pdo = new \PDO('sqlite::memory:');
Expand Down Expand Up @@ -231,13 +228,12 @@ public static function providerWithFixedTargetCurrency() : array
}

/**
* @dataProvider providerWithParameters
*
* @param string $sourceCurrencyCode The code of the source currency.
* @param string $targetCurrencyCode The code of the target currency.
* @param array $parameters The parameters to resolve the extra query placeholders.
* @param float|string $expectedResult The expected exchange rate, or an exception class if expected.
*/
#[DataProvider('providerWithParameters')]
public function testWithParameters(string $sourceCurrencyCode, string $targetCurrencyCode, array $parameters, float|string $expectedResult) : void
{
$pdo = new \PDO('sqlite::memory:');
Expand Down
Loading

0 comments on commit 7038978

Please sign in to comment.