ProductBundleStockProxyKernelTest.php in Commerce Product Bundle 8
File
modules/commerce_product_bundle_stock/tests/src/Kernel/ProductBundleStockProxyKernelTest.php
View source
<?php
namespace Drupal\Tests\commerce_product_bundle_stock\Kernel;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product_bundle\Entity\ProductBundle;
use Drupal\commerce_product_bundle\Entity\ProductBundleItem;
use Drupal\commerce_product_bundle_stock\ProductBundleStockProxy;
class ProductBundleStockProxyKernelTest extends ProductBundleStockKernelTestBase {
protected $bundle;
public function setup() {
parent::setup();
$variations = [];
for ($i = 1; $i <= 5; $i++) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => $i % 2,
]);
$variation
->save();
$variations[] = $variation;
}
$variations = array_reverse($variations);
$product = Product::create([
'type' => 'default',
'variations' => $variations,
]);
$product
->save();
$product1 = $this
->reloadEntity($product);
$variations = [];
for ($i = 1; $i <= 3; $i++) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => TRUE,
]);
$variation
->save();
$variations[] = $variation;
}
$variations = array_reverse($variations);
$product = Product::create([
'type' => 'default',
'variations' => $variations,
]);
$product
->save();
$product2 = $this
->reloadEntity($product);
$bundleItem1 = ProductBundleItem::create([
'type' => 'default',
'uid' => $this->user
->id(),
'title' => 'testBundle1',
'status' => TRUE,
]);
$bundleItem1
->setProduct($product1);
$bundleItem1
->save();
$bundleItem1 = $this
->reloadEntity($bundleItem1);
$bundleItem2 = ProductBundleItem::create([
'type' => 'default',
'uid' => $this->user
->id(),
'title' => 'testBundle2',
'status' => TRUE,
]);
$bundleItem2
->setProduct($product2);
$bundleItem2
->save();
$bundleItem2 = $this
->reloadEntity($bundleItem2);
$bundle = ProductBundle::create([
'type' => 'default',
'uid' => $this->user
->id(),
'status' => TRUE,
]);
$bundle
->setBundleItems([
$bundleItem1,
$bundleItem2,
]);
$bundle
->save();
$this->bundle = $this
->reloadEntity($bundle);
}
public function testProductBundleStockProxy() {
$stockServiceManager = \Drupal::service('commerce_stock.service_manager');
$proxy = new ProductBundleStockProxy($stockServiceManager);
$this
->assertTrue($proxy
->getIsStockManaged($this->bundle));
$this
->assertTrue($proxy
->getIsAlwaysInStock($this->bundle));
$this
->assertTrue($proxy
->getIsInStock($this->bundle, []));
}
}