You are here

public function CommerceInvoiceTestCase::testInvoiceCreationAndPayment in Commerce Invoice 7.2

Tests creation and payment of an Invoice.

File

tests/commerce_invoice.test, line 58
Functional tests for the commerce invoice module.

Class

CommerceInvoiceTestCase
Test commerce invoices.

Code

public function testInvoiceCreationAndPayment() {

  // Create an order with two products, total quantity 7, total price $450
  $order = $this
    ->createDummyOrder(1, [
    $this->product100->product_id => 2,
    $this->product50->product_id => 5,
  ], 'pending');

  // Rule should have created a pending invoice.
  $invoice = commerce_invoice_load_current($order);
  $this
    ->assertEqual($invoice->invoice_status, Invoice::STATUS_PENDING, 'Invoices for new orders are pending by default.');

  // The invoice should total $450.
  $total = $invoice
    ->wrapper()->commerce_invoice_total->amount
    ->value();
  $this
    ->assertEqual($total, 450, 'Invoice total properly calculated.');

  // Paid invoices should trigger a rule to complete order.
  $invoice->invoice_status = Invoice::STATUS_PAID;
  $invoice
    ->save();
  $order = commerce_order_load($invoice->order_id);
  $this
    ->assertEqual($order->status, 'completed', 'Paid invoice changed order status to completed.');

  // Update a completed order.
  $order->status = 'pending';
  $this
    ->addLineItemToOrder($order);
  $invoices = commerce_invoice_load_for_order($order);

  // Old invoice should be refunded.
  $oldInvoice = reset($invoices);
  $this
    ->assertEqual($oldInvoice->invoice_status, Invoice::STATUS_REFUND_PENDING, 'Flag old invoice for refunding after change of order.');

  // New invoice should have a pending status.
  $newInvoice = next($invoices);
  $this
    ->assertEqual($newInvoice->invoice_status, Invoice::STATUS_PENDING, 'Create a new pending invoice after change of order.');

  // The new invoice total should be $950.
  $total = $newInvoice
    ->wrapper()->commerce_invoice_total->amount
    ->value();
  $this
    ->assertEqual($total, 950, 'Updated invoice total properly calculated.');
}