You are here

protected function ProductVariationAttributeMapperTest::generateThreeByTwoOptionalScenario in Commerce Core 8.2

Generates a three by two (optional) scenario.

Generated product variations: [ 8GBx1TB, X , X ] [ X , 16GBx1TB , X ] [ X , 16GBx1TBx1TB, X ]

Return value

\Drupal\commerce_product\Entity\ProductInterface The product.

2 calls to ProductVariationAttributeMapperTest::generateThreeByTwoOptionalScenario()
ProductVariationAttributeMapperTest::testPrepareAttributesOptional in modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php
Tests preparing attributes when there are optional attributes.
ProductVariationAttributeMapperTest::testSelectWithOptionalAttributes in modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php
Tests selecting a variation when there are optional attributes.

File

modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php, line 599

Class

ProductVariationAttributeMapperTest
Tests the product variation attribute mapper.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

protected function generateThreeByTwoOptionalScenario() {
  $product = Product::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [],
  ]);
  $attribute_values_matrix = [
    [
      '8gb',
      '1tb',
      '',
    ],
    [
      '16gb',
      '1tb',
      '',
    ],
    [
      '16gb',
      '1tb',
      '1tb',
    ],
  ];
  $variations = [];
  foreach ($attribute_values_matrix as $key => $value) {
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => $this
        ->randomMachineName(),
      'price' => [
        'number' => 999,
        'currency_code' => 'USD',
      ],
      'attribute_ram' => $this->ramAttributes[$value[0]],
      'attribute_disk1' => $this->disk1Attributes[$value[1]],
      'attribute_disk2' => isset($this->disk2Attributes[$value[2]]) ? $this->disk2Attributes[$value[2]] : NULL,
    ]);
    $variation
      ->save();
    $variations[] = $variation;
    $product
      ->addVariation($variation);
  }
  $product
    ->save();
  return $product;
}