3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \GroupedImportExport \Model \Import \Product \Type ;
7
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Product ;
12
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
13
+ use Magento \CatalogImportExport \Model \Import \Product as ProductImport ;
14
+ use Magento \CatalogInventory \Api \Data \StockItemInterface ;
15
+ use Magento \CatalogInventory \Api \StockConfigurationInterface ;
16
+ use Magento \CatalogInventory \Api \StockItemCriteriaInterfaceFactory ;
17
+ use Magento \CatalogInventory \Api \StockItemRepositoryInterface ;
8
18
use Magento \Framework \App \Filesystem \DirectoryList ;
19
+ use Magento \Framework \Exception \LocalizedException ;
20
+ use Magento \Framework \Filesystem ;
21
+ use Magento \Framework \ObjectManagerInterface ;
9
22
use Magento \ImportExport \Model \Import ;
23
+ use Magento \ImportExport \Model \Import \Source \Csv ;
24
+ use Magento \TestFramework \Helper \Bootstrap ;
25
+ use PHPUnit \Framework \TestCase ;
10
26
11
- class GroupedTest extends \ PHPUnit \ Framework \ TestCase
27
+ class GroupedTest extends TestCase
12
28
{
13
29
/**
14
30
* Configurable product test Name
@@ -21,26 +37,29 @@ class GroupedTest extends \PHPUnit\Framework\TestCase
21
37
const TEST_PRODUCT_TYPE = 'grouped ' ;
22
38
23
39
/**
24
- * @var \Magento\CatalogImportExport\Model\Import\Product
40
+ * @var ProductImport
25
41
*/
26
- protected $ model ;
42
+ private $ model ;
27
43
28
44
/**
29
- * @var \Magento\Framework\ ObjectManagerInterface
45
+ * @var ObjectManagerInterface
30
46
*/
31
- protected $ objectManager ;
47
+ private $ objectManager ;
32
48
33
49
/**
34
50
* Grouped product options SKU list
35
51
*
36
52
* @var array
37
53
*/
38
- protected $ optionSkuList = ['Simple for Grouped 1 ' , 'Simple for Grouped 2 ' ];
54
+ private $ optionSkuList = ['Simple for Grouped 1 ' , 'Simple for Grouped 2 ' ];
39
55
56
+ /**
57
+ * @ingeritdoc
58
+ */
40
59
protected function setUp (): void
41
60
{
42
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
43
- $ this ->model = $ this ->objectManager ->create (\ Magento \ CatalogImportExport \ Model \ Import \Product ::class);
61
+ $ this ->objectManager = Bootstrap::getObjectManager ();
62
+ $ this ->model = $ this ->objectManager ->create (ProductImport ::class);
44
63
}
45
64
46
65
/**
@@ -52,33 +71,12 @@ public function testImport()
52
71
{
53
72
// Import data from CSV file
54
73
$ pathToFile = __DIR__ . '/../../_files/grouped_product.csv ' ;
55
- $ filesystem = $ this ->objectManager -> create (\ Magento \ Framework \Filesystem::class );
74
+ $ this ->import ( $ pathToFile );
56
75
57
- $ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
58
- $ source = $ this ->objectManager ->create (
59
- \Magento \ImportExport \Model \Import \Source \Csv::class,
60
- [
61
- 'file ' => $ pathToFile ,
62
- 'directory ' => $ directory
63
- ]
64
- );
65
- $ errors = $ this ->model ->setSource (
66
- $ source
67
- )->setParameters (
68
- [
69
- 'behavior ' => \Magento \ImportExport \Model \Import::BEHAVIOR_APPEND ,
70
- 'entity ' => 'catalog_product '
71
- ]
72
- )->validateData ();
73
-
74
- $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
75
- $ this ->model ->importData ();
76
-
77
- $ resource = $ this ->objectManager ->get (\Magento \Catalog \Model \ResourceModel \Product::class);
76
+ $ resource = $ this ->objectManager ->get (ProductResource::class);
78
77
$ productId = $ resource ->getIdBySku ('Test Grouped ' );
79
78
$ this ->assertIsNumeric ($ productId );
80
- /** @var \Magento\Catalog\Model\Product $product */
81
- $ product = $ this ->objectManager ->create (\Magento \Catalog \Model \Product::class);
79
+ $ product = $ this ->objectManager ->create (Product::class);
82
80
$ product ->load ($ productId );
83
81
84
82
$ this ->assertFalse ($ product ->isObjectNew ());
@@ -91,4 +89,71 @@ public function testImport()
91
89
$ this ->assertContains ($ childProduct ->getSku (), $ this ->optionSkuList );
92
90
}
93
91
}
92
+
93
+ /**
94
+ * Verify grouped product stock status updated during import.
95
+ *
96
+ * @magentoDataFixture Magento/GroupedProduct/_files/product_grouped.php
97
+ * @return void
98
+ */
99
+ public function testImportUpdateStockStatus (): void
100
+ {
101
+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
102
+ //Verify grouped product is out of stock after import.
103
+ $ pathToOutOfStockFile = __DIR__ . '/../../_files/grouped_product_children_out_of_stock.csv ' ;
104
+ $ this ->import ($ pathToOutOfStockFile );
105
+ $ groupedProduct = $ productRepository ->get ('grouped-product ' , false , null , true );
106
+ $ stockItem = $ this ->getStockItem ((int )$ groupedProduct ->getId ());
107
+ self ::assertFalse ($ stockItem ->getIsInStock ());
108
+ //Verify grouped product is in stock after import.
109
+ $ pathToOutOfStockFile = __DIR__ . '/../../_files/grouped_product_children_in_stock.csv ' ;
110
+ $ this ->import ($ pathToOutOfStockFile );
111
+ $ groupedProduct = $ productRepository ->get ('grouped-product ' , false , null , true );
112
+ $ stockItem = $ this ->getStockItem ((int )$ groupedProduct ->getId ());
113
+ self ::assertTrue ($ stockItem ->getIsInStock ());
114
+ }
115
+
116
+ /**
117
+ * Retrieve product stock status.
118
+ *
119
+ * @param int $productId
120
+ * @return StockItemInterface|null
121
+ */
122
+ private function getStockItem (int $ productId ): ?StockItemInterface
123
+ {
124
+ $ criteriaFactory = $ this ->objectManager ->create (StockItemCriteriaInterfaceFactory::class);
125
+ $ stockItemRepository = $ this ->objectManager ->create (StockItemRepositoryInterface::class);
126
+ $ stockConfiguration = $ this ->objectManager ->create (StockConfigurationInterface::class);
127
+ $ criteria = $ criteriaFactory ->create ();
128
+ $ criteria ->setScopeFilter ($ stockConfiguration ->getDefaultScopeId ());
129
+ $ criteria ->setProductsFilter ($ productId );
130
+ $ items = $ stockItemRepository ->getList ($ criteria )->getItems ();
131
+
132
+ return reset ($ items );
133
+ }
134
+
135
+
136
+ /**
137
+ * Perform products import.
138
+ *
139
+ * @param string $pathToFile
140
+ * @throws LocalizedException
141
+ */
142
+ private function import (string $ pathToFile ): void
143
+ {
144
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
145
+ $ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
146
+ $ source = $ this ->objectManager ->create (Csv::class, ['file ' => $ pathToFile , 'directory ' => $ directory ]);
147
+ $ errors = $ this ->model ->setSource (
148
+ $ source
149
+ )->setParameters (
150
+ [
151
+ 'behavior ' => Import::BEHAVIOR_APPEND ,
152
+ 'entity ' => 'catalog_product ' ,
153
+ ]
154
+ )->validateData ();
155
+
156
+ $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
157
+ $ this ->model ->importData ();
158
+ }
94
159
}
0 commit comments