Skip to content

Commit

Permalink
Merge pull request #64 from lfolco/product-build
Browse files Browse the repository at this point in the history
add build without save method for products
  • Loading branch information
schmengler authored Feb 9, 2021
2 parents 6333145 + 68ddecc commit 43dae87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Catalog/ProductBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ public function build(): ProductInterface
}
}

/**
* @return ProductInterface
*/
public function buildWithoutSave() : ProductInterface
{
if (!$this->product->getSku()) {
$this->product->setSku(sha1(uniqid('', true)));
}
$this->product->setCustomAttribute('url_key', $this->product->getSku());
$this->product->setData('category_ids', $this->categoryIds);

return clone $this->product;
}

/**
* @return ProductInterface
* @throws Exception
Expand Down
10 changes: 10 additions & 0 deletions tests/Catalog/ProductBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ public function testRandomSkuOnBuild()
$this->products[] = $otherProductFixture;
}

public function testRandomSkuOnBuildWithoutSave()
{
$product = ProductBuilder::aSimpleProduct()->buildWithoutSave();
$this->assertRegExp('/[0-9a-f]{32}/', $product->getSku());

$otherProduct = ProductBuilder::aSimpleProduct()->buildWithoutSave();
$this->assertRegExp('/[0-9a-f]{32}/', $otherProduct->getSku());
$this->assertNotEquals($product->getSku(), $otherProduct->getSku());
}

public function testProductCanBeLoadedWithCollection()
{
$productFixture = new ProductFixture(
Expand Down

0 comments on commit 43dae87

Please sign in to comment.