Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeFilterTest.php
Go to the documentation of this file.
1 <?php
8 
12 use PHPUnit_Framework_MockObject_MockObject as MockObject;
13 
14 class AttributeFilterTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $objectManagerMock;
25 
29  protected $productMock;
30 
31  protected function setUp()
32  {
33  $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
34  $this->model = $objectHelper->getObject(AttributeFilter::class);
35  }
36 
44  public function testPrepareProductAttributes(
45  $requestProductData,
46  $useDefaults,
47  $expectedProductData,
48  $initialProductData
49  ) {
51  $productMockMap = $this->getMockBuilder(Product::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['getData', 'getAttributes'])
54  ->getMock();
55 
56  if (!empty($initialProductData)) {
57  $productMockMap->expects($this->any())->method('getData')->willReturnMap($initialProductData);
58  }
59 
60  if ($useDefaults) {
61  $productMockMap
62  ->expects($this->once())
63  ->method('getAttributes')
64  ->willReturn(
65  $this->getProductAttributesMock($useDefaults)
66  );
67  }
68 
69  $actualProductData = $this->model->prepareProductAttributes($productMockMap, $requestProductData, $useDefaults);
70  $this->assertEquals($expectedProductData, $actualProductData);
71  }
72 
77  public function setupInputDataProvider()
78  {
79  return [
80  'create_new_product' => [
81  'productData' => [
82  'name' => 'testName',
83  'sku' => 'testSku',
84  'price' => '100',
85  'description' => '',
86  ],
87  'useDefaults' => [],
88  'expectedProductData' => [
89  'name' => 'testName',
90  'sku' => 'testSku',
91  'price' => '100',
92  ],
93  'initialProductData' => [],
94  ],
95  'update_product_without_use_defaults' => [
96  'productData' => [
97  'name' => 'testName2',
98  'sku' => 'testSku2',
99  'price' => '101',
100  'description' => '',
101  'special_price' => null,
102  ],
103  'useDefaults' => [],
104  'expectedProductData' => [
105  'name' => 'testName2',
106  'sku' => 'testSku2',
107  'price' => '101',
108  'special_price' => null,
109  ],
110  'initialProductData' => [
111  ['name', 'testName2'],
112  ['sku', 'testSku2'],
113  ['price', '101'],
114  ['special_price', null],
115  ],
116  ],
117  'update_product_without_use_defaults_2' => [
118  'productData' => [
119  'name' => 'testName2',
120  'sku' => 'testSku2',
121  'price' => '101',
122  'description' => 'updated description',
123  'special_price' => null,
124  ],
125  'useDefaults' => [],
126  'expectedProductData' => [
127  'name' => 'testName2',
128  'sku' => 'testSku2',
129  'price' => '101',
130  'description' => 'updated description',
131  'special_price' => null,
132  ],
133  'initialProductData' => [
134  ['name', 'testName2'],
135  ['sku', 'testSku2'],
136  ['price', '101'],
137  ['special_price', null],
138  ],
139  ],
140  'update_product_with_use_defaults' => [
141  'productData' => [
142  'name' => 'testName2',
143  'sku' => 'testSku2',
144  'price' => '101',
145  'description' => '',
146  'special_price' => null,
147  ],
148  'useDefaults' => [
149  'description' => '0',
150  ],
151  'expectedProductData' => [
152  'name' => 'testName2',
153  'sku' => 'testSku2',
154  'price' => '101',
155  'special_price' => null,
156  'description' => '',
157  ],
158  'initialProductData' => [
159  ['name', 'testName2'],
160  ['sku', 'testSku2'],
161  ['price', '101'],
162  ['special_price', null],
163  ['description', 'descr text'],
164  ],
165  ],
166  'update_product_with_use_defaults_2' => [
167  'requestProductData' => [
168  'name' => 'testName3',
169  'sku' => 'testSku3',
170  'price' => '103',
171  'description' => 'descr modified',
172  'special_price' => '100',
173  ],
174  'useDefaults' => [
175  'description' => '0',
176  ],
177  'expectedProductData' => [
178  'name' => 'testName3',
179  'sku' => 'testSku3',
180  'price' => '103',
181  'special_price' => '100',
182  'description' => 'descr modified',
183  ],
184  'initialProductData' => [
185  ['name', null, 'testName2'],
186  ['sku', null, 'testSku2'],
187  ['price', null, '101'],
188  ['description', null, 'descr text'],
189  ],
190  ],
191  'update_product_with_use_defaults_3' => [
192  'requestProductData' => [
193  'name' => 'testName3',
194  'sku' => 'testSku3',
195  'price' => '103',
196  'special_price' => '100',
197  'description' => 'descr modified',
198  ],
199  'useDefaults' => [
200  'description' => '1',
201  ],
202  'expectedProductData' => [
203  'name' => 'testName3',
204  'sku' => 'testSku3',
205  'price' => '103',
206  'special_price' => '100',
207  'description' => false,
208  ],
209  'initialProductData' => [
210  ['name', null, 'testName2'],
211  ['sku', null, 'testSku2'],
212  ['price', null, '101'],
213  ['description', null, 'descr text'],
214  ],
215  ],
216  ];
217  }
218 
223  private function getProductAttributesMock(array $useDefaults): array
224  {
225  $returnArray = [];
226  foreach ($useDefaults as $attributecode => $isDefault) {
227  if ($isDefault === '1') {
229  $attribute = $this->getMockBuilder(Attribute::class)
230  ->disableOriginalConstructor()
231  ->getMock();
232  $attribute->expects($this->any())
233  ->method('getBackendType')
234  ->willReturn('varchar');
235 
236  $returnArray[$attributecode] = $attribute;
237  }
238  }
239  return $returnArray;
240  }
241 }