You are here

public function OrderLockingTest::testOrderVersionMismatchException in Commerce Core 8.2

Tests exception is thrown on save.

File

modules/order/tests/src/Kernel/OrderLockingTest.php, line 86

Class

OrderLockingTest
Tests order locking.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testOrderVersionMismatchException() {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = Order::create([
    'type' => 'default',
    'mail' => $this->user
      ->getEmail(),
    'uid' => $this->user
      ->id(),
    'store_id' => $this->store
      ->id(),
  ]);
  $order
    ->save();
  $this
    ->assertEquals(1, $order
    ->getVersion());
  (function ($order_id) {
    $order = Order::load($order_id);
    assert($order instanceof OrderInterface);
    $order
      ->addItem(OrderItem::create([
      'type' => 'test',
      'quantity' => 1,
      'unit_price' => new Price('12.00', 'USD'),
    ]));
    $order
      ->save();
    $this
      ->assertEquals(2, $order
      ->getVersion());
  })($order
    ->id());
  try {
    $order
      ->save();
    $this
      ->fail('Expected OrderVersionMismatchException exception');
  } catch (EntityStorageException $e) {
    $this
      ->assertEquals(sprintf('Attempted to save order %s with version %s. Current version is %s.', $order
      ->id(), $order
      ->getVersion(), $order->original
      ->getVersion()), $e
      ->getMessage());
    $original = $e
      ->getPrevious();
    $this
      ->assertInstanceOf(OrderVersionMismatchException::class, $original);
  }
}