/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testPrepareJsonAttributes() { $storeId = '1'; $attributeId = 5; $attributeOptions = [['value_index' => 'option_id_1', 'label' => 'label_1'], ['value_index' => 'option_id_2', 'label' => 'label_2']]; $position = 2; $expected = ['attributes' => [$attributeId => ['id' => $attributeId, 'code' => 'test_attribute', 'label' => 'Test', 'position' => $position, 'options' => [0 => ['id' => 'option_id_1', 'label' => 'label_1', 'products' => 'option_products_1'], 1 => ['id' => 'option_id_2', 'label' => 'label_2', 'products' => 'option_products_2']]]], 'defaultValues' => [$attributeId => 'option_id_1']]; $options = [$attributeId => ['option_id_1' => 'option_products_1', 'option_id_2' => 'option_products_2']]; $productAttributeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Entity\\Attribute')->disableOriginalConstructor()->setMethods(['getStoreLabel', '__wakeup', 'getAttributeCode', 'getId', 'getAttributeLabel'])->getMock(); $productAttributeMock->expects($this->once())->method('getId')->willReturn($attributeId); $productAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn($expected['attributes'][$attributeId]['code']); $attributeMock = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute')->disableOriginalConstructor()->setMethods(['getProductAttribute', '__wakeup', 'getLabel', 'getOptions', 'getAttributeId', 'getPosition'])->getMock(); $attributeMock->expects($this->once())->method('getProductAttribute')->willReturn($productAttributeMock); $attributeMock->expects($this->once())->method('getPosition')->willReturn($position); $this->product->expects($this->once())->method('getStoreId')->willReturn($storeId); $productAttributeMock->expects($this->once())->method('getStoreLabel')->with($storeId)->willReturn($expected['attributes'][$attributeId]['label']); $attributeMock->expects($this->atLeastOnce())->method('getAttributeId')->willReturn($attributeId); $attributeMock->expects($this->atLeastOnce())->method('getOptions')->willReturn($attributeOptions); $configurableProduct = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable')->disableOriginalConstructor()->getMock(); $configurableProduct->expects($this->once())->method('getConfigurableAttributes')->with($this->product)->willReturn([$attributeMock]); $configuredValueMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $configuredValueMock->expects($this->any())->method('getData')->willReturn($expected['defaultValues'][$attributeId]); $this->product->expects($this->once())->method('getTypeInstance')->willReturn($configurableProduct); $this->product->expects($this->once())->method('hasPreconfiguredValues')->willReturn(true); $this->product->expects($this->once())->method('getPreconfiguredValues')->willReturn($configuredValueMock); $this->assertEquals($expected, $this->configurableAttributeData->getAttributesData($this->product, $options)); }
/** * Composes configuration for js * * @return string */ public function getJsonConfig() { $store = $this->getCurrentStore(); $currentProduct = $this->getProduct(); $regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price'); $finalPrice = $currentProduct->getPriceInfo()->getPrice('final_price'); $options = $this->helper->getOptions($currentProduct, $this->getAllowProducts()); $attributesData = $this->configurableAttributeData->getAttributesData($currentProduct, $options); $config = ['attributes' => $attributesData['attributes'], 'template' => str_replace('%s', '<%- data.price %>', $store->getCurrentCurrency()->getOutputFormat()), 'optionPrices' => $this->getOptionPrices(), 'prices' => ['oldPrice' => ['amount' => $this->_registerJsPrice($this->_convertPrice($regularPrice->getAmount()->getValue()))], 'basePrice' => ['amount' => $this->_registerJsPrice($this->_convertPrice($finalPrice->getAmount()->getBaseAmount()))], 'finalPrice' => ['amount' => $this->_registerJsPrice($this->_convertPrice($finalPrice->getAmount()->getValue()))]], 'productId' => $currentProduct->getId(), 'chooseText' => __('Choose an Option...'), 'images' => isset($options['images']) ? $options['images'] : [], 'index' => isset($options['index']) ? $options['index'] : []]; if ($currentProduct->hasPreconfiguredValues() && !empty($attributesData['defaultValues'])) { $config['defaultValues'] = $attributesData['defaultValues']; } $config = array_merge($config, $this->_getAdditionalConfig()); return $this->jsonEncoder->encode($config); }