Skip to content

Commit 89ecfe7

Browse files
committed
Merge branch 'MAGNIMEX-SPRINT2-AK-UNIT-TESTS' into MAGNIMEX-SPRINT2
Conflicts: lib/internal/Magento/Framework/Archive/Test/Unit/ZipTest.php
2 parents 5ab157b + 4805f82 commit 89ecfe7

File tree

9 files changed

+397
-10
lines changed

9 files changed

+397
-10
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,58 @@ 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+
* Test for isRowValid()
352+
*/
353+
public function testIsRowValid()
354+
{
355+
$this->entityModel->expects($this->any())->method('getRowScope')->will($this->returnValue(-1));
356+
$rowData = [
357+
'price_type' => 'fixed',
358+
'price_view' => 'bundle_price_view'
359+
];
360+
$this->assertEquals($this->bundle->isRowValid($rowData, 0), true);
361+
}
362+
363+
/**
364+
* @param $object
365+
* @param $property
366+
* @param $value
367+
*/
368+
protected function setPropertyValue(&$object, $property, $value)
369+
{
370+
$reflection = new \ReflectionClass(get_class($object));
371+
$reflectionProperty = $reflection->getProperty($property);
372+
$reflectionProperty->setAccessible(true);
373+
$reflectionProperty->setValue($object, $value);
374+
return $object;
375+
}
322376
}

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1919
* @SuppressWarnings(PHPMD.TooManyFields)
20+
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
2021
*
2122
*/
2223
class ProductTest extends \PHPUnit_Framework_TestCase
@@ -385,6 +386,80 @@ public function testGetCategoryCollection()
385386
$this->assertInstanceOf('\Magento\Framework\Data\Collection', $this->model->getCategoryCollection());
386387
}
387388

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

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,17 @@ protected function setUp()
8080
'',
8181
false
8282
);
83+
8384
$categoryColFactory->method('create')->will($this->returnValue($categoryCollection));
8485

85-
$categoryFactory = $this->getMockBuilder('Magento\Catalog\Model\CategoryFactory')
86-
->disableOriginalConstructor()
87-
->getMock();
86+
$categoryFactory = $this->getMock(
87+
'Magento\Catalog\Model\CategoryFactory',
88+
['create'],
89+
[],
90+
'',
91+
false
92+
);
93+
8894
$categoryFactory->method('create')->will($this->returnValue($childCategory));
8995

9096
$this->categoryProcessor =

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ protected function setUp()
5050
'Magento\Tax\Model\Resource\TaxClass\Collection',
5151
[$taxClass]
5252
);
53+
5354
$taxClassCollectionFactory = $this->getMock(
5455
'Magento\Tax\Model\Resource\TaxClass\CollectionFactory',
5556
['create'],
5657
[],
5758
'',
5859
false
5960
);
61+
6062
$taxClassCollectionFactory->method('create')->will($this->returnValue($taxClassCollection));
6163

6264
$anotherTaxClass = $this->getMockBuilder('Magento\Tax\Model\ClassModel')
@@ -72,6 +74,7 @@ protected function setUp()
7274
'',
7375
false
7476
);
77+
7578
$taxClassFactory->method('create')->will($this->returnValue($anotherTaxClass));
7679

7780
$this->taxClassProcessor =

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
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
14+
*
15+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1316
*/
1417
class AbstractTypeTest extends \PHPUnit_Framework_TestCase
1518
{
@@ -28,6 +31,11 @@ class AbstractTypeTest extends \PHPUnit_Framework_TestCase
2831
*/
2932
protected $objectManagerHelper;
3033

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

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

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)