You are here

protected function ProductVariationAttributeMapperTest::generateThreeByTwoScenario in Commerce Core 8.2

Generates a three by two scenario.

There are three sizes and two colors. Missing one color option. Generated product variations: [ RS, RM, RL ] [ BS, BM, X ]

Return value

\Drupal\commerce_product\Entity\ProductInterface The product.

2 calls to ProductVariationAttributeMapperTest::generateThreeByTwoScenario()
ProductVariationAttributeMapperTest::testPrepareAttributes in modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php
Tests preparing attributes.
ProductVariationAttributeMapperTest::testSelect in modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php
Tests selecting a variation.

File

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

Class

ProductVariationAttributeMapperTest
Tests the product variation attribute mapper.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

protected function generateThreeByTwoScenario() {
  $product = Product::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [],
  ]);
  $attribute_values_matrix = [
    [
      'red',
      'small',
    ],
    [
      'red',
      'medium',
    ],
    [
      'red',
      'large',
    ],
    [
      'blue',
      'small',
    ],
    [
      'blue',
      'medium',
    ],
  ];
  $variations = [];
  foreach ($attribute_values_matrix as $key => $value) {
    $variation = ProductVariation::create([
      'type' => 'default',
      'sku' => $this
        ->randomMachineName(),
      'price' => [
        'number' => 999,
        'currency_code' => 'USD',
      ],
      'attribute_color' => $this->colorAttributes[$value[0]],
      'attribute_size' => $this->sizeAttributes[$value[1]],
    ]);
    $variation
      ->save();
    $variations[] = $variation;
    $product
      ->addVariation($variation);
  }
  $product
    ->save();
  return $product;
}