You are here

public function ProductBundleStockProxyKernelTest::setup in Commerce Product Bundle 8

Sets up the the product bundle we need for test.

@todo Try to mock at least parts of it, instead of relying on real objects.

File

modules/commerce_product_bundle_stock/tests/src/Kernel/ProductBundleStockProxyKernelTest.php, line 32

Class

ProductBundleStockProxyKernelTest
Tests the product bundle stock proxy.

Namespace

Drupal\Tests\commerce_product_bundle_stock\Kernel

Code

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);
}