You are here

public function AdjustmentTransformerTest::testRounding in Commerce Core 8.2

Tests adjustment rounding.

@covers ::roundAdjustments @covers ::roundAdjustment

File

modules/order/tests/src/Kernel/AdjustmentTransformerTest.php, line 124

Class

AdjustmentTransformerTest
Tests the adjustment transformer.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testRounding() {
  $first_adjustment = new Adjustment([
    'type' => 'tax',
    'label' => 'VAT',
    'amount' => new Price('10.489', 'USD'),
    'percentage' => '0.1',
  ]);
  $second_adjustment = new Adjustment([
    'type' => 'promotion',
    'label' => '20% off',
    'amount' => new Price('20.555', 'USD'),
  ]);
  $first_rounded_adjustment = new Adjustment([
    'type' => 'tax',
    'label' => 'VAT',
    'amount' => new Price('10.49', 'USD'),
    'percentage' => '0.1',
  ]);
  $second_rounded_adjustment = new Adjustment([
    'type' => 'promotion',
    'label' => '20% off',
    'amount' => new Price('20.56', 'USD'),
  ]);
  $second_rounded_down_adjustment = new Adjustment([
    'type' => 'promotion',
    'label' => '20% off',
    'amount' => new Price('20.55', 'USD'),
  ]);
  $adjustments = $this->adjustmentTransformer
    ->roundAdjustments([
    $first_adjustment,
    $second_adjustment,
  ]);
  $this
    ->assertEquals([
    $first_rounded_adjustment,
    $second_rounded_adjustment,
  ], $adjustments);
  $adjustment = $this->adjustmentTransformer
    ->roundAdjustment($first_adjustment);
  $this
    ->assertEquals($first_rounded_adjustment, $adjustment);
  $adjustment = $this->adjustmentTransformer
    ->roundAdjustment($second_adjustment);
  $this
    ->assertEquals($second_rounded_adjustment, $adjustment);

  // Confirm that the $mode is passed along.
  $adjustments = $this->adjustmentTransformer
    ->roundAdjustments([
    $second_adjustment,
  ], PHP_ROUND_HALF_DOWN);
  $this
    ->assertEquals([
    $second_rounded_down_adjustment,
  ], $adjustments);
  $adjustment = $this->adjustmentTransformer
    ->roundAdjustment($second_adjustment, PHP_ROUND_HALF_DOWN);
  $this
    ->assertEquals($second_rounded_down_adjustment, $adjustment);
}