Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableAttributeDataTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ConfigurableAttributeDataTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $product;
18 
23 
28  protected $attributeMock;
29 
33  protected function setUp()
34  {
35  $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
36  'getTypeInstance',
37  'setParentId',
38  'hasPreconfiguredValues',
39  'getPreconfiguredValues',
40  'getPriceInfo',
41  'getStoreId'
42  ]);
43  $this->attributeMock = $this->createMock(
45  );
46  $this->configurableAttributeData = new \Magento\ConfigurableProduct\Model\ConfigurableAttributeData();
47  }
48 
52  public function testPrepareJsonAttributes()
53  {
54  $storeId = '1';
55  $attributeId = 5;
56  $attributeOptions = [
57  ['value_index' => 'option_id_1', 'label' => 'label_1'],
58  ['value_index' => 'option_id_2', 'label' => 'label_2'],
59  ];
60  $position = 2;
61  $expected = [
62  'attributes' => [
63  $attributeId => [
64  'id' => $attributeId,
65  'code' => 'test_attribute',
66  'label' => 'Test',
67  'position' => $position,
68  'options' => [
69  0 => [
70  'id' => 'option_id_1',
71  'label' => 'label_1',
72  'products' => 'option_products_1',
73  ],
74  1 => [
75  'id' => 'option_id_2',
76  'label' => 'label_2',
77  'products' => 'option_products_2',
78  ],
79  ],
80  ],
81  ],
82  'defaultValues' => [
83  $attributeId => 'option_id_1',
84  ],
85  ];
86  $options = [
87  $attributeId => ['option_id_1' => 'option_products_1', 'option_id_2' => 'option_products_2'],
88  ];
89 
90  $productAttributeMock = $this->getMockBuilder(\Magento\Catalog\Model\Entity\Attribute::class)
91  ->disableOriginalConstructor()
92  ->setMethods(['getStoreLabel', '__wakeup', 'getAttributeCode', 'getId', 'getAttributeLabel'])
93  ->getMock();
94  $productAttributeMock->expects($this->once())
95  ->method('getId')
96  ->willReturn($attributeId);
97  $productAttributeMock->expects($this->once())
98  ->method('getAttributeCode')
99  ->willReturn($expected['attributes'][$attributeId]['code']);
100 
101  $attributeMock = $this->getMockBuilder(
102  \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute::class
103  )
104  ->disableOriginalConstructor()
105  ->setMethods(['getProductAttribute', '__wakeup', 'getLabel', 'getOptions', 'getAttributeId', 'getPosition'])
106  ->getMock();
107  $attributeMock->expects($this->once())
108  ->method('getProductAttribute')
109  ->willReturn($productAttributeMock);
110  $attributeMock->expects($this->once())
111  ->method('getPosition')
112  ->willReturn($position);
113 
114  $this->product->expects($this->once())->method('getStoreId')->willReturn($storeId);
115  $productAttributeMock->expects($this->once())
116  ->method('getStoreLabel')
117  ->with($storeId)
118  ->willReturn($expected['attributes'][$attributeId]['label']);
119 
120  $attributeMock->expects($this->atLeastOnce())
121  ->method('getAttributeId')
122  ->willReturn($attributeId);
123  $attributeMock->expects($this->atLeastOnce())
124  ->method('getOptions')
125  ->willReturn($attributeOptions);
126 
127  $configurableProduct = $this->getMockBuilder(
128  \Magento\ConfigurableProduct\Model\Product\Type\Configurable::class
129  )->disableOriginalConstructor()->getMock();
130  $configurableProduct->expects($this->once())
131  ->method('getConfigurableAttributes')
132  ->with($this->product)
133  ->willReturn([$attributeMock]);
134 
135  $configuredValueMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
136  ->disableOriginalConstructor()
137  ->getMock();
138  $configuredValueMock->expects($this->any())
139  ->method('getData')
140  ->willReturn($expected['defaultValues'][$attributeId]);
141 
142  $this->product->expects($this->once())
143  ->method('getTypeInstance')
144  ->willReturn($configurableProduct);
145  $this->product->expects($this->once())
146  ->method('hasPreconfiguredValues')
147  ->willReturn(true);
148  $this->product->expects($this->once())
149  ->method('getPreconfiguredValues')
150  ->willReturn($configuredValueMock);
151 
152  $this->assertEquals($expected, $this->configurableAttributeData->getAttributesData($this->product, $options));
153  }
154 }