public function InvoiceTest::testInvoice in Commerce Invoice 8.2
Tests the invoice entity and its methods.
@covers ::getInvoiceNumber @covers ::setInvoiceNumber @covers ::getStore @covers ::setStore @covers ::getStoreId @covers ::setStoreId @covers ::getCustomer @covers ::setCustomer @covers ::getCustomerId @covers ::setCustomerId @covers ::getEmail @covers ::setEmail @covers ::getBillingProfile @covers ::setBillingProfile @covers ::getOrders @covers ::setOrders @covers ::getItems @covers ::setItems @covers ::hasItems @covers ::addItem @covers ::removeItem @covers ::hasItem @covers ::getAdjustments @covers ::setAdjustments @covers ::addAdjustment @covers ::removeAdjustment @covers ::collectAdjustments @covers ::recalculateTotalPrice @covers ::getPaymentMethod @covers ::setPaymentMethod @covers ::getTotalPrice @covers ::getTotalPaid @covers ::setTotalPaid @covers ::getBalance @covers ::isPaid @covers ::getState @covers ::getData @covers ::setData @covers ::unsetData @covers ::getCreatedTime @covers ::setCreatedTime @covers ::getChangedTime @covers ::setChangedTime @covers ::getInvoiceDateTime @covers ::setInvoiceDateTime @covers ::getDueDateTime @covers ::setDueDateTime @covers ::getFile @covers ::setFile
File
- tests/
src/ Kernel/ Entity/ InvoiceTest.php, line 96
Class
- InvoiceTest
- Tests the invoice entity.
Namespace
Drupal\Tests\commerce_invoice\Kernel\EntityCode
public function testInvoice() {
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = Profile::create([
'type' => 'customer',
'address' => [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
],
]);
$profile
->save();
$profile = $this
->reloadEntity($profile);
/** @var \Drupal\commerce_invoice\Entity\InvoiceItemInterface $invoice_item */
$invoice_item = InvoiceItem::create([
'type' => 'commerce_product_variation',
'quantity' => '1',
'unit_price' => new Price('2.00', 'USD'),
]);
$invoice_item
->save();
$invoice_item = $this
->reloadEntity($invoice_item);
/** @var \Drupal\commerce_invoice\Entity\InvoiceItemInterface $another_invoice_item */
$another_invoice_item = InvoiceItem::create([
'type' => 'commerce_product_variation',
'quantity' => '2',
'unit_price' => new Price('3.00', 'USD'),
]);
$another_invoice_item
->save();
$another_invoice_item = $this
->reloadEntity($another_invoice_item);
/** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
$invoice = Invoice::create([
'type' => 'default',
'store_id' => $this->store
->id(),
]);
$invoice
->save();
// Assert that saving a draft invoice didn't automatically generate an
// invoice number.
$this
->assertNull($invoice
->getInvoiceNumber());
$invoice
->setInvoiceNumber(7);
$this
->assertEquals(7, $invoice
->getInvoiceNumber());
$invoice
->setStore($this->store);
$this
->assertEquals($this->store, $invoice
->getStore());
$this
->assertEquals($this->store
->id(), $invoice
->getStoreId());
$invoice
->setStoreId(0);
$this
->assertEquals(NULL, $invoice
->getStore());
$invoice
->setStoreId($this->store
->id());
$this
->assertEquals($this->store, $invoice
->getStore());
$this
->assertEquals($this->store
->id(), $invoice
->getStoreId());
$this
->assertInstanceOf(UserInterface::class, $invoice
->getCustomer());
$this
->assertTrue($invoice
->getCustomer()
->isAnonymous());
$this
->assertEquals(0, $invoice
->getCustomerId());
$invoice
->setCustomer($this->user);
$this
->assertEquals($this->user, $invoice
->getCustomer());
$this
->assertEquals($this->user
->id(), $invoice
->getCustomerId());
$this
->assertTrue($invoice
->getCustomer()
->isAuthenticated());
// Non-existent/deleted user ID.
$invoice
->setCustomerId(888);
$this
->assertInstanceOf(UserInterface::class, $invoice
->getCustomer());
$this
->assertTrue($invoice
->getCustomer()
->isAnonymous());
$this
->assertEquals(888, $invoice
->getCustomerId());
$invoice
->setCustomerId($this->user
->id());
$this
->assertEquals($this->user, $invoice
->getCustomer());
$this
->assertEquals($this->user
->id(), $invoice
->getCustomerId());
$invoice
->setEmail('commerce@example.com');
$this
->assertEquals('commerce@example.com', $invoice
->getEmail());
$invoice
->setBillingProfile($profile);
$this
->assertEquals($profile, $invoice
->getBillingProfile());
$order = Order::create([
'type' => 'default',
'state' => 'completed',
]);
$order
->save();
$order = $this
->reloadEntity($order);
$invoice
->setOrders([
$order,
]);
$this
->assertEquals([
$order,
], $invoice
->getOrders());
$invoice
->setItems([
$invoice_item,
$another_invoice_item,
]);
$this
->assertEquals([
$invoice_item,
$another_invoice_item,
], $invoice
->getItems());
$this
->assertNotEmpty($invoice
->hasItems());
$invoice
->removeItem($another_invoice_item);
$this
->assertEquals([
$invoice_item,
], $invoice
->getItems());
$this
->assertNotEmpty($invoice
->hasItem($invoice_item));
$this
->assertEmpty($invoice
->hasItem($another_invoice_item));
$invoice
->addItem($another_invoice_item);
$this
->assertEquals([
$invoice_item,
$another_invoice_item,
], $invoice
->getItems());
$this
->assertNotEmpty($invoice
->hasItem($another_invoice_item));
$this
->assertEquals(new Price('8.00', 'USD'), $invoice
->getTotalPrice());
$adjustments = [];
$adjustments[] = new Adjustment([
'type' => 'custom',
'label' => '10% off',
'amount' => new Price('-1.00', 'USD'),
]);
$adjustments[] = new Adjustment([
'type' => 'fee',
'label' => 'Handling fee',
'amount' => new Price('10.00', 'USD'),
'locked' => TRUE,
]);
$invoice
->addAdjustment($adjustments[0]);
$invoice
->addAdjustment($adjustments[1]);
$this
->assertEquals($adjustments, $invoice
->getAdjustments());
$this
->assertEquals($adjustments, $invoice
->getAdjustments([
'custom',
'fee',
]));
$this
->assertEquals([
$adjustments[0],
], $invoice
->getAdjustments([
'custom',
]));
$this
->assertEquals([
$adjustments[1],
], $invoice
->getAdjustments([
'fee',
]));
$invoice
->removeAdjustment($adjustments[0]);
$this
->assertEquals(new Price('8.00', 'USD'), $invoice
->getSubtotalPrice());
$this
->assertEquals(new Price('18.00', 'USD'), $invoice
->getTotalPrice());
$this
->assertEquals([
$adjustments[1],
], $invoice
->getAdjustments());
$invoice
->setAdjustments($adjustments);
$this
->assertEquals($adjustments, $invoice
->getAdjustments());
$this
->assertEquals(new Price('17.00', 'USD'), $invoice
->getTotalPrice());
$this
->assertEquals($adjustments, $invoice
->collectAdjustments());
$this
->assertEquals($adjustments, $invoice
->collectAdjustments([
'custom',
'fee',
]));
$this
->assertEquals([
$adjustments[0],
], $invoice
->collectAdjustments([
'custom',
]));
$this
->assertEquals([
$adjustments[1],
], $invoice
->collectAdjustments([
'fee',
]));
$invoice
->setPaymentMethod('Payment by invoice');
$this
->assertEquals('Payment by invoice', $invoice
->getPaymentMethod());
$this
->assertEquals(new Price('0', 'USD'), $invoice
->getTotalPaid());
$this
->assertEquals(new Price('17.00', 'USD'), $invoice
->getBalance());
$this
->assertFalse($invoice
->isPaid());
$invoice
->setTotalPaid(new Price('7.00', 'USD'));
$this
->assertEquals(new Price('7.00', 'USD'), $invoice
->getTotalPaid());
$this
->assertEquals(new Price('10.00', 'USD'), $invoice
->getBalance());
$this
->assertFalse($invoice
->isPaid());
$invoice
->setTotalPaid(new Price('17.00', 'USD'));
$this
->assertEquals(new Price('17.00', 'USD'), $invoice
->getTotalPaid());
$this
->assertEquals(new Price('0', 'USD'), $invoice
->getBalance());
$this
->assertTrue($invoice
->isPaid());
$invoice
->setTotalPaid(new Price('27.00', 'USD'));
$this
->assertEquals(new Price('27.00', 'USD'), $invoice
->getTotalPaid());
$this
->assertEquals(new Price('-10.00', 'USD'), $invoice
->getBalance());
$this
->assertTrue($invoice
->isPaid());
$this
->assertEquals('draft', $invoice
->getState()
->getId());
$this
->assertEquals('default', $invoice
->getData('test', 'default'));
$invoice
->setData('test', 'value');
$this
->assertEquals('value', $invoice
->getData('test', 'default'));
$invoice
->unsetData('test');
$this
->assertNull($invoice
->getData('test'));
$this
->assertEquals('default', $invoice
->getData('test', 'default'));
$invoice
->setCreatedTime(635879700);
$this
->assertEquals(635879700, $invoice
->getCreatedTime());
$invoice
->setChangedTime(635879800);
$this
->assertEquals(635879800, $invoice
->getChangedTime());
$invoice
->setInvoiceDateTime(635879900);
$this
->assertEquals(635879900, $invoice
->getInvoiceDateTime());
$invoice
->setDueDateTime(635879950);
$this
->assertEquals(635879950, $invoice
->getDueDateTime());
$this
->assertNull($invoice
->getFile());
$file = File::create([
'uri' => 'public://invoice.pdf',
'filename' => 'invoice.pdf',
]);
$file
->save();
$file = $this
->reloadEntity($file);
$invoice
->setFile($file);
$this
->assertEquals($invoice
->getFile(), $file);
}