33namespace JustBetter \MagentoStock \Tests \Actions ;
44
55use Illuminate \Support \Facades \Bus ;
6+ use JustBetter \MagentoAsync \Enums \OperationStatus ;
7+ use JustBetter \MagentoAsync \Models \BulkRequest ;
68use JustBetter \MagentoClient \Contracts \ChecksMagento ;
9+ use JustBetter \MagentoProducts \Models \MagentoProduct ;
710use JustBetter \MagentoStock \Actions \ProcessStocks ;
811use JustBetter \MagentoStock \Jobs \Retrieval \RetrieveStockJob ;
912use JustBetter \MagentoStock \Jobs \Update \UpdateStockAsyncJob ;
@@ -58,6 +61,11 @@ public function it_dispatches_async_update_job(): void
5861 Bus::fake ();
5962 config ()->set ('magento-stock.async ' , true );
6063
64+ MagentoProduct::query ()->create ([
65+ 'sku ' => '::sku:: ' ,
66+ 'exists_in_magento ' => true ,
67+ ]);
68+
6169 Stock::query ()->create ([
6270 'sku ' => '::sku:: ' ,
6371 'update ' => true ,
@@ -70,6 +78,65 @@ public function it_dispatches_async_update_job(): void
7078 Bus::assertDispatched (UpdateStockAsyncJob::class);
7179 }
7280
81+ #[Test]
82+ public function it_does_not_dispatch_stocks_with_open_async_operations (): void
83+ {
84+ Bus::fake ();
85+ config ()->set ('magento-stock.async ' , true );
86+
87+ MagentoProduct::query ()->create ([
88+ 'sku ' => '::sku_1:: ' ,
89+ 'exists_in_magento ' => true ,
90+ ]);
91+
92+ MagentoProduct::query ()->create ([
93+ 'sku ' => '::sku_2:: ' ,
94+ 'exists_in_magento ' => true ,
95+ ]);
96+
97+ /** @var Stock $stock */
98+ $ stock = Stock::query ()->create ([
99+ 'sku ' => '::sku_1:: ' ,
100+ 'update ' => true ,
101+ ]);
102+
103+ /** @var BulkRequest $request */
104+ $ request = BulkRequest::query ()->create ([
105+ 'magento_connection ' => '::magento-connection:: ' ,
106+ 'store_code ' => '::store-code:: ' ,
107+ 'method ' => 'POST ' ,
108+ 'path ' => '::path:: ' ,
109+ 'bulk_uuid ' => '::bulk-uuid-1:: ' ,
110+ 'request ' => [
111+ [
112+ 'call-1 ' ,
113+ ],
114+ ],
115+ 'response ' => [],
116+ 'created_at ' => now (),
117+ ]);
118+
119+ $ request ->operations ()->create ([
120+ 'operation_id ' => 0 ,
121+ 'subject_type ' => $ stock ->getMorphClass (),
122+ 'subject_id ' => $ stock ->getKey (),
123+ 'status ' => OperationStatus::Open,
124+ ]);
125+
126+ Stock::query ()->create ([
127+ 'sku ' => '::sku_2:: ' ,
128+ 'update ' => true ,
129+ ]);
130+
131+ /** @var ProcessStocks $action */
132+ $ action = app (ProcessStocks::class);
133+ $ action ->process ();
134+
135+ Bus::assertDispatched (UpdateStockAsyncJob::class, function (UpdateStockAsyncJob $ job ): bool {
136+ return $ job ->stocks ->count () === 1 && $ job ->stocks ->first ()?->sku === '::sku_2:: ' ;
137+ });
138+ }
139+
73140 #[Test]
74141 public function it_does_not_dispatch_update_jobs_if_magento_is_unavailable (): void
75142 {
0 commit comments