Skip to content

Commit bb295f6

Browse files
Andrei KuprienkaAndrei Kuprienka
authored andcommitted
MAGNIMEX-SPRINT2: Add lost php unit tests
1 parent 304b33e commit bb295f6

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed

app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/BundleTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,45 @@ public function testSaveDataDelete()
319319
]));
320320
$this->bundle->saveData();
321321
}
322+
323+
public function testPrepareAttributesWithDefaultValueForSaveInsideCall()
324+
{
325+
$bundleMock = $this->getMock(
326+
'Magento\BundleImportExport\Model\Import\Product\Type\Bundle',
327+
['transformBundleCustomAttributes'],
328+
[],
329+
'',
330+
false
331+
);
332+
// Set some attributes to bypass errors due to static call inside method.
333+
$attrVal = 'value';
334+
$rowData = [
335+
\Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET => $attrVal,
336+
];
337+
$this->setPropertyValue($bundleMock, '_attributes', [
338+
$attrVal => [],
339+
]);
340+
341+
$bundleMock
342+
->expects($this->once())
343+
->method('transformBundleCustomAttributes')
344+
->with($rowData)
345+
->willReturn([]);
346+
347+
$bundleMock->prepareAttributesWithDefaultValueForSave($rowData);
348+
}
349+
350+
/**
351+
* @param $object
352+
* @param $property
353+
* @param $value
354+
*/
355+
protected function setPropertyValue(&$object, $property, $value)
356+
{
357+
$reflection = new \ReflectionClass(get_class($object));
358+
$reflectionProperty = $reflection->getProperty($property);
359+
$reflectionProperty->setAccessible(true);
360+
$reflectionProperty->setValue($object, $value);
361+
return $object;
362+
}
322363
}

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,80 @@ public function testGetCategoryCollection()
385385
$this->assertInstanceOf('\Magento\Framework\Data\Collection', $this->model->getCategoryCollection());
386386
}
387387

388+
/**
389+
* @dataProvider getCategoryCollectionCollectionNullDataProvider
390+
*/
391+
public function testGetCategoryCollectionCollectionNull($initCategoryCollection, $getIdResult, $productIdCached)
392+
{
393+
$product = $this->getMock(
394+
'\Magento\Catalog\Model\Product',
395+
[
396+
'_getResource',
397+
'setCategoryCollection',
398+
'getId',
399+
],
400+
[],
401+
'',
402+
false
403+
);
404+
405+
$abstractDbMock = $this->getMockBuilder('\Magento\Framework\Model\Resource\Db\AbstractDb')
406+
->disableOriginalConstructor()
407+
->setMethods([
408+
'getCategoryCollection',
409+
])
410+
->getMockForAbstractClass();
411+
$getCategoryCollectionMock = $this->getMock(
412+
'\Magento\Framework\Data\Collection',
413+
[],
414+
[],
415+
'',
416+
false
417+
);
418+
$product
419+
->expects($this->once())
420+
->method('setCategoryCollection')
421+
->with($getCategoryCollectionMock);
422+
$product
423+
->expects($this->atLeastOnce())
424+
->method('getId')
425+
->willReturn($getIdResult);
426+
$abstractDbMock
427+
->expects($this->once())
428+
->method('getCategoryCollection')
429+
->with($product)
430+
->willReturn($getCategoryCollectionMock);
431+
$product
432+
->expects($this->once())
433+
->method('_getResource')
434+
->willReturn($abstractDbMock);
435+
436+
$this->setPropertyValue($product, 'categoryCollection', $initCategoryCollection);
437+
$this->setPropertyValue($product, '_productIdCached', $productIdCached);
438+
439+
$result = $product->getCategoryCollection();
440+
441+
$productIdCachedActual = $this->getPropertyValue($product, '_productIdCached', $productIdCached);
442+
$this->assertEquals($getIdResult, $productIdCachedActual);
443+
$this->assertEquals($initCategoryCollection, $result);
444+
}
445+
446+
public function getCategoryCollectionCollectionNullDataProvider()
447+
{
448+
return [
449+
[
450+
'$initCategoryCollection' => null,
451+
'$getIdResult' => 'getIdResult value',
452+
'$productIdCached' => 'productIdCached value',
453+
],
454+
[
455+
'$initCategoryCollection' => 'value',
456+
'$getIdResult' => 'getIdResult value',
457+
'$productIdCached' => 'not getIdResult value',
458+
],
459+
];
460+
}
461+
388462
public function testSetCategoryCollection()
389463
{
390464
$collection = $this->getMockBuilder('\Magento\Framework\Data\Collection')
@@ -1210,4 +1284,31 @@ public function testGetOptions()
12101284
$productModel->setOptions([]);
12111285
$this->assertEquals([], $productModel->getOptions());
12121286
}
1287+
1288+
/**
1289+
* @param $object
1290+
* @param $property
1291+
* @param $value
1292+
*/
1293+
protected function setPropertyValue(&$object, $property, $value)
1294+
{
1295+
$reflection = new \ReflectionClass(get_class($object));
1296+
$reflectionProperty = $reflection->getProperty($property);
1297+
$reflectionProperty->setAccessible(true);
1298+
$reflectionProperty->setValue($object, $value);
1299+
return $object;
1300+
}
1301+
1302+
/**
1303+
* @param $object
1304+
* @param $property
1305+
*/
1306+
protected function getPropertyValue(&$object, $property)
1307+
{
1308+
$reflection = new \ReflectionClass(get_class($object));
1309+
$reflectionProperty = $reflection->getProperty($property);
1310+
$reflectionProperty->setAccessible(true);
1311+
1312+
return $reflectionProperty->getValue($object);
1313+
}
12131314
}

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Type/AbstractTypeTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Type;
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
use Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType as AbstractType;
1011

1112
/**
1213
* Test class for import product AbstractType class
@@ -28,6 +29,11 @@ class AbstractTypeTest extends \PHPUnit_Framework_TestCase
2829
*/
2930
protected $objectManagerHelper;
3031

32+
/**
33+
* @var AbstractType|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $abstractType;
36+
3137
protected function setUp()
3238
{
3339
$this->entityModel = $this->getMock(
@@ -123,6 +129,45 @@ protected function setUp()
123129
'params' => [$this->entityModel, 'simple'],
124130
]
125131
);
132+
133+
$this->abstractType = $this->getMockBuilder(
134+
'\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType'
135+
)
136+
->disableOriginalConstructor()
137+
->getMockForAbstractClass();
138+
}
139+
140+
/**
141+
* @dataProvider addAttributeOptionDataProvider
142+
*/
143+
public function testAddAttributeOption($code, $optionKey, $optionValue, $initAttributes, $resultAttributes)
144+
{
145+
$this->setPropertyValue($this->abstractType, '_attributes', $initAttributes);
146+
147+
$this->abstractType->addAttributeOption($code, $optionKey, $optionValue);
148+
149+
$this->assertEquals($resultAttributes, $this->getPropertyValue($this->abstractType, '_attributes'));
150+
}
151+
152+
public function testAddAttributeOptionReturn()
153+
{
154+
$code = 'attr set name value key';
155+
$optionKey = 'option key';
156+
$optionValue = 'option value';
157+
158+
$result = $this->abstractType->addAttributeOption($code, $optionKey, $optionValue);
159+
160+
$this->assertEquals($result, $this->abstractType);
161+
}
162+
163+
public function testGetCustomFieldsMapping()
164+
{
165+
$expectedResult = ['value'];
166+
$this->setPropertyValue($this->abstractType, '_customFieldsMapping', $expectedResult);
167+
168+
$result = $this->abstractType->getCustomFieldsMapping();
169+
170+
$this->assertEquals($expectedResult, $result);
126171
}
127172

128173
public function testIsRowValidSuccess()
@@ -148,4 +193,71 @@ public function testIsRowValidError()
148193
->willReturnSelf();
149194
$this->assertFalse($this->simpleType->isRowValid($rowData, $rowNum));
150195
}
196+
197+
public function addAttributeOptionDataProvider()
198+
{
199+
return [
200+
[
201+
'$code' => 'attr set name value key',
202+
'$optionKey' => 'option key',
203+
'$optionValue' => 'option value',
204+
'$initAttributes' => [
205+
'attr set name' => [
206+
'attr set name value key' => [],
207+
],
208+
],
209+
'$resultAttributes' => [
210+
'attr set name' => [
211+
'attr set name value key' => [
212+
'options' => [
213+
'option key' => 'option value'
214+
]
215+
]
216+
],
217+
],
218+
],
219+
[
220+
'$code' => 'attr set name value key',
221+
'$optionKey' => 'option key',
222+
'$optionValue' => 'option value',
223+
'$initAttributes' => [
224+
'attr set name' => [
225+
'not equal to code value' => [],
226+
],
227+
],
228+
'$resultAttributes' => [
229+
'attr set name' => [
230+
'not equal to code value' => [],
231+
],
232+
]
233+
],
234+
];
235+
}
236+
237+
/**
238+
* @param $object
239+
* @param $property
240+
*/
241+
protected function getPropertyValue(&$object, $property)
242+
{
243+
$reflection = new \ReflectionClass(get_class($object));
244+
$reflectionProperty = $reflection->getProperty($property);
245+
$reflectionProperty->setAccessible(true);
246+
247+
return $reflectionProperty->getValue($object);
248+
}
249+
250+
/**
251+
* @param $object
252+
* @param $property
253+
* @param $value
254+
*/
255+
protected function setPropertyValue(&$object, $property, $value)
256+
{
257+
$reflection = new \ReflectionClass(get_class($object));
258+
$reflectionProperty = $reflection->getProperty($property);
259+
$reflectionProperty->setAccessible(true);
260+
$reflectionProperty->setValue($object, $value);
261+
return $object;
262+
}
151263
}

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Validator/MediaTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ public function testIsValid($data, $expected)
4747
$this->assertEquals($expected['messages'], $messages);
4848
}
4949

50+
public function testIsValidClearMessagesCall()
51+
{
52+
$media = $this->getMock(
53+
'\Magento\CatalogImportExport\Model\Import\Product\Validator\Media',
54+
['_clearMessages'],
55+
[],
56+
'',
57+
false
58+
);
59+
$media->expects($this->once())->method('_clearMessages');
60+
61+
$media->isValid([]);
62+
}
63+
5064
/**
5165
* @return array
5266
*/

0 commit comments

Comments
 (0)