/**
  * Normalize the attributes
  *
  * @param Group $group
  *
  * @return array
  */
 protected function normalizeAttributes(Group $group)
 {
     $attributes = array();
     foreach ($group->getAttributes() as $attribute) {
         $attributes[] = $attribute->getCode();
     }
     sort($attributes);
     return $attributes;
 }
 /**
  * Test getter/add/remove for attributes property
  */
 public function testGetAddRemoveAttribute()
 {
     // Change value and assert new
     $newAttribute = new Attribute();
     $this->assertEntity($this->group->addAttribute($newAttribute));
     $this->assertInstanceOf('Pim\\Bundle\\CatalogBundle\\Entity\\Attribute', $this->group->getAttributes()->first());
     $this->assertEntity($this->group->removeAttribute($newAttribute));
     $this->assertNotInstanceOf('Pim\\Bundle\\CatalogBundle\\Entity\\Attribute', $this->group->getAttributes()->first());
     $this->assertCount(0, $this->group->getAttributes());
 }
 /**
  * Get price mapping for the given group and products.
  *
  * @param Group             $group
  * @param array             $products
  * @param MappingCollection $attributeMapping
  * @param boolean           $lowest
  *
  * @return array
  */
 public function getPriceMapping(Group $group, array $products, MappingCollection $attributeMapping, $lowest = true)
 {
     $attributes = $group->getAttributes();
     $basePrice = $this->getLimitPrice($products, $attributeMapping, [], $lowest);
     $sortedAttributes = $this->getSortedAttributes($attributes, $products, $basePrice, $attributeMapping, $lowest);
     $priceChanges = [];
     foreach ($sortedAttributes as $attribute) {
         $attributePriceMapping = $this->getAttributePriceMapping($attribute, $basePrice, $products, $priceChanges, $attributeMapping, $lowest);
         $priceChanges[$attributeMapping->getTarget($attribute->getCode())] = $attributePriceMapping;
     }
     if ($lowest) {
         try {
             $this->validatePriceMapping($products, $priceChanges, $basePrice, $attributeMapping);
         } catch (ComputedPriceNotMatchedException $e) {
             return $this->getPriceMapping($group, $products, $attributeMapping, false);
         }
     }
     ksort($priceChanges);
     return [ConfigurableNormalizer::PRICE_CHANGES => $priceChanges, ConfigurableNormalizer::BASE_PRICE => $basePrice];
 }
 function let(ChannelManager $channelManager, ProductNormalizer $productNormalizer, PriceMappingManager $priceMappingManager, MappingCollection $categoryMapping, MappingCollection $attributeMapping, MappingCollection $storeViewMapping, ProductInterface $product, Channel $channel, Locale $localeFR, Locale $localeEN, Group $group, Attribute $attribute)
 {
     $this->beConstructedWith($channelManager, $productNormalizer, $priceMappingManager, 4);
     $this->globalContext = ['attributeSetId' => 0, 'magentoAttributes' => ['attribute_code' => ['attribute_id' => 42]], 'magentoAttributesOptions' => [], 'storeViewMapping' => $storeViewMapping, 'magentoStoreViews' => [['code' => 'fr_fr']], 'defaultLocale' => 'default_locale', 'website' => 'website', 'channel' => 'channel', 'categoryMapping' => $categoryMapping, 'attributeCodeMapping' => $attributeMapping, 'create' => true, 'defaultStoreView' => 'default', 'smallImageAttribute' => 'smallImageAttr', 'baseImageAttribute' => 'baseImageAttr', 'thumbnailAttribute' => 'thumbnailAttr', 'pimGrouped' => 'grouped_product_code', 'urlKey' => false, 'skuFirst' => false];
     $productNormalizer->getNormalizedImages($product, 'conf-group_code', 'smallImageAttr', 'baseImageAttr', 'thumbnailAttr')->willReturn([]);
     $productNormalizer->getValues(Argument::cetera())->willReturn([ProductNormalizer::URL_KEY => 'my-url-key']);
     $channelManager->getChannelByCode('channel')->willReturn($channel);
     $channel->getLocales()->willReturn([$localeEN, $localeFR]);
     $localeEN->getCode()->willReturn('default_locale');
     $localeFR->getCode()->willReturn('fr_FR');
     $channel->getCode()->willReturn('channel_code');
     $storeViewMapping->getTarget('default_locale')->willReturn('default_locale');
     $storeViewMapping->getTarget('fr_FR')->willReturn('fr_fr');
     $group->getId()->willReturn(44);
     $group->getCode()->willReturn('group_code');
     $product->getIdentifier()->willReturn('sku-000');
     $attribute->setCode('attribute_code');
     $group->getAttributes()->willReturn([$attribute]);
     $attribute->getCode()->willReturn('attribute_code');
     $attributeMapping->getTarget('attribute_code')->willReturn('attribute_code');
 }
 function let(Group $group, Product $product1, Product $product2, Product $product3, Attribute $attribute1, Attribute $attribute2, AttributeOption $attributeOption11, AttributeOption $attributeOption12, AttributeOption $attributeOption21, AttributeOption $attributeOption22, ProductValue $productValueOption11, ProductValue $productValueOption12, ProductValue $productValueOption21, ProductValue $productValueOption22, ProductValue $productValuePrice1, ProductPrice $productPrice1, ProductValue $productValuePrice2, ProductPrice $productPrice2, ProductValue $productValuePrice3, ProductPrice $productPrice3, MappingCollection $attributeMapping)
 {
     $this->beConstructedWith('locale', 'currency');
     $group->getAttributes()->willReturn([$attribute1, $attribute2]);
     //get attribute options
     $attribute1->getOptions()->willReturn([$attributeOption11, $attributeOption12]);
     $attribute1->getCode()->willReturn('attribute_1');
     $attributeOption11->getAttribute()->willReturn($attribute1);
     $attributeOption11->getCode()->willReturn('attribute_1_option_1');
     $productValueOption11->getData()->willReturn($attributeOption11);
     $attributeOption12->getAttribute()->willReturn($attribute1);
     $attributeOption12->getCode()->willReturn('attribute_1_option_2');
     $productValueOption12->getData()->willReturn($attributeOption12);
     $attribute2->getOptions()->willReturn([$attributeOption21, $attributeOption22]);
     $attribute2->getCode()->willReturn('attribute_2');
     $attributeOption21->getAttribute()->willReturn($attribute2);
     $attributeOption21->getCode()->willReturn('attribute_2_option_1');
     $productValueOption21->getData()->willReturn($attributeOption21);
     $attributeOption22->getAttribute()->willReturn($attribute2);
     $attributeOption22->getCode()->willReturn('attribute_2_option_2');
     $productValueOption22->getData()->willReturn($attributeOption22);
     //Get product prices
     $product1->getValue('price', 'locale')->willReturn($productValuePrice1);
     $product1->getIdentifier()->willReturn('product_1');
     $productValuePrice1->getPrice('currency')->willReturn($productPrice1);
     $productPrice1->getData()->willReturn(5.0);
     $product2->getValue('price', 'locale')->willReturn($productValuePrice2);
     $product2->getIdentifier()->willReturn('product_2');
     $productValuePrice2->getPrice('currency')->willReturn($productPrice2);
     $productPrice2->getData()->willReturn(15.0);
     $product3->getValue('price', 'locale')->willReturn($productValuePrice3);
     $product3->getIdentifier()->willReturn('product_3');
     $productValuePrice3->getPrice('currency')->willReturn($productPrice3);
     $productPrice3->getData()->willReturn(10.0);
     $attributeMapping->getSource('attribute_1')->willReturn('attribute_1');
     $attributeMapping->getSource('attribute_2')->willReturn('attribute_2');
     $attributeMapping->getTarget('attribute_1')->willReturn('attribute_1');
     $attributeMapping->getTarget('attribute_2')->willReturn('attribute_2');
 }
 /**
  * {@inheritDoc}
  */
 public function getAttributes()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getAttributes', array());
     return parent::getAttributes();
 }
 /**
  * Prepare query criteria for variant group
  *
  * @param Group            $variantGroup
  * @param ProductInterface $entity
  *
  * @return array
  */
 protected function prepareQueryCriterias(Group $variantGroup, ProductInterface $entity)
 {
     $criteria = array();
     foreach ($variantGroup->getAttributes() as $attribute) {
         $value = $entity->getValue($attribute->getCode());
         $criteria[] = array('attribute' => $attribute, 'option' => $value ? $value->getOption() : null);
     }
     return $criteria;
 }