Skip to content

Commit 1da5540

Browse files
Andrei KuprienkaAndrei Kuprienka
authored andcommitted
MAGNIMEX-120: Update unittests
1 parent c3fdb71 commit 1da5540

File tree

4 files changed

+1310
-145
lines changed

4 files changed

+1310
-145
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ protected function setUp()
7272
$categoryCollection->expects($this->any())
7373
->method('getItemById')
7474
->will($this->returnValueMap($map));
75+
$categoryCollection->expects($this->exactly(3))
76+
->method('addAttributeToSelect')
77+
->withConsecutive(
78+
array('name'),
79+
array('url_key'),
80+
array('url_path')
81+
)
82+
->will($this->returnSelf());
7583

7684
$categoryColFactory = $this->getMock(
7785
'Magento\Catalog\Model\Resource\Category\CollectionFactory',
@@ -105,4 +113,50 @@ public function testUpsertCategories()
105113
$categoryIds = $this->categoryProcessor->upsertCategories(self::CHILD_CATEGORY_NAME);
106114
$this->assertArrayHasKey(self::CHILD_CATEGORY_ID, array_flip($categoryIds));
107115
}
116+
117+
/**
118+
* Cover getCategoryById().
119+
*
120+
* @dataProvider getCategoryByIdDataProvider
121+
*/
122+
public function testGetCategoryById($categoriesCache, $expectedResult)
123+
{
124+
$categoryId = 'category_id';
125+
$this->setPropertyValue($this->categoryProcessor, 'categoriesCache', $categoriesCache);
126+
127+
$actualResult = $this->categoryProcessor->getCategoryById($categoryId);
128+
$this->assertEquals($expectedResult, $actualResult);
129+
}
130+
131+
public function getCategoryByIdDataProvider()
132+
{
133+
return [
134+
[
135+
'$categoriesCache' => [
136+
'category_id' => 'category_id value',
137+
],
138+
'$expectedResult' => 'category_id value',
139+
],
140+
[
141+
'$categoriesCache' => [],
142+
'$expectedResult' => null,
143+
],
144+
];
145+
}
146+
147+
/**
148+
* Set property for an object.
149+
*
150+
* @param object $object
151+
* @param string $property
152+
* @param mixed $value
153+
*/
154+
protected function setPropertyValue(&$object, $property, $value)
155+
{
156+
$reflection = new \ReflectionClass(get_class($object));
157+
$reflectionProperty = $reflection->getProperty($property);
158+
$reflectionProperty->setAccessible(true);
159+
$reflectionProperty->setValue($object, $value);
160+
return $object;
161+
}
108162
}

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/ProductTest.php

Lines changed: 105 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ class ProductTest extends \PHPUnit_Framework_TestCase
144144
/** @var \Magento\Framework\Model\Resource\Db\TransactionManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
145145
protected $transactionManager;
146146

147-
/** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
148-
protected $catalogProductFactory;
149-
150147
/** @var \Magento\CatalogImportExport\Model\Import\Product\TaxClassProcessor|\PHPUnit_Framework_MockObject_MockObject */
151148
// @codingStandardsIgnoreEnd
152149
protected $taxClassProcessor;
@@ -282,6 +279,9 @@ protected function setUp()
282279
->getMock();
283280
$this->storeResolver =
284281
$this->getMockBuilder('\Magento\CatalogImportExport\Model\Import\Product\StoreResolver')
282+
->setMethods([
283+
'getStoreCodeToId',
284+
])
285285
->disableOriginalConstructor()
286286
->getMock();
287287
$this->skuProcessor =
@@ -303,13 +303,6 @@ protected function setUp()
303303
$this->transactionManager =
304304
$this->getMockBuilder('\Magento\Framework\Model\Resource\Db\TransactionManagerInterface')
305305
->getMock();
306-
$this->catalogProductFactory = $this->getMock(
307-
'\Magento\Catalog\Model\ProductFactory',
308-
['create'],
309-
[],
310-
'',
311-
false
312-
);
313306

314307
$this->taxClassProcessor =
315308
$this->getMockBuilder('\Magento\CatalogImportExport\Model\Import\Product\TaxClassProcessor')
@@ -355,7 +348,6 @@ protected function setUp()
355348
$this->skuProcessor,
356349
$this->categoryProcessor,
357350
$this->validator,
358-
$this->catalogProductFactory,
359351
$this->objectRelationProcessor,
360352
$this->transactionManager,
361353
$this->taxClassProcessor,
@@ -479,33 +471,6 @@ protected function _initSkus()
479471
return $this;
480472
}
481473

482-
public function testGetAffectedProducts()
483-
{
484-
$testProduct = 'test_product';
485-
$rowData = ['data'];
486-
$rowNum = 666;
487-
$importProduct = $this->getMockBuilder('\Magento\CatalogImportExport\Model\Import\Product')
488-
->disableOriginalConstructor()
489-
->setMethods(['isRowAllowedToImport', '_populateToUrlGeneration'])
490-
->getMock();
491-
492-
$this->_dataSourceModel->expects($this->exactly(2))->method('getNextBunch')->willReturnOnConsecutiveCalls(
493-
[
494-
$rowNum => $rowData
495-
],
496-
null
497-
);
498-
$this->setPropertyValue($importProduct, '_dataSourceModel', $this->_dataSourceModel);
499-
$importProduct->expects($this->once())
500-
->method('isRowAllowedToImport')
501-
->with($rowData, $rowNum)->willReturn(true);
502-
$importProduct->expects($this->once())->method('_populateToUrlGeneration')
503-
->with($rowData)
504-
->willReturn($testProduct);
505-
506-
$this->assertEquals([$testProduct], $importProduct->getAffectedProducts());
507-
}
508-
509474
public function testSaveProductAttributes()
510475
{
511476
$testTable = 'test_table';
@@ -815,6 +780,108 @@ public function testValidateRowValidatorCheck()
815780
$importProduct->validateRow($rowData, $rowNum);
816781
}
817782

783+
/**
784+
* Cover getProductWebsites().
785+
*/
786+
public function testGetProductWebsites()
787+
{
788+
$productSku = 'productSku';
789+
$productValue = [
790+
'key 1' => 'val',
791+
'key 2' => 'val',
792+
'key 3' => 'val',
793+
];
794+
$expectedResult = array_keys($productValue);
795+
$this->setPropertyValue($this->importProduct, 'websitesCache', [
796+
$productSku => $productValue
797+
]);
798+
799+
$actualResult = $this->importProduct->getProductWebsites($productSku);
800+
801+
$this->assertEquals($expectedResult, $actualResult);
802+
}
803+
804+
/**
805+
* Cover getProductCategories().
806+
*/
807+
public function testGetProductCategories()
808+
{
809+
$productSku = 'productSku';
810+
$productValue = [
811+
'key 1' => 'val',
812+
'key 2' => 'val',
813+
'key 3' => 'val',
814+
];
815+
$expectedResult = array_keys($productValue);
816+
$this->setPropertyValue($this->importProduct, 'categoriesCache', [
817+
$productSku => $productValue
818+
]);
819+
820+
$actualResult = $this->importProduct->getProductCategories($productSku);
821+
822+
$this->assertEquals($expectedResult, $actualResult);
823+
}
824+
825+
/**
826+
* Cover getStoreIdByCode().
827+
*
828+
* @dataProvider getStoreIdByCodeDataProvider
829+
*/
830+
public function testGetStoreIdByCode($storeCode, $expectedResult)
831+
{
832+
$this->storeResolver
833+
->expects($this->any())
834+
->method('getStoreCodeToId')
835+
->willReturn('getStoreCodeToId value');
836+
837+
$actualResult = $this->importProduct->getStoreIdByCode($storeCode);
838+
$this->assertEquals($expectedResult, $actualResult);
839+
}
840+
841+
/**
842+
* Cover getNewSku().
843+
*/
844+
public function testGetNewSku()
845+
{
846+
$expectedSku = 'value';
847+
$expectedResult = 'result value';
848+
849+
$this->skuProcessor
850+
->expects($this->any())
851+
->method('getNewSku')
852+
->with($expectedSku)
853+
->willReturn($expectedResult);
854+
855+
$actualResult = $this->importProduct->getNewSku($expectedSku);
856+
$this->assertEquals($expectedResult, $actualResult);
857+
}
858+
859+
/**
860+
* Cover getCategoryProcessor().
861+
*/
862+
public function testGetCategoryProcessor()
863+
{
864+
$expectedResult = 'value';
865+
$this->setPropertyValue($this->importProduct, 'categoryProcessor', $expectedResult);
866+
867+
$actualResult = $this->importProduct->getCategoryProcessor();
868+
$this->assertEquals($expectedResult, $actualResult);
869+
}
870+
871+
public function getStoreIdByCodeDataProvider()
872+
{
873+
return [
874+
[
875+
'$storeCode' => null,
876+
'$expectedResult' => \Magento\CatalogImportExport\Model\Import\Product::SCOPE_DEFAULT,
877+
],
878+
[
879+
'$storeCode' => 'value',
880+
'$expectedResult' => 'getStoreCodeToId value',
881+
],
882+
];
883+
}
884+
818885
/**
819886
* @dataProvider validateRowCheckSpecifiedSkuDataProvider
820887
*/
@@ -1154,95 +1221,6 @@ public function testValidateValidateOptionEntity()
11541221
$importProduct->validateRow($rowData, $rowNum);
11551222
}
11561223

1157-
/**
1158-
* @dataProvider populateToUrlGenerationReturnNullDataProvider
1159-
*/
1160-
public function testPopulateToUrlGenerationReturnNull($rowData, $newSku)
1161-
{
1162-
$productMock = $this->getMock(
1163-
'\Magento\Catalog\Model\Product',
1164-
['addData'],
1165-
[],
1166-
'',
1167-
false
1168-
);
1169-
1170-
$this->catalogProductFactory
1171-
->expects($this->once())
1172-
->method('create')
1173-
->willReturn($productMock);
1174-
1175-
$this->skuProcessor
1176-
->expects($this->once())
1177-
->method('getNewSku')
1178-
->willReturn($newSku);
1179-
1180-
$result = $this->importProduct->_populateToUrlGeneration($rowData);
1181-
1182-
$this->assertNull($result);
1183-
1184-
}
1185-
1186-
public function testPopulateToUrlGenerationReturnProduct()
1187-
{
1188-
$rowData = [
1189-
\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value'
1190-
];
1191-
$newSku = [
1192-
'entity_id' => 'new sku value',
1193-
];
1194-
$expectedRowData = [
1195-
\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value',
1196-
'entity_id' => $newSku['entity_id'],
1197-
];
1198-
$productMock = $this->getMock(
1199-
'\Magento\Catalog\Model\Product',
1200-
['addData'],
1201-
[],
1202-
'',
1203-
false
1204-
);
1205-
1206-
$productMock
1207-
->expects($this->once())
1208-
->method('addData')
1209-
->with($expectedRowData);
1210-
1211-
$this->catalogProductFactory
1212-
->expects($this->once())
1213-
->method('create')
1214-
->willReturn($productMock);
1215-
1216-
$this->skuProcessor
1217-
->expects($this->once())
1218-
->method('getNewSku')
1219-
->willReturn($newSku);
1220-
1221-
$result = $this->importProduct->_populateToUrlGeneration($rowData);
1222-
1223-
$this->assertEquals($productMock, $result);
1224-
}
1225-
1226-
public function populateToUrlGenerationReturnNullDataProvider()
1227-
{
1228-
return [
1229-
[
1230-
'$rowData' => [
1231-
\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value'
1232-
],
1233-
'$newSku' => [
1234-
'entity_id' => null,
1235-
],
1236-
],
1237-
[
1238-
'$rowData' => [
1239-
\Magento\CatalogImportExport\Model\Import\Product::COL_SKU => 'value'
1240-
],
1241-
'$newSku' => [],
1242-
],
1243-
];
1244-
}
1245-
12461224
public function validateRowValidateNewProductTypeAddRowErrorCallDataProvider()
12471225
{
12481226
return [

0 commit comments

Comments
 (0)