View source
<?php
namespace Drupal\Tests\commerce_product_bundle\Kernel\Entity;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product_bundle\Entity\BundleItemOrderItem;
use Drupal\commerce_product_bundle\Entity\ProductBundle;
use Drupal\commerce_product_bundle\Entity\ProductBundleItem;
use Drupal\Tests\commerce_product_bundle\Kernel\CommerceProductBundleKernelTestBase;
class BundleItemOrderItemTest extends CommerceProductBundleKernelTestBase {
protected $productBundle;
protected $productVariation;
protected $productBundleItem;
protected $orderItem;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('cpb_order_item');
$variation = ProductVariation::create([
'type' => 'default',
]);
$variation
->save();
$this->productVariation = $variation;
$bundleItem = ProductBundleItem::create([
'type' => 'default',
'variations' => [
$variation,
],
]);
$bundleItem
->save();
$this->productBundleItem = $bundleItem;
$productBundle = ProductBundle::create([
'type' => 'default',
'bundle_items' => [
$bundleItem,
],
]);
$productBundle
->save();
$this->productBundle = $productBundle;
$this->orderItem = OrderItem::create([
'type' => 'commerce_product_bundle_default',
]);
}
public function testBundleItemOrderItem() {
$created = new \DateTime('now');
$bundleItemOrderItem = BundleItemOrderItem::create([
'title' => 'TestTitle',
'created' => $created
->getTimestamp(),
'bundle_item' => $this->productBundleItem,
'purchased_entity' => $this->productVariation,
'unit_price' => new Price('222.33', 'USD'),
'quantity' => '3',
'total_price' => new Price('444.66', 'USD'),
]);
self::assertEquals(new Price('444.66', 'USD'), $bundleItemOrderItem
->getTotalPrice());
$bundleItemOrderItem
->save();
$bundleItemOrderItem = $this
->reloadEntity($bundleItemOrderItem);
self::assertEquals(new Price('666.99', 'USD'), $bundleItemOrderItem
->getTotalPrice());
self::assertEquals('TestTitle', $bundleItemOrderItem
->getTitle());
$priceToTest = $bundleItemOrderItem
->getUnitPrice();
self::assertEquals(new Price('222.33', 'USD'), $priceToTest);
self::assertEquals('222.33', $priceToTest
->getNumber());
self::assertEquals('USD', $priceToTest
->getCurrencyCode());
$price = new Price('55.55', 'USD');
$bundleItemOrderItem
->setUnitPrice($price);
self::assertEquals($price, $bundleItemOrderItem
->getUnitPrice());
self::assertEquals('55.55', $price
->getNumber());
self::assertEquals('USD', $price
->getCurrencyCode());
self::assertEquals(new Price('166.65', 'USD'), $bundleItemOrderItem
->getTotalPrice());
self::assertEquals($created
->getTimestamp(), $bundleItemOrderItem
->getCreatedTime());
self::assertEquals('3', $bundleItemOrderItem
->getQuantity());
self::assertEquals($this->productVariation
->id(), $bundleItemOrderItem
->getPurchasedEntityId());
self::assertEquals($this->productBundleItem
->id(), $bundleItemOrderItem
->getBundleItem()
->id());
}
public function testBundleItemOrderItemOnOrderItem() {
$orderItem = $this->orderItem;
self::assertEquals('commerce_product_bundle_default', $this->orderItem
->bundle());
self::assertTrue($orderItem
->hasField('bundle_item_order_items'));
$bundleItemOrderItem0 = BundleItemOrderItem::create([
'title' => $this
->randomString(),
'bundle_item' => $this->productBundleItem,
'purchased_entity' => $this->productVariation,
'unit_price' => new Price('22.33', 'USD'),
'quantity' => '3',
'total_price' => new Price('66.99', 'USD'),
]);
$bundleItemOrderItem1 = BundleItemOrderItem::create([
'title' => $this
->randomString(),
'bundle_item' => $this->productBundleItem,
'purchased_entity' => $this->productVariation,
'unit_price' => new Price('11.11', 'USD'),
'quantity' => '1',
'total_price' => new Price('22.22', 'USD'),
]);
$orderItem
->set('bundle_item_order_items', [
$bundleItemOrderItem0,
$bundleItemOrderItem1,
]);
$orderItem
->set('purchased_entity', $this->productBundle);
$orderItem
->save();
$orderItem = $this
->reloadEntity($orderItem);
$bundleItemOrderItems = $orderItem
->get('bundle_item_order_items')
->referencedEntities();
self::assertEquals('3', $bundleItemOrderItems[0]
->getQuantity());
self::assertEquals('1', $bundleItemOrderItems[1]
->getQuantity());
$orderItemId = $orderItem
->id();
self::assertEquals($orderItemId, $bundleItemOrderItems[0]
->getOrderItemId());
self::assertEquals($orderItemId, $bundleItemOrderItems[1]
->getOrderItemId());
self::assertEquals('11.11', $bundleItemOrderItems[1]
->getTotalPrice()
->getNumber());
$orderItem
->delete();
$bundleItemOrderItem0Exists = (bool) BundleItemOrderItem::load($bundleItemOrderItems[0]
->id());
$bundleItemOrderItem1Exists = (bool) BundleItemOrderItem::load($bundleItemOrderItems[1]
->id());
$this
->assertEmpty($bundleItemOrderItem0Exists, 'The bundle item order item 0 has been deleted from database.');
$this
->assertEmpty($bundleItemOrderItem1Exists, 'The bundle item order item 1 has been deleted from database.');
}
}