You are here

public function OrderTest::testMultipleCurrencies in Commerce Core 8.2

Tests the order with order items using different currencies.

@covers ::getSubtotalPrice @covers ::recalculateTotalPrice @covers ::getTotalPrice

File

modules/order/tests/src/Kernel/Entity/OrderTest.php, line 563

Class

OrderTest
Tests the Order entity.

Namespace

Drupal\Tests\commerce_order\Kernel\Entity

Code

public function testMultipleCurrencies() {
  $currency_importer = $this->container
    ->get('commerce_price.currency_importer');
  $currency_importer
    ->import('EUR');
  $usd_order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => '1',
    'unit_price' => new Price('2.00', 'USD'),
  ]);
  $usd_order_item
    ->save();
  $eur_order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => '1',
    'unit_price' => new Price('3.00', 'EUR'),
  ]);
  $eur_order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'state' => 'completed',
  ]);
  $order
    ->save();

  // The order currency should match the currency of the first order item.
  $this
    ->assertNull($order
    ->getTotalPrice());
  $order
    ->addItem($usd_order_item);
  $this
    ->assertEquals($usd_order_item
    ->getTotalPrice(), $order
    ->getTotalPrice());

  // Replacing the order item should replace the order total and its currency.
  $order
    ->removeItem($usd_order_item);
  $order
    ->addItem($eur_order_item);
  $this
    ->assertEquals($eur_order_item
    ->getTotalPrice(), $order
    ->getTotalPrice());

  // Adding a second order item with a different currency should fail.
  $currency_mismatch = FALSE;
  try {
    $order
      ->addItem($usd_order_item);
  } catch (CurrencyMismatchException $e) {
    $currency_mismatch = TRUE;
  }
  $this
    ->assertTrue($currency_mismatch);
}