You are here

public function InvoiceGeneratorTest::testGenerate in Commerce Invoice 8.2

Tests generating invoices.

@covers ::generate

File

tests/src/Kernel/InvoiceGeneratorTest.php, line 111

Class

InvoiceGeneratorTest
Tests the invoice generator service.

Namespace

Drupal\Tests\commerce_invoice\Kernel

Code

public function testGenerate() {
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 1,
    'unit_price' => new Price('12.00', 'USD'),
  ]);
  $order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'state' => 'completed',
    'store_id' => $this->store,
    'billing_profile' => $this->profile,
    'uid' => $this->user
      ->id(),
    'order_items' => [
      $order_item,
    ],
    'adjustments' => [
      new Adjustment([
        'type' => 'custom',
        'label' => '10% off',
        'amount' => new Price('-1.20', 'USD'),
        'percentage' => '0.1',
      ]),
    ],
    'total_paid' => new Price('5.00', 'USD'),
  ]);
  $order
    ->save();
  $order = $this
    ->reloadEntity($order);
  $another_order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 3,
    'unit_price' => new Price('10.00', 'USD'),
  ]);
  $another_order_item
    ->save();
  $another_order = Order::create([
    'type' => 'default',
    'state' => 'completed',
    'store_id' => $this->store,
    'uid' => $this->user
      ->id(),
    'billing_profile' => $this->profile,
    'order_items' => [
      $another_order_item,
    ],
    'adjustments' => [
      new Adjustment([
        'type' => 'fee',
        'label' => 'Random fee',
        'amount' => new Price('2.00', 'USD'),
      ]),
    ],
  ]);
  $another_order
    ->save();
  $another_order = $this
    ->reloadEntity($another_order);
  $invoice = $this->invoiceGenerator
    ->generate([
    $order,
    $another_order,
  ], $this->store, $this->profile, [
    'uid' => $this->user
      ->id(),
  ]);
  $invoice_billing_profile = $invoice
    ->getBillingProfile();
  $this
    ->assertNotEmpty($invoice
    ->getBillingProfile());
  $this
    ->assertTrue($this->profile
    ->equalToProfile($invoice_billing_profile));
  $this
    ->assertEquals($this->user
    ->id(), $invoice
    ->getCustomerId());
  $this
    ->assertEquals([
    $order,
    $another_order,
  ], $invoice
    ->getOrders());
  $this
    ->assertEquals($this->store, $invoice
    ->getStore());
  $this
    ->assertEquals(new Price('42.8', 'USD'), $invoice
    ->getTotalPrice());
  $this
    ->assertCount(2, $invoice
    ->getItems());
  $this
    ->assertCount(2, $invoice
    ->getAdjustments());
  $this
    ->assertEquals(new Price('5.00', 'USD'), $invoice
    ->getTotalPaid());
}