public function CommerceProductBundleItemTest::testVariationsAndProductMethods in Commerce Product Bundle 8
Test the setters, getters and valdiation methods around the reference product and variations.
@covers ::setProduct @covers ::getProduct @covers ::getProductId @covers ::hasProduct @covers ::setVariations @covers ::getVariations @covers ::hasVariations @covers ::addVariation @covers ::setCurrentVariation @covers ::getCurrentVariation
File
- tests/
src/ Kernel/ Entity/ CommerceProductBundleItemTest.php, line 164
Class
- CommerceProductBundleItemTest
- Test the Product Bundle Item entity.
Namespace
Drupal\Tests\commerce_product_bundle\Kernel\EntityCode
public function testVariationsAndProductMethods() {
$bundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
// Ensure nothing fatals if we call certain methods without setting the
// variations reference or product reference.
$this
->assertNull($bundleItem
->getVariations());
$this
->assertNull($bundleItem
->getCurrentVariation());
$this
->assertNull($bundleItem
->getVariationIds());
$this
->assertFalse($bundleItem
->hasProduct());
$variations = [];
for ($i = 1; $i <= 5; $i++) {
$variation = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'uid' => $this->user
->id(),
'status' => $i % 2,
]);
$variation
->save();
$variations[] = $variation;
}
$variations = array_reverse($variations);
$product = Product::create([
'type' => 'default',
'variations' => $variations,
'uid' => $this->user
->id(),
]);
$product
->save();
$product = $this
->reloadEntity($product);
$bundleItem
->setProduct($product);
$this
->assertTrue($bundleItem
->hasProduct());
$this
->assertEquals($product
->id(), $bundleItem
->getProductId());
$current = $bundleItem
->getCurrentVariation();
$this
->assertInstanceOf(ProductVariationInterface::class, $current);
$this
->assertFalse($bundleItem
->hasVariations());
$bundleItem
->setVariations($variations);
// Uncomment after https://www.drupal.org/project/commerce_product_bundle/issues/2837499
// $this->assertCount(3, $bundleItem->getVariations());
$this
->assertTrue($bundleItem
->hasVariations());
$this
->assertEquals($variations[0]
->id(), $bundleItem
->getDefaultVariation()
->id());
$this
->assertEquals($variations[0]
->id(), $bundleItem
->getCurrentVariation()
->id());
$bundleItem
->setCurrentVariation($variations[4]);
$this
->assertEquals($variations[4]
->id(), $bundleItem
->getCurrentVariation()
->id());
$bundleItem
->removeVariation($variations[0]);
// Uncomment after https://www.drupal.org/project/commerce_product_bundle/issues/2837499
// $this->assertCount(2, $bundleItem->getVariations());
// Wether the backreference to the bundle gets saved on bundle save.
$this
->assertNull($bundleItem
->getBundleId());
$bundle = ProductBundle::create([
'type' => 'default',
]);
$bundle
->addBundleItem($bundleItem);
$bundle
->save();
$bundleItem = $this
->reloadEntity($bundleItem);
$this
->assertEquals($bundle
->id(), $bundleItem
->getBundleId());
// Wether setting the variations sets the product reference.
$bundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$bundleItem
->setVariations($variations);
$this
->assertEquals($product
->id(), $bundleItem
->getProduct()
->id());
// @todo Test the bundle <> back reference.
$freshBundleItem = ProductBundleItem::create([
'type' => 'default',
]);
$bundleItem
->save();
$values = [
'id' => strtolower($this
->randomMachineName(8)),
'label' => $this
->randomMachineName(),
'orderItemType' => 'default',
];
$variationType = $this
->createEntity('commerce_product_variation_type', $values);
$otherVariation = ProductVariation::create([
'type' => $variationType
->getEntityTypeId(),
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'status' => $i % 2,
]);
$freshBundleItem
->addVariation($otherVariation);
$this
->assertNull($freshBundleItem
->getProduct());
$this
->assertNull($freshBundleItem
->getVariations());
$this::expectException('\\InvalidArgumentException');
$bundleItem
->addVariation($otherVariation);
$this::expectException('\\InvalidArgumentException');
$bundleItem
->setVariations([
$otherVariation,
]);
$this::expectException('\\InvalidArgumentException');
$bundleItem
->setCurrentVariation($otherVariation);
}