1616use Magento \Framework \App \Filesystem \DirectoryList ;
1717use Magento \Framework \Exception \NoSuchEntityException ;
1818use Magento \Framework \Filesystem ;
19+ use Magento \Framework \MessageQueue \MessageEncoder ;
20+ use Magento \Framework \MessageQueue \QueueFactoryInterface ;
21+ use Magento \Framework \ObjectManagerInterface ;
1922use Magento \ImportExport \Model \Import ;
2023use Magento \ImportExport \Model \Import \Source \Csv ;
24+ use Magento \ImportExport \Model \Import \Source \CsvFactory ;
2125use Magento \InventoryApi \Api \Data \SourceItemInterface ;
2226use Magento \InventoryApi \Api \Data \SourceItemSearchResultsInterface ;
2327use Magento \InventoryApi \Api \SourceItemRepositoryInterface ;
28+ use Magento \InventoryCatalog \Model \DeleteSourceItemsBySkus ;
2429use Magento \InventoryCatalogApi \Api \DefaultSourceProviderInterface ;
30+ use Magento \InventoryLowQuantityNotification \Model \ResourceModel \SourceItemConfiguration \GetBySku ;
31+ use Magento \MysqlMq \Model \Driver \Queue ;
2532use Magento \TestFramework \Helper \Bootstrap ;
33+ use Magento \TestFramework \MysqlMq \DeleteTopicRelatedMessages ;
2634use PHPUnit \Framework \TestCase ;
2735
2836/**
3341 */
3442class ProductTest extends TestCase
3543{
44+ /**
45+ * @var ObjectManagerInterface
46+ */
47+ private $ objectManager ;
48+
3649 /**
3750 * @var DefaultSourceProviderInterface
3851 */
@@ -63,26 +76,50 @@ class ProductTest extends TestCase
6376 */
6477 private $ importedProducts ;
6578
79+ /**
80+ * @var Queue
81+ */
82+ private $ queue ;
83+
84+ /**
85+ * @var MessageEncoder
86+ */
87+ private $ messageEncoder ;
88+
89+ /**
90+ * @var DeleteSourceItemsBySkus
91+ */
92+ private $ consumer ;
93+
94+ /**
95+ * @var DeleteTopicRelatedMessages
96+ */
97+ private $ deleteTopicMessages ;
98+
99+ /**
100+ * @var GetBySku
101+ */
102+ private $ getBySku ;
103+
66104 /**
67105 * Setup Test for Product Import
68106 */
69107 public function setUp (): void
70108 {
71- $ this ->defaultSourceProvider = Bootstrap::getObjectManager ()->get (
72- DefaultSourceProviderInterface::class
73- );
74- $ this ->filesystem = Bootstrap::getObjectManager ()->get (
75- Filesystem::class
76- );
77- $ this ->productImporterFactory = Bootstrap::getObjectManager ()->get (
78- ProductFactory::class
79- );
80- $ this ->searchCriteriaBuilderFactory = Bootstrap::getObjectManager ()->get (
81- SearchCriteriaBuilderFactory::class
82- );
83- $ this ->sourceItemRepository = Bootstrap::getObjectManager ()->get (
84- SourceItemRepositoryInterface::class
109+ $ this ->objectManager = Bootstrap::getObjectManager ();
110+ $ this ->defaultSourceProvider = $ this ->objectManager ->get (DefaultSourceProviderInterface::class);
111+ $ this ->filesystem = $ this ->objectManager ->get (Filesystem::class);
112+ $ this ->productImporterFactory = $ this ->objectManager ->get (ProductFactory::class);
113+ $ this ->searchCriteriaBuilderFactory = $ this ->objectManager ->get (SearchCriteriaBuilderFactory::class);
114+ $ this ->sourceItemRepository = $ this ->objectManager ->get (SourceItemRepositoryInterface::class);
115+ $ this ->queue = $ this ->objectManager ->get (QueueFactoryInterface::class)->create (
116+ 'inventory.source.items.cleanup ' ,
117+ 'db '
85118 );
119+ $ this ->messageEncoder = $ this ->objectManager ->get (MessageEncoder::class);
120+ $ this ->consumer = $ this ->objectManager ->get (DeleteSourceItemsBySkus::class);
121+ $ this ->deleteTopicMessages = $ this ->objectManager ->get (DeleteTopicRelatedMessages::class);
122+ $ this ->getBySku = $ this ->objectManager ->get (GetBySku::class);
86123 }
87124
88125 /**
@@ -100,7 +137,7 @@ public function testSourceItemCreatedOnProductImport(): void
100137 $ productImporterModel ->importData ();
101138 $ sku = 'example_simple_for_source_item ' ;
102139 $ compareData = $ this ->buildDataArray (
103- $ this ->getSourceItemList ()->getItems ()
140+ $ this ->getSourceItemList (' example_simple_for_source_item ' )->getItems ()
104141 );
105142 $ expectedData = [
106143 SourceItemInterface::SKU => $ sku ,
@@ -134,7 +171,7 @@ public function testSourceItemUpdatedOnProductImport(): void
134171 $ productImporterModel ->importData ();
135172 $ sku = 'example_simple_for_source_item ' ;
136173 $ compareData = $ this ->buildDataArray (
137- $ this ->getSourceItemList ()->getItems ()
174+ $ this ->getSourceItemList (' example_simple_for_source_item ' )->getItems ()
138175 );
139176 $ expectedData = [
140177 SourceItemInterface::SKU => $ sku ,
@@ -153,19 +190,59 @@ public function testSourceItemUpdatedOnProductImport(): void
153190 $ this ->importedProducts = [$ sku ];
154191 }
155192
193+ /**
194+ * @magentoConfigFixture default/cataloginventory/options/synchronize_with_catalog 1
195+ *
196+ * @magentoDataFixture Magento_InventoryApi::Test/_files/products.php
197+ * @magentoDataFixture Magento_InventoryApi::Test/_files/sources.php
198+ * @magentoDataFixture Magento_InventoryApi::Test/_files/source_items.php
199+ * @magentoDataFixture Magento_InventoryLowQuantityNotificationApi::Test/_files/source_item_configuration.php
200+ *
201+ * @return void
202+ */
203+ public function testSourceItemDeletedOnProductImport (): void
204+ {
205+ $ this ->deleteTopicMessages ->execute ('inventory.source.items.cleanup ' );
206+ $ pathToFile = __DIR__ . '/_files/product_import_SKU-1.csv ' ;
207+ $ productSku = 'SKU-1 ' ;
208+ $ productImporterModel = $ this ->getProductImporterModel ($ pathToFile , Import::BEHAVIOR_DELETE );
209+ $ errors = $ productImporterModel ->validateData ();
210+ $ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
211+ $ productImporterModel ->importData ();
212+ $ this ->importedProducts [] = $ productSku ;
213+ $ this ->processMessages ('inventory.source.items.cleanup ' );
214+
215+ $ this ->assertEmpty ($ this ->getSourceItemList ($ productSku )->getItems ());
216+ $ this ->assertEmpty ($ this ->getBySku ->execute ($ productSku ));
217+ }
218+
219+ /**
220+ * Process topic messages
221+ *
222+ * @param string $topicName
223+ * @return void
224+ */
225+ private function processMessages (string $ topicName ): void
226+ {
227+ $ envelope = $ this ->queue ->dequeue ();
228+ $ decodedMessage = $ this ->messageEncoder ->decode ($ topicName , $ envelope ->getBody ());
229+ $ this ->consumer ->execute ($ decodedMessage );
230+ }
231+
156232 /**
157233 * Get List of Source Items which match SKU and Source ID of dummy data
158234 *
235+ * @param string $sku
159236 * @return SourceItemSearchResultsInterface
160237 */
161- private function getSourceItemList (): SourceItemSearchResultsInterface
238+ private function getSourceItemList (string $ sku ): SourceItemSearchResultsInterface
162239 {
163240 /** @var SearchCriteriaBuilder $searchCriteria */
164241 $ searchCriteriaBuilder = $ this ->searchCriteriaBuilderFactory ->create ();
165242
166243 $ searchCriteriaBuilder ->addFilter (
167244 SourceItemInterface::SKU ,
168- ' example_simple_for_source_item '
245+ $ sku
169246 );
170247
171248 $ searchCriteriaBuilder ->addFilter (
@@ -206,15 +283,14 @@ private function buildDataArray(array $sourceItems): array
206283 * @return Product
207284 */
208285 private function getProductImporterModel (
209- $ pathToFile ,
210- $ behavior = Import::BEHAVIOR_ADD_UPDATE
286+ string $ pathToFile ,
287+ string $ behavior = Import::BEHAVIOR_ADD_UPDATE
211288 ): Product {
212289 /** @var Filesystem\Directory\WriteInterface $directory */
213290 $ directory = $ this ->filesystem
214291 ->getDirectoryWrite (DirectoryList::ROOT );
215292 /** @var Csv $source */
216- $ source = Bootstrap::getObjectManager ()->create (
217- Csv::class,
293+ $ source = $ this ->objectManager ->get (CsvFactory::class)->create (
218294 [
219295 'file ' => $ pathToFile ,
220296 'directory ' => $ directory
@@ -234,7 +310,6 @@ private function getProductImporterModel(
234310 /**
235311 * Cleanup test by removing products.
236312 *
237- * @param string[] $skus
238313 * @return void
239314 */
240315 protected function tearDown (): void
0 commit comments