You are here

protected function ProductAttributesOverviewFormatterTest::setUp in Commerce Core 8.2

Overrides CommerceKernelTestBase::setUp

File

modules/product/tests/src/Kernel/ProductAttributesOverviewFormatterTest.php, line 52

Class

ProductAttributesOverviewFormatterTest
Tests the "commerce_product_attributes_overview" formatter.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installEntitySchema('commerce_product_attribute');
  $this
    ->installEntitySchema('commerce_product_attribute_value');
  $this
    ->installEntitySchema('commerce_product_variation');
  $this
    ->installEntitySchema('commerce_product');
  $this
    ->installConfig([
    'commerce_product',
  ]);
  $this->productDefaultDisplay = commerce_get_entity_display('commerce_product', 'default', 'view');
  $this->attributeDefaultDisplay = commerce_get_entity_display('commerce_product_attribute_value', 'color', 'view');
  $this->productViewBuilder = $this->container
    ->get('entity_type.manager')
    ->getViewBuilder('commerce_product');
  $attribute = ProductAttribute::create([
    'id' => 'color',
    'label' => 'Color',
  ]);
  $attribute
    ->save();
  $this->container
    ->get('commerce_product.attribute_field_manager')
    ->createField($attribute, 'default');
  EntityViewMode::create([
    'id' => 'commerce_product.catalog',
    'label' => 'Catalog',
    'targetEntityType' => 'commerce_product',
  ])
    ->save();
  $this->container
    ->get('router.builder')
    ->rebuildIfNeeded();
  $attribute_values = [];
  $attribute_values['cyan'] = ProductAttributeValue::create([
    'attribute' => $attribute
      ->id(),
    'name' => 'Cyan',
  ]);
  $attribute_values['cyan']
    ->save();
  $attribute_values['yellow'] = ProductAttributeValue::create([
    'attribute' => $attribute
      ->id(),
    'name' => 'Yellow',
  ]);
  $attribute_values['yellow']
    ->save();
  $this->attributeDefaultDisplay
    ->setComponent('name', [
    'label' => 'above',
  ]);
  $this->attributeDefaultDisplay
    ->save();
  $variation1 = ProductVariation::create([
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => 999,
      'currency_code' => 'USD',
    ],
    'attribute_color' => $attribute_values['cyan'],
  ]);
  $variation2 = ProductVariation::create([
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => 999,
      'currency_code' => 'USD',
    ],
    'attribute_color' => $attribute_values['yellow'],
  ]);
  $variation3 = ProductVariation::create([
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => 999,
      'currency_code' => 'USD',
    ],
    'attribute_color' => $attribute_values['yellow'],
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $this->product = Product::create([
    'type' => 'default',
    'title' => 'My product',
    'variations' => [
      $variation1,
      $variation2,
      $variation3,
    ],
  ]);
  $this->product
    ->save();
}