Skip to content

Commit

Permalink
Add missing type hints and return types in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jan 9, 2020
1 parent 20acd28 commit eddb1f9
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 166 deletions.
43 changes: 26 additions & 17 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ abstract class AbstractTestCase extends TestCase
/**
* @param string $expected
* @param BigDecimal $actual
*
* @return void
*/
final protected function assertBigDecimalIs($expected, $actual)
final protected function assertBigDecimalIs(string $expected, BigDecimal $actual) : void
{
$this->assertInstanceOf(BigDecimal::class, $actual);
$this->assertSame($expected, (string) $actual);
}

/**
* @param string $expectedAmount The expected decimal amount.
* @param string $expectedCurrency The expected currency code.
* @param Money $actual The money to test.
*
* @return void
*/
final protected function assertMoneyEquals($expectedAmount, $expectedCurrency, $actual)
final protected function assertMoneyEquals(string $expectedAmount, string $expectedCurrency, Money $actual) : void
{
$this->assertInstanceOf(Money::class, $actual);
$this->assertSame($expectedCurrency, (string) $actual->getCurrency());
$this->assertSame($expectedAmount, (string) $actual->getAmount());
}
Expand All @@ -45,11 +47,12 @@ final protected function assertMoneyEquals($expectedAmount, $expectedCurrency, $
* @param string $expected The expected string representation of the Money.
* @param Money $actual The money to test.
* @param Context|null $context An optional context to check against the Money.
*
* @return void
*/
final protected function assertMoneyIs($expected, $actual, Context $context = null)
final protected function assertMoneyIs(string $expected, Money $actual, Context $context = null) : void
{
$this->assertInstanceOf(Money::class, $actual);
$this->assertSame((string) $expected, (string) $actual);
$this->assertSame($expected, (string) $actual);

if ($context !== null) {
$this->assertEquals($context, $actual->getContext());
Expand All @@ -59,8 +62,10 @@ final protected function assertMoneyIs($expected, $actual, Context $context = nu
/**
* @param string[] $expected
* @param Money[] $actual
*
* @return void
*/
final protected function assertMoniesAre(array $expected, array $actual)
final protected function assertMoniesAre(array $expected, array $actual) : void
{
foreach ($actual as $key => $money) {
$this->assertInstanceOf(Money::class, $money);
Expand All @@ -73,20 +78,22 @@ final protected function assertMoniesAre(array $expected, array $actual)
/**
* @param string $expected
* @param BigNumber $actual
*
* @return void
*/
final protected function assertBigNumberEquals($expected, BigNumber $actual)
final protected function assertBigNumberEquals(string $expected, BigNumber $actual) : void
{
$this->assertTrue($actual->isEqualTo($expected), $actual . ' != ' . $expected);
}

/**
* @param array $expectedAmounts
* @param MoneyBag $moneyBag
*
* @return void
*/
final protected function assertMoneyBagContains(array $expectedAmounts, $moneyBag)
final protected function assertMoneyBagContains(array $expectedAmounts, MoneyBag $moneyBag) : void
{
$this->assertInstanceOf(MoneyBag::class, $moneyBag);

// Test get() on each currency
foreach ($expectedAmounts as $currencyCode => $expectedAmount) {
$actualAmount = $moneyBag->getAmount($currencyCode);
Expand All @@ -107,10 +114,11 @@ final protected function assertMoneyBagContains(array $expectedAmounts, $moneyBa
/**
* @param string $expected
* @param RationalMoney $actual
*
* @return void
*/
final protected function assertRationalMoneyEquals($expected, $actual)
final protected function assertRationalMoneyEquals(string $expected, RationalMoney $actual) : void
{
$this->assertInstanceOf(RationalMoney::class, $actual);
$this->assertSame($expected, (string) $actual);
}

Expand All @@ -120,10 +128,11 @@ final protected function assertRationalMoneyEquals($expected, $actual)
* @param string $name
* @param int $defaultFractionDigits
* @param Currency $currency
*
* @return void
*/
final protected function assertCurrencyEquals($currencyCode, $numericCode, $name, $defaultFractionDigits, $currency)
final protected function assertCurrencyEquals(string $currencyCode, int $numericCode, string $name, int $defaultFractionDigits, Currency $currency) : void
{
$this->assertInstanceOf(Currency::class, $currency);
$this->assertSame($currencyCode, $currency->getCurrencyCode());
$this->assertSame($numericCode, $currency->getNumericCode());
$this->assertSame($name, $currency->getName());
Expand All @@ -135,7 +144,7 @@ final protected function assertCurrencyEquals($currencyCode, $numericCode, $name
*
* @return bool
*/
final protected function isExceptionClass($value)
final protected function isExceptionClass(string $value) : bool
{
return is_string($value) && substr($value, -9) === 'Exception';
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Context/AutoContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AutoContextTest extends AbstractTestCase
* @param int $roundingMode
* @param string $expected
*/
public function testApplyTo($amount, $currency, $roundingMode, $expected)
public function testApplyTo(string $amount, string $currency, int $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
$currency = Currency::of($currency);
Expand All @@ -45,7 +45,7 @@ public function testApplyTo($amount, $currency, $roundingMode, $expected)
/**
* @return array
*/
public function providerApplyTo()
public function providerApplyTo() : array
{
return [
['1', 'USD', RoundingMode::UNNECESSARY, '1'],
Expand All @@ -56,7 +56,7 @@ public function providerApplyTo()
];
}

public function testGetStep()
public function testGetStep() : void
{
$context = new CashContext(5);
$this->assertSame(5, $context->getStep());
Expand Down
6 changes: 3 additions & 3 deletions tests/Context/CashContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CashContextTest extends AbstractTestCase
* @param int $roundingMode
* @param string $expected
*/
public function testApplyTo($step, $amount, $currency, $roundingMode, $expected)
public function testApplyTo(int $step, string $amount, string $currency, int $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
$currency = Currency::of($currency);
Expand All @@ -45,7 +45,7 @@ public function testApplyTo($step, $amount, $currency, $roundingMode, $expected)
/**
* @return array
*/
public function providerApplyTo()
public function providerApplyTo() : array
{
return [
[1, '1', 'USD', RoundingMode::UNNECESSARY, '1.00'],
Expand All @@ -72,7 +72,7 @@ public function providerApplyTo()
];
}

public function testGetStep()
public function testGetStep() : void
{
$context = new CashContext(5);
$this->assertSame(5, $context->getStep());
Expand Down
6 changes: 3 additions & 3 deletions tests/Context/CustomContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CustomContextTest extends AbstractTestCase
* @param int $roundingMode
* @param string $expected
*/
public function testApplyTo($scale, $step, $amount, $currency, $roundingMode, $expected)
public function testApplyTo(int $scale, int $step, string $amount, string $currency, int $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
$currency = Currency::of($currency);
Expand All @@ -46,7 +46,7 @@ public function testApplyTo($scale, $step, $amount, $currency, $roundingMode, $e
/**
* @return array
*/
public function providerApplyTo()
public function providerApplyTo() : array
{
return [
[2, 1, '1', 'USD', RoundingMode::UNNECESSARY, '1.00'],
Expand Down Expand Up @@ -94,7 +94,7 @@ public function providerApplyTo()
];
}

public function testGetScaleGetStep()
public function testGetScaleGetStep() : void
{
$context = new CustomContext(8, 50);
$this->assertSame(8, $context->getScale());
Expand Down
6 changes: 3 additions & 3 deletions tests/Context/DefaultContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DefaultContextTest extends AbstractTestCase
* @param int $roundingMode
* @param string $expected
*/
public function testApplyTo($amount, $currency, $roundingMode, $expected)
public function testApplyTo(string $amount, string $currency, int $roundingMode, string $expected) : void
{
$amount = BigNumber::of($amount);
$currency = Currency::of($currency);
Expand All @@ -44,7 +44,7 @@ public function testApplyTo($amount, $currency, $roundingMode, $expected)
/**
* @return array
*/
public function providerApplyTo()
public function providerApplyTo() : array
{
return [
['1', 'USD', RoundingMode::UNNECESSARY, '1.00'],
Expand All @@ -59,7 +59,7 @@ public function providerApplyTo()
];
}

public function testGetStep()
public function testGetStep() : void
{
$context = new DefaultContext();
$this->assertSame(1, $context->getStep());
Expand Down
12 changes: 6 additions & 6 deletions tests/CurrencyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CurrencyConverterTest extends AbstractTestCase
/**
* @return CurrencyConverter
*/
private function createCurrencyConverter()
private function createCurrencyConverter() : CurrencyConverter
{
$exchangeRateProvider = new ConfigurableProvider();
$exchangeRateProvider->setExchangeRate('EUR', 'USD', '1.1');
Expand All @@ -42,7 +42,7 @@ private function createCurrencyConverter()
* @param int $roundingMode The rounding mode to use.
* @param string $expectedResult The expected money's string representation, or an exception class name.
*/
public function testConvertMoney(array $money, $toCurrency, $roundingMode, $expectedResult)
public function testConvertMoney(array $money, string $toCurrency, int $roundingMode, string $expectedResult) : void
{
$money = Money::of(...$money);
$currencyConverter = $this->createCurrencyConverter();
Expand All @@ -61,7 +61,7 @@ public function testConvertMoney(array $money, $toCurrency, $roundingMode, $expe
/**
* @return array
*/
public function providerConvertMoney()
public function providerConvertMoney() : array
{
return [
[['1.23', 'EUR'], 'USD', RoundingMode::DOWN, 'USD 1.35'],
Expand All @@ -88,7 +88,7 @@ public function providerConvertMoney()
* @param int $roundingMode The rounding mode to use.
* @param string $total The expected total.
*/
public function testConvertMoneyBag(array $monies, $currency, Context $context, $roundingMode, $total)
public function testConvertMoneyBag(array $monies, string $currency, Context $context, int $roundingMode, string $total) : void
{
$exchangeRateProvider = new ConfigurableProvider();
$exchangeRateProvider->setExchangeRate('EUR', 'USD', '1.23456789');
Expand Down Expand Up @@ -164,7 +164,7 @@ public function providerConvertMoneyBagToRational() : array
* @param int $roundingMode The rounding mode to use.
* @param string $expectedResult The expected money's string representation, or an exception class name.
*/
public function testConvertRationalMoney(array $money, $toCurrency, $roundingMode, $expectedResult)
public function testConvertRationalMoney(array $money, string $toCurrency, int $roundingMode, string $expectedResult) : void
{
$currencyConverter = $this->createCurrencyConverter();

Expand All @@ -184,7 +184,7 @@ public function testConvertRationalMoney(array $money, $toCurrency, $roundingMod
/**
* @return array
*/
public function providerConvertRationalMoney()
public function providerConvertRationalMoney() : array
{
return [
[['7/9', 'USD'], 'EUR', RoundingMode::DOWN, 'EUR 0.70'],
Expand Down
20 changes: 10 additions & 10 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CurrencyTest extends AbstractTestCase
* @param int $fractionDigits The currency's default fraction digits.
* @param string $name The currency's name.
*/
public function testOf($currencyCode, $numericCode, $fractionDigits, $name)
public function testOf(string $currencyCode, int $numericCode, int $fractionDigits, string $name) : void
{
$currency = Currency::of($currencyCode);
$this->assertCurrencyEquals($currencyCode, $numericCode, $name, $fractionDigits, $currency);
Expand All @@ -30,7 +30,7 @@ public function testOf($currencyCode, $numericCode, $fractionDigits, $name)
/**
* @return array
*/
public function providerOf()
public function providerOf() : array
{
return [
['USD', 840, 2, 'US Dollar'],
Expand All @@ -47,29 +47,29 @@ public function providerOf()
*
* @param string|int $currencyCode
*/
public function testOfUnknownCurrencyCode($currencyCode)
public function testOfUnknownCurrencyCode($currencyCode) : void
{
Currency::of($currencyCode);
}

/**
* @return array
*/
public function providerOfUnknownCurrencyCode()
public function providerOfUnknownCurrencyCode() : array
{
return [
['XXX'],
[-1],
];
}

public function testConstructor()
public function testConstructor() : void
{
$bitCoin = new Currency('BTC', -1, 'BitCoin', 8);
$this->assertCurrencyEquals('BTC', -1, 'BitCoin', 8, $bitCoin);
}

public function testOfReturnsSameInstance()
public function testOfReturnsSameInstance() : void
{
$this->assertSame(Currency::of('EUR'), Currency::of('EUR'));
}
Expand All @@ -80,7 +80,7 @@ public function testOfReturnsSameInstance()
* @param string $countryCode
* @param string $expected
*/
public function testOfCountry($countryCode, $expected)
public function testOfCountry(string $countryCode, string $expected) : void
{
if ($this->isExceptionClass($expected)) {
$this->expectException($expected);
Expand All @@ -97,7 +97,7 @@ public function testOfCountry($countryCode, $expected)
/**
* @return array
*/
public function providerOfCountry()
public function providerOfCountry() : array
{
return [
['CA', 'CAD'],
Expand All @@ -117,12 +117,12 @@ public function providerOfCountry()
/**
* @expectedException \InvalidArgumentException
*/
public function testCreateWithNegativeFractionDigits()
public function testCreateWithNegativeFractionDigits() : void
{
new Currency('BTC', 0, 'BitCoin', -1);
}

public function testIs()
public function testIs() : void
{
$currency = Currency::of('EUR');

Expand Down
6 changes: 3 additions & 3 deletions tests/ExchangeRateProvider/BaseCurrencyProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BaseCurrencyProviderTest extends AbstractTestCase
/**
* @return ExchangeRateProvider
*/
private function getExchangeRateProvider()
private function getExchangeRateProvider() : ExchangeRateProvider
{
$provider = new ConfigurableProvider();

Expand All @@ -36,7 +36,7 @@ private function getExchangeRateProvider()
* @param string $targetCurrencyCode The code of the target currency.
* @param string $exchangeRate The expected exchange rate, rounded DOWN to 6 decimals.
*/
public function testGetExchangeRate($sourceCurrencyCode, $targetCurrencyCode, $exchangeRate)
public function testGetExchangeRate(string $sourceCurrencyCode, string $targetCurrencyCode, string $exchangeRate) : void
{
$rate = $this->getExchangeRateProvider()->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
$this->assertSame($exchangeRate, (string) BigRational::of($rate)->toScale(6, RoundingMode::DOWN));
Expand All @@ -45,7 +45,7 @@ public function testGetExchangeRate($sourceCurrencyCode, $targetCurrencyCode, $e
/**
* @return array
*/
public function providerGetExchangeRate()
public function providerGetExchangeRate() : array
{
return [
['USD', 'EUR', '0.900000'],
Expand Down
Loading

0 comments on commit eddb1f9

Please sign in to comment.