Skip to content

Commit

Permalink
Merge pull request #7205 from David-Guillot/fix/product-model-attribu…
Browse files Browse the repository at this point in the history
…te-methods-requirements

Fixed Product model Attributes-related methods
  • Loading branch information
michalmarcinkowski committed Jan 13, 2017
2 parents 19303d7 + 93a9c8b commit 2981ca2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Sylius/Component/Product/Model/Product.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Webmozart\Assert\Assert;
use Sylius\Component\Attribute\Model\AttributeValueInterface;
use Sylius\Component\Resource\Model\TimestampableTrait;
use Sylius\Component\Resource\Model\ToggleableTrait;
Expand Down Expand Up @@ -247,6 +248,12 @@ public function getAttributes()
*/
public function addAttribute(AttributeValueInterface $attribute)
{
Assert::isInstanceOf(
$attribute,
ProductAttributeValueInterface::class,
'Attribute objects added to a Product object have to implement ProductAttributeValueInterface'
);

if (!$this->hasAttribute($attribute)) {
$attribute->setProduct($this);
$this->attributes->add($attribute);
Expand All @@ -258,6 +265,12 @@ public function addAttribute(AttributeValueInterface $attribute)
*/
public function removeAttribute(AttributeValueInterface $attribute)
{
Assert::isInstanceOf(
$attribute,
ProductAttributeValueInterface::class,
'Attribute objects removed from a Product object have to implement ProductAttributeValueInterface'
);

if ($this->hasAttribute($attribute)) {
$this->attributes->removeElement($attribute);
$attribute->setProduct(null);
Expand Down
12 changes: 12 additions & 0 deletions src/Sylius/Component/Product/spec/Model/ProductSpec.php
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Attribute\Model\AttributeValueInterface;
use Sylius\Component\Product\Model\Product;
use Sylius\Component\Product\Model\ProductAssociationInterface;
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
Expand Down Expand Up @@ -141,6 +142,17 @@ function it_removes_attribute(ProductAttributeValueInterface $attribute)
$this->hasAttribute($attribute)->shouldReturn(false);
}

function it_refuses_to_add_non_product_attribute(AttributeValueInterface $attribute)
{
$this->shouldThrow('\InvalidArgumentException')->duringAddAttribute($attribute);
$this->hasAttribute($attribute)->shouldReturn(false);
}

function it_refuses_to_remove_non_product_attribute(AttributeValueInterface $attribute)
{
$this->shouldThrow('\InvalidArgumentException')->duringRemoveAttribute($attribute);
}

function it_has_no_variants_by_default()
{
$this->hasVariants()->shouldReturn(false);
Expand Down

0 comments on commit 2981ca2

Please sign in to comment.