Comparator.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Money\PHPUnit; |
| 4 | |
| 5 | use Money\Currencies\AggregateCurrencies; |
| 6 | use Money\Currencies\BitcoinCurrencies; |
| 7 | use Money\Currencies\ISOCurrencies; |
| 8 | use Money\Formatter\IntlMoneyFormatter; |
| 9 | use Money\Money; |
| 10 | use SebastianBergmann\Comparator\ComparisonFailure; |
| 11 | |
| 12 | /** |
| 13 | * The comparator is for comparing Money objects in PHPUnit tests. |
| 14 | * |
| 15 | * Add this to your bootstrap file: |
| 16 | * |
| 17 | * \SebastianBergmann\Comparator\Factory::getInstance()->register(new \Money\PHPUnit\Comparator()); |
| 18 | */ |
| 19 | final class Comparator extends \SebastianBergmann\Comparator\Comparator |
| 20 | { |
| 21 | /** |
| 22 | * @var IntlMoneyFormatter |
| 23 | */ |
| 24 | private $formatter; |
| 25 | |
| 26 | public function __construct() |
| 27 | { |
| 28 | parent::__construct(); |
| 29 | |
| 30 | $currencies = new AggregateCurrencies([ |
| 31 | new ISOCurrencies(), |
| 32 | new BitcoinCurrencies(), |
| 33 | ]); |
| 34 | |
| 35 | $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); |
| 36 | $this->formatter = new IntlMoneyFormatter($numberFormatter, $currencies); |
| 37 | } |
| 38 | |
| 39 | public function accepts($expected, $actual) |
| 40 | { |
| 41 | return $expected instanceof Money && $actual instanceof Money; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param Money $expected |
| 46 | * @param Money $actual |
| 47 | * @param float $delta |
| 48 | * @param bool $canonicalize |
| 49 | * @param bool $ignoreCase |
| 50 | */ |
| 51 | public function assertEquals( |
| 52 | $expected, |
| 53 | $actual, |
| 54 | $delta = 0.0, |
| 55 | $canonicalize = false, |
| 56 | $ignoreCase = false, |
| 57 | array &$processed = [] |
| 58 | ) { |
| 59 | if (!$expected->equals($actual)) { |
| 60 | throw new ComparisonFailure($expected, $actual, $this->formatter->format($expected), $this->formatter->format($actual), false, 'Failed asserting that two Money objects are equal.'); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |