Skip to content

Commit

Permalink
Add native union types to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Mar 27, 2024
1 parent 71d27c1 commit e175973
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 93 deletions.
87 changes: 42 additions & 45 deletions tests/BigDecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
class BigDecimalTest extends AbstractTestCase
{
/**
* @param string|number $value The value to convert to a BigDecimal.
* @param string $unscaledValue The expected unscaled value.
* @param int $scale The expected scale.
* @param int|float|string $value The value to convert to a BigDecimal.
* @param string $unscaledValue The expected unscaled value.
* @param int $scale The expected scale.
*/
#[DataProvider('providerOf')]
public function testOf($value, string $unscaledValue, int $scale) : void
public function testOf(int|float|string $value, string $unscaledValue, int $scale) : void
{
self::assertBigDecimalInternalValues($unscaledValue, $scale, BigDecimal::of($value));
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public static function providerOfFloatInDifferentLocales() : array
}

#[DataProvider('providerOfInvalidValueThrowsException')]
public function testOfInvalidValueThrowsException($value) : void
public function testOfInvalidValueThrowsException(int|float|string $value) : void
{
$this->expectException(NumberFormatException::class);
BigDecimal::of($value);
Expand Down Expand Up @@ -266,12 +266,12 @@ public function testOfBigDecimalReturnsThis() : void
}

/**
* @param string|int $unscaledValue The unscaled value of the BigDecimal to create.
* @param int|string $unscaledValue The unscaled value of the BigDecimal to create.
* @param int $scale The scale of the BigDecimal to create.
* @param string $expectedUnscaledValue The expected result unscaled value.
*/
#[DataProvider('providerOfUnscaledValue')]
public function testOfUnscaledValue($unscaledValue, int $scale, string $expectedUnscaledValue) : void
public function testOfUnscaledValue(int|string $unscaledValue, int $scale, string $expectedUnscaledValue) : void
{
$number = BigDecimal::ofUnscaledValue($unscaledValue, $scale);
self::assertBigDecimalInternalValues($expectedUnscaledValue, $scale, $number);
Expand Down Expand Up @@ -751,11 +751,8 @@ public static function providerDividedBy() : array
];
}

/**
* @param string|number $zero
*/
#[DataProvider('providerDividedByByZeroThrowsException')]
public function testDividedByByZeroThrowsException($zero) : void
public function testDividedByByZeroThrowsException(int|float|string $zero) : void
{
$this->expectException(DivisionByZeroException::class);
BigDecimal::of(1)->dividedBy($zero, 0);
Expand All @@ -773,12 +770,12 @@ public static function providerDividedByByZeroThrowsException() : array
}

/**
* @param string|number $number The number to divide.
* @param string|number $divisor The divisor.
* @param string $expected The expected result, or a class name if an exception is expected.
* @param int|float|string $number The number to divide.
* @param int|float|string $divisor The divisor.
* @param string $expected The expected result, or a class name if an exception is expected.
*/
#[DataProvider('providerExactlyDividedBy')]
public function testExactlyDividedBy($number, $divisor, string $expected) : void
public function testExactlyDividedBy(int|float|string $number, int|float|string $divisor, string $expected) : void
{
$number = BigDecimal::of($number);

Expand Down Expand Up @@ -2044,66 +2041,66 @@ public static function providerNegated() : array

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testCompareTo(string $a, $b, int $c) : void
public function testCompareTo(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c, BigDecimal::of($a)->compareTo($b));
}

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testIsEqualTo(string $a, $b, int $c) : void
public function testIsEqualTo(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c === 0, BigDecimal::of($a)->isEqualTo($b));
}

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testIsLessThan(string $a, $b, int $c) : void
public function testIsLessThan(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c < 0, BigDecimal::of($a)->isLessThan($b));
}

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testIsLessThanOrEqualTo(string $a, $b, int $c) : void
public function testIsLessThanOrEqualTo(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c <= 0, BigDecimal::of($a)->isLessThanOrEqualTo($b));
}

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testIsGreaterThan(string $a, $b, int $c) : void
public function testIsGreaterThan(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c > 0, BigDecimal::of($a)->isGreaterThan($b));
}

/**
* @param string $a The base number as a string.
* @param string|int|float $b The number to compare to.
* @param int|float|string $b The number to compare to.
* @param int $c The comparison result.
*/
#[DataProvider('providerCompareTo')]
public function testIsGreaterThanOrEqualTo(string $a, $b, int $c) : void
public function testIsGreaterThanOrEqualTo(string $a, int|float|string $b, int $c) : void
{
self::assertSame($c >= 0, BigDecimal::of($a)->isGreaterThanOrEqualTo($b));
}
Expand Down Expand Up @@ -2158,61 +2155,61 @@ public static function providerCompareTo() : array
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testGetSign($number, int $sign) : void
public function testGetSign(int|float|string $number, int $sign) : void
{
self::assertSame($sign, BigDecimal::of($number)->getSign());
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsZero($number, int $sign) : void
public function testIsZero(int|float|string $number, int $sign) : void
{
self::assertSame($sign === 0, BigDecimal::of($number)->isZero());
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsNegative($number, int $sign) : void
public function testIsNegative(int|float|string $number, int $sign) : void
{
self::assertSame($sign < 0, BigDecimal::of($number)->isNegative());
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsNegativeOrZero($number, int $sign) : void
public function testIsNegativeOrZero(int|float|string $number, int $sign) : void
{
self::assertSame($sign <= 0, BigDecimal::of($number)->isNegativeOrZero());
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsPositive($number, int $sign) : void
public function testIsPositive(int|float|string $number, int $sign) : void
{
self::assertSame($sign > 0, BigDecimal::of($number)->isPositive());
}

/**
* @param number|string $number The number to test.
* @param int $sign The sign of the number.
* @param int|float|string $number The number to test.
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsPositiveOrZero($number, int $sign) : void
public function testIsPositiveOrZero(int|float|string $number, int $sign) : void
{
self::assertSame($sign >= 0, BigDecimal::of($number)->isPositiveOrZero());
}
Expand Down
40 changes: 17 additions & 23 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
class BigIntegerTest extends AbstractTestCase
{
/**
* @param string|number $value The value to convert to a BigInteger.
* @param string $expected The expected string value of the result.
* @param int|float|string $value The value to convert to a BigInteger.
* @param string $expected The expected string value of the result.
*/
#[DataProvider('providerOf')]
public function testOf($value, string $expected) : void
public function testOf(int|float|string $value, string $expected) : void
{
self::assertBigIntegerEquals($expected, BigInteger::of($value));
}
Expand Down Expand Up @@ -110,11 +110,8 @@ public function testOfBigIntegerReturnsThis() : void
self::assertSame($decimal, BigInteger::of($decimal));
}

/**
* @param string|number $value
*/
#[DataProvider('providerOfInvalidFormatThrowsException')]
public function testOfInvalidFormatThrowsException($value) : void
public function testOfInvalidFormatThrowsException(int|float|string $value) : void
{
$this->expectException(NumberFormatException::class);
BigInteger::of($value);
Expand All @@ -138,11 +135,8 @@ public static function providerOfInvalidFormatThrowsException() : array
];
}

/**
* @param float|string $value
*/
#[DataProvider('providerOfNonConvertibleValueThrowsException')]
public function testOfNonConvertibleValueThrowsException($value) : void
public function testOfNonConvertibleValueThrowsException(float|string $value) : void
{
$this->expectException(RoundingNecessaryException::class);
BigInteger::of($value);
Expand Down Expand Up @@ -568,12 +562,12 @@ public static function providerMinus() : array
}

/**
* @param string $a The base number.
* @param string|int $b The number to multiply.
* @param string $r The expected result.
* @param string $a The base number.
* @param int|float|string $b The number to multiply.
* @param string $r The expected result.
*/
#[DataProvider('providerMultipliedBy')]
public function testMultipliedBy(string $a, $b, string $r) : void
public function testMultipliedBy(string $a, int|float|string $b, string $r) : void
{
self::assertBigIntegerEquals($r, BigInteger::of($a)->multipliedBy($b));
}
Expand Down Expand Up @@ -602,11 +596,11 @@ public static function providerMultipliedBy() : array

/**
* @param string $number The base number.
* @param string|int|float $divisor The divisor.
* @param int|float|string $divisor The divisor.
* @param string $expected The expected result, or a class name if an exception is expected.
*/
#[DataProvider('providerDividedBy')]
public function testDividedBy(string $number, $divisor, string $expected) : void
public function testDividedBy(string $number, int|float|string $divisor, string $expected) : void
{
$number = BigInteger::of($number);

Expand Down Expand Up @@ -2847,7 +2841,7 @@ public static function providerCompareTo() : array
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testGetSign($number, int $sign) : void
public function testGetSign(int|string $number, int $sign) : void
{
self::assertSame($sign, BigInteger::of($number)->getSign());
}
Expand All @@ -2857,7 +2851,7 @@ public function testGetSign($number, int $sign) : void
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsZero($number, int $sign) : void
public function testIsZero(int|string $number, int $sign) : void
{
self::assertSame($sign === 0, BigInteger::of($number)->isZero());
}
Expand All @@ -2867,7 +2861,7 @@ public function testIsZero($number, int $sign) : void
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsNegative($number, int $sign) : void
public function testIsNegative(int|string $number, int $sign) : void
{
self::assertSame($sign < 0, BigInteger::of($number)->isNegative());
}
Expand All @@ -2877,7 +2871,7 @@ public function testIsNegative($number, int $sign) : void
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsNegativeOrZero($number, int $sign) : void
public function testIsNegativeOrZero(int|string $number, int $sign) : void
{
self::assertSame($sign <= 0, BigInteger::of($number)->isNegativeOrZero());
}
Expand All @@ -2887,7 +2881,7 @@ public function testIsNegativeOrZero($number, int $sign) : void
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsPositive($number, int $sign) : void
public function testIsPositive(int|string $number, int $sign) : void
{
self::assertSame($sign > 0, BigInteger::of($number)->isPositive());
}
Expand All @@ -2897,7 +2891,7 @@ public function testIsPositive($number, int $sign) : void
* @param int $sign The sign of the number.
*/
#[DataProvider('providerSign')]
public function testIsPositiveOrZero($number, int $sign) : void
public function testIsPositiveOrZero(int|string $number, int $sign) : void
{
self::assertSame($sign >= 0, BigInteger::of($number)->isPositiveOrZero());
}
Expand Down
Loading

0 comments on commit e175973

Please sign in to comment.