public function ProductVariationAttributeMapperTest::testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo in Commerce Core 8.2
Tests preparing attributes when the values are mutually exclusive.
@covers ::prepareAttributes
File
- modules/
product/ tests/ src/ Kernel/ ProductVariationAttributeMapperTest.php, line 328
Class
- ProductVariationAttributeMapperTest
- Tests the product variation attribute mapper.
Namespace
Drupal\Tests\commerce_product\KernelCode
public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
$product = Product::create([
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [],
]);
$attribute_values_matrix = [
[
'4gb',
'2tb',
'2tb',
],
[
'8gb',
'1tb',
'2tb',
],
[
'8gb',
'2tb',
'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();
// Test from the initial variation.
$attributes = $this->mapper
->prepareAttributes(reset($variations), $variations);
$ram_attribute = $attributes['attribute_ram'];
$this
->assertEquals('ram', $ram_attribute
->getId());
$this
->assertEquals('Ram', $ram_attribute
->getLabel());
$this
->assertEquals('select', $ram_attribute
->getElementType());
$this
->assertTrue($ram_attribute
->isRequired());
$this
->assertEquals([
'11' => '8GB',
'10' => '4GB',
], $ram_attribute
->getValues());
$disk1_attribute = $attributes['attribute_disk1'];
$this
->assertEquals('disk1', $disk1_attribute
->getId());
$this
->assertEquals('Disk1', $disk1_attribute
->getLabel());
$this
->assertEquals('select', $disk1_attribute
->getElementType());
$this
->assertTrue($disk1_attribute
->isRequired());
$this
->assertNotCount(3, $disk1_attribute
->getValues(), 'Out of the three available attribute values, only the one used is returned.');
$this
->assertCount(1, $disk1_attribute
->getValues());
$this
->assertEquals([
'15' => '2TB',
], $disk1_attribute
->getValues());
$disk2_attribute = $attributes['attribute_disk2'];
$this
->assertEquals('disk2', $disk2_attribute
->getId());
$this
->assertEquals('Disk2', $disk2_attribute
->getLabel());
$this
->assertEquals('select', $disk2_attribute
->getElementType());
$this
->assertFalse($disk2_attribute
->isRequired());
$this
->assertEquals([
'18' => '2TB',
], $disk2_attribute
->getValues());
// Test 8GB x 1TB x 2TB.
$attributes = $this->mapper
->prepareAttributes($variations[1], $variations);
$ram_attribute = $attributes['attribute_ram'];
$this
->assertEquals('ram', $ram_attribute
->getId());
$this
->assertEquals('Ram', $ram_attribute
->getLabel());
$this
->assertEquals('select', $ram_attribute
->getElementType());
$this
->assertTrue($ram_attribute
->isRequired());
$this
->assertNotCount(4, $ram_attribute
->getValues(), 'Out of the four available attribute values, only the two used are returned.');
$this
->assertCount(2, $ram_attribute
->getValues());
$this
->assertEquals([
'11' => '8GB',
'10' => '4GB',
], $ram_attribute
->getValues());
$disk1_attribute = $attributes['attribute_disk1'];
$this
->assertEquals('disk1', $disk1_attribute
->getId());
$this
->assertEquals('Disk1', $disk1_attribute
->getLabel());
$this
->assertEquals('select', $disk1_attribute
->getElementType());
$this
->assertTrue($disk1_attribute
->isRequired());
$this
->assertEquals([
'15' => '2TB',
'14' => '1TB',
], $disk1_attribute
->getValues());
$disk2_attribute = $attributes['attribute_disk2'];
$this
->assertEquals('disk2', $disk2_attribute
->getId());
$this
->assertEquals('Disk2', $disk2_attribute
->getLabel());
$this
->assertEquals('select', $disk2_attribute
->getElementType());
$this
->assertFalse($disk2_attribute
->isRequired());
// There should only be one Disk 2 option, since the other 8GB RAM option
// has a Disk 1 value of 2TB.
$this
->assertEquals([
'18' => '2TB',
], $disk2_attribute
->getValues());
// Test 8GB x 2TB x 1TB.
$attributes = $this->mapper
->prepareAttributes($variations[2], $variations);
$ram_attribute = $attributes['attribute_ram'];
$this
->assertEquals('ram', $ram_attribute
->getId());
$this
->assertEquals('Ram', $ram_attribute
->getLabel());
$this
->assertEquals('select', $ram_attribute
->getElementType());
$this
->assertTrue($ram_attribute
->isRequired());
$this
->assertEquals([
'11' => '8GB',
'10' => '4GB',
], $ram_attribute
->getValues());
$disk1_attribute = $attributes['attribute_disk1'];
$this
->assertEquals('disk1', $disk1_attribute
->getId());
$this
->assertEquals('Disk1', $disk1_attribute
->getLabel());
$this
->assertEquals('select', $disk1_attribute
->getElementType());
$this
->assertTrue($disk1_attribute
->isRequired());
$this
->assertEquals([
'15' => '2TB',
'14' => '1TB',
], $disk1_attribute
->getValues());
$disk2_attribute = $attributes['attribute_disk2'];
$this
->assertEquals('disk2', $disk2_attribute
->getId());
$this
->assertEquals('Disk2', $disk2_attribute
->getLabel());
$this
->assertEquals('select', $disk2_attribute
->getElementType());
$this
->assertFalse($disk2_attribute
->isRequired());
// There should only be one Disk 2 option, since the other 8GB RAM option
// has a Disk 1 value of 2TB.
$this
->assertEquals([
'17' => '1TB',
], $disk2_attribute
->getValues());
}