Skip to content

Commit

Permalink
Merge pull request #23 from tddwizard/set-weight
Browse files Browse the repository at this point in the history
Add method to set weight in product builder
  • Loading branch information
Fabian Schmengler / committed Nov 15, 2018
2 parents d08296a + f874282 commit 3ca74c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/Catalog/ProductBuilder.php
Expand Up @@ -208,6 +208,13 @@ public function withStockQty($qty) : ProductBuilder
return $builder;
}

public function withWeight($weight) : ProductBuilder
{
$builder = clone $this;
$builder->product->setWeight($weight);
return $builder;
}

public function withCustomAttributes(array $values, $storeId = null) : ProductBuilder
{
$builder = clone $this;
Expand Down
23 changes: 13 additions & 10 deletions tests/Catalog/ProductBuilderTest.php
Expand Up @@ -84,6 +84,7 @@ public function testSimpleProductWithSpecificAttributes()
->withTaxClassId(2)
->withIsInStock(false)
->withStockQty(-1)
->withWeight(10)
->withCustomAttributes(
[
'cost' => 2.0
Expand All @@ -94,20 +95,22 @@ public function testSimpleProductWithSpecificAttributes()
$this->products[] = $productFixture;
/** @var Product $product */
$product = $this->productRepository->getById($productFixture->getId());
$this->assertEquals('foobar', $product->getSku());
$this->assertEquals('foobar', $product->getSku(), 'sku');
$this->assertEquals('foobar', $product->getUrlKey(), 'URL key should equal SKU if not set otherwise');
$this->assertEquals('Foo Bar', $product->getName());
$this->assertEquals(Status::STATUS_DISABLED, $product->getStatus());
$this->assertEquals(Product\Visibility::VISIBILITY_NOT_VISIBLE, $product->getVisibility());
$this->assertEquals('Foo Bar', $product->getName(), 'name');
$this->assertEquals(Status::STATUS_DISABLED, $product->getStatus(), 'status');
$this->assertEquals(Product\Visibility::VISIBILITY_NOT_VISIBLE, $product->getVisibility(), 'visibility');
// current website (1) is always added by ProductRepository
$this->assertEquals([1, $secondWebsiteId], $product->getWebsiteIds());
$this->assertEquals(9.99, $product->getPrice());
$this->assertEquals(2, $product->getData('tax_class_id'));
$this->assertEquals([1, $secondWebsiteId], $product->getWebsiteIds(), 'website ids');
$this->assertEquals(9.99, $product->getPrice(), 'price');
$this->assertEquals(10, $product->getWeight(), 'weight');
$this->assertEquals(2, $product->getData('tax_class_id'), 'tax class id');
$this->assertFalse(
$product->getExtensionAttributes()->getStockItem()->getIsInStock()
$product->getExtensionAttributes()->getStockItem()->getIsInStock(),
'in stock'
);
$this->assertEquals(-1, $product->getExtensionAttributes()->getStockItem()->getQty());
$this->assertEquals(2.0, $product->getCustomAttribute('cost')->getValue());
$this->assertEquals(-1, $product->getExtensionAttributes()->getStockItem()->getQty(), 'stock qty');
$this->assertEquals(2.0, $product->getCustomAttribute('cost')->getValue(), 'custom attribute "cost"');
}

/**
Expand Down

0 comments on commit 3ca74c0

Please sign in to comment.