1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Test\Unit\Model;
/**
* Class CustomOptionTest
*/
class ConfigurableAttributeDataTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
*/
protected $product;
/**
* @var \Magento\ConfigurableProduct\Model\ConfigurableAttributeData|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configurableAttributeData;
/**
* @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute|
* \PHPUnit_Framework_MockObject_MockObject
*/
protected $attributeMock;
/**
* Test setUp
*/
protected function setUp()
{
$this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
'getTypeInstance',
'setParentId',
'hasPreconfiguredValues',
'getPreconfiguredValues',
'getPriceInfo',
'getStoreId'
]);
$this->attributeMock = $this->createMock(
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute::class
);
$this->configurableAttributeData = new \Magento\ConfigurableProduct\Model\ConfigurableAttributeData();
}
/**
* @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::class)
->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::class
)
->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::class
)->disableOriginalConstructor()->getMock();
$configurableProduct->expects($this->once())
->method('getConfigurableAttributes')
->with($this->product)
->willReturn([$attributeMock]);
$configuredValueMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
->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));
}
}