You are here

public function InvoiceTest::testPaidTransition in Commerce Invoice 8.2

Tests that the paid transition is applied when the balance reaches zero.

File

tests/src/Kernel/Entity/InvoiceTest.php, line 358

Class

InvoiceTest
Tests the invoice entity.

Namespace

Drupal\Tests\commerce_invoice\Kernel\Entity

Code

public function testPaidTransition() {

  /** @var \Drupal\commerce_invoice\Entity\InvoiceItemInterface $invoice_item */
  $invoice_item = InvoiceItem::create([
    'type' => 'commerce_product_variation',
    'quantity' => '2',
    'unit_price' => new Price('10.00', 'USD'),
  ]);
  $invoice_item
    ->save();

  /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
  $invoice = Invoice::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'invoice_items' => [
      $invoice_item,
    ],
  ]);
  $invoice
    ->save();
  $this
    ->assertNull($invoice
    ->getData('invoice_test_called'));
  $this
    ->assertEquals('draft', $invoice
    ->getState()
    ->getId());
  $invoice
    ->setTotalPaid(new Price('20.00', 'USD'));
  $invoice
    ->save();
  $this
    ->assertEquals(1, $invoice
    ->getData('invoice_test_called'));
  $this
    ->assertEquals('paid', $invoice
    ->getState()
    ->getId());

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

  // Confirm that the event is dispatched for invoices created as paid.
  $another_invoice = Invoice::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'invoice_items' => [
      $invoice_item,
    ],
    'total_paid' => new Price('20.00', 'USD'),
  ]);
  $another_invoice
    ->save();
  $this
    ->assertEquals(1, $another_invoice
    ->getData('invoice_test_called'));
}