You are here

public function ProductVariationAttributeMapperTest::testSelect in Commerce Core 8.2

Tests selecting a variation.

@covers ::selectVariation

File

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

Class

ProductVariationAttributeMapperTest
Tests the product variation attribute mapper.

Namespace

Drupal\Tests\commerce_product\Kernel

Code

public function testSelect() {
  $product = $this
    ->generateThreeByTwoScenario();
  $variations = $product
    ->getVariations();

  // No attribute values.
  $selected_variation = $this->mapper
    ->selectVariation($product
    ->getVariations());
  $this
    ->assertNull($selected_variation);

  // Empty attribute values.
  $selected_variation = $this->mapper
    ->selectVariation($product
    ->getVariations(), [
    'attribute_color' => '',
    'attribute_size' => '',
  ]);
  $this
    ->assertNull($selected_variation);

  // Missing first attribute.
  $selected_variation = $this->mapper
    ->selectVariation($variations, [
    'attribute_color' => '',
    'attribute_size' => $this->sizeAttributes['large']
      ->id(),
  ]);
  $this
    ->assertNull($selected_variation);

  // Single attribute value.
  $selected_variation = $this->mapper
    ->selectVariation($variations, [
    'attribute_color' => $this->colorAttributes['blue']
      ->id(),
  ]);
  $this
    ->assertEquals($variations[3]
    ->id(), $selected_variation
    ->id());
  $selected_variation = $this->mapper
    ->selectVariation($variations, [
    'attribute_size' => $this->sizeAttributes['large']
      ->id(),
  ]);
  $this
    ->assertEquals($variations[2]
    ->id(), $selected_variation
    ->id());

  // Two attribute values.
  $selected_variation = $this->mapper
    ->selectVariation($variations, [
    'attribute_color' => $this->colorAttributes['red']
      ->id(),
    'attribute_size' => $this->sizeAttributes['large']
      ->id(),
  ]);
  $this
    ->assertEquals($variations[2]
    ->id(), $selected_variation
    ->id());

  // Invalid attribute combination.
  $selected_variation = $this->mapper
    ->selectVariation($variations, [
    'attribute_color' => $this->colorAttributes['blue']
      ->id(),
    'attribute_size' => $this->sizeAttributes['large']
      ->id(),
  ]);
  $this
    ->assertEquals($variations[3]
    ->id(), $selected_variation
    ->id());
  $this
    ->assertEquals('Blue', $selected_variation
    ->getAttributeValue('attribute_color')
    ->label());
  $this
    ->assertEquals('Small', $selected_variation
    ->getAttributeValue('attribute_size')
    ->label());
}