You are here

public function OrderTest::testPaidEvent in Commerce Core 8.2

Tests that the paid event is dispatched when the balance reaches zero.

File

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

Class

OrderTest
Tests the Order entity.

Namespace

Drupal\Tests\commerce_order\Kernel\Entity

Code

public function testPaidEvent() {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => '2',
    'unit_price' => new Price('10.00', 'USD'),
  ]);
  $order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $order_item,
    ],
    'state' => 'draft',
  ]);
  $order
    ->save();
  $this
    ->assertNull($order
    ->getData('order_test_called'));
  $order
    ->setTotalPaid(new Price('20.00', 'USD'));
  $order
    ->save();
  $this
    ->assertEquals(1, $order
    ->getData('order_test_called'));

  // Confirm that the event is not dispatched the second time the balance
  // reaches zero.
  $order
    ->setTotalPaid(new Price('10.00', 'USD'));
  $order
    ->save();
  $order
    ->setTotalPaid(new Price('20.00', 'USD'));
  $order
    ->save();
  $this
    ->assertEquals(1, $order
    ->getData('order_test_called'));

  // Confirm that the event is dispatched for orders created as paid.
  $another_order = Order::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'order_items' => [
      $order_item,
    ],
    'total_paid' => new Price('20.00', 'USD'),
    'state' => 'draft',
  ]);
  $another_order
    ->save();
  $this
    ->assertEquals(1, $another_order
    ->getData('order_test_called'));
}