/** * @dataProvider comparisonProvider * @covers ::compare * * @param mixed $a * @param mixed $b * @param int $expected * @param int $scale */ public function testCanCompare($a, $b, int $expected, int $scale = null) { $decimalA = Decimal::create($a); // This is to ensure we test the path where the same instance is used for comparison. In that case there // is an optimisation which returns 0 without any further checking. This is really an implementation detail // that cannot be explicitly tested, but this allows full coverage. $decimalB = $a === $b ? $decimalA : Decimal::create($b); $result = $decimalA->compare($decimalB, $scale); // Integer is not compared directly Decimal Ratio::compare() only guarantees a return of <=> 0. if ($expected === -1) { self::assertLessThan(0, $result); } elseif ($expected === 1) { self::assertGreaterThan(0, $result); } else { self::assertSame(0, $result); } }
/** * @dataProvider roundProvider * @covers ::round * * @param mixed $decimal * @param int $decimalPlaces * @param string $expected */ public function testCanRound($decimal, int $decimalPlaces, string $expected) { $decimal = Decimal::create($decimal); $rounded = $decimal->round($decimalPlaces); self::assertSame($expected, $rounded->toString()); }
/** * @dataProvider absProvider * @covers ::abs * * @param mixed $decimal * @param string $expected */ public function testCanGetAbsoluteValue($decimal, string $expected) { $decimal = Decimal::create($decimal); $absolute = $decimal->abs(); self::assertSame($expected, $absolute->toString()); }
/** * @dataProvider ratioToExpectedDecimalProvider * @covers ::create * * @param string $ratio * @param string $decimal */ public function testCreateDelegatesToStaticRatioFactory(string $ratio, string $decimal, int $scale = null) { $decimal = Decimal::create($decimal, $scale); $ratio = Ratio::fromString($ratio); self::assertTrue($decimal->equals(Decimal::fromRatio($ratio, $scale))); }