22
33namespace JustBetter \MagentoAsync \Tests \Actions ;
44
5+ use Illuminate \Support \Carbon ;
56use JustBetter \MagentoAsync \Actions \CleanBulkRequests ;
7+ use JustBetter \MagentoAsync \Enums \OperationStatus ;
68use JustBetter \MagentoAsync \Models \BulkRequest ;
79use JustBetter \MagentoAsync \Tests \TestCase ;
10+ use PHPUnit \Framework \Attributes \DataProvider ;
811use PHPUnit \Framework \Attributes \Test ;
912
1013class CleanBulkRequestsTest extends TestCase
1114{
1215 #[Test]
13- public function it_deletes_request (): void
16+ public function it_deletes_completed_operations (): void
1417 {
15- config ()->set ('magento-async.cleanup ' , 1 );
16-
17- BulkRequest::query ()->create ([
18+ /** @var BulkRequest $request */
19+ $ request = BulkRequest::query ()->create ([
1820 'magento_connection ' => '::magento-connection:: ' ,
1921 'store_code ' => '::store-code:: ' ,
2022 'path ' => '::path:: ' ,
@@ -24,22 +26,108 @@ public function it_deletes_request(): void
2426 'created_at ' => now (),
2527 ]);
2628
27- BulkRequest::query ()->create ([
29+ $ request ->operations ()->create ([
30+ 'operation_id ' => 1 ,
31+ 'status ' => OperationStatus::Complete,
32+ 'updated_at ' => now ()->subHours (2 ),
33+ ]);
34+
35+ $ request ->operations ()->create ([
36+ 'operation_id ' => 2 ,
37+ 'status ' => OperationStatus::Complete,
38+ ]);
39+
40+ $ request ->operations ()->create ([
41+ 'operation_id ' => 3 ,
42+ 'status ' => OperationStatus::Open,
43+ ]);
44+
45+ /** @var CleanBulkRequests $action */
46+ $ action = app (CleanBulkRequests::class);
47+ $ action ->clean ();
48+
49+ $ this ->assertEquals (2 , $ request ->operations ()->count ());
50+ }
51+
52+ /** @param array<string, mixed> $operations */
53+ #[Test]
54+ #[DataProvider('cases ' )]
55+ public function it_deletes_request (Carbon $ requestCreatedAt , array $ operations , bool $ shouldBeDeleted ): void
56+ {
57+ config ()->set ('magento-async.cleanup ' , 2 );
58+
59+ /** @var BulkRequest $request */
60+ $ request = BulkRequest::query ()->create ([
2861 'magento_connection ' => '::magento-connection:: ' ,
2962 'store_code ' => '::store-code:: ' ,
3063 'path ' => '::path:: ' ,
31- 'bulk_uuid ' => '::bulk-uuid-2 :: ' ,
64+ 'bulk_uuid ' => '::bulk-uuid-1 :: ' ,
3265 'request ' => [],
3366 'response ' => [],
34- 'created_at ' => now ()-> subHours ( 2 ) ,
67+ 'created_at ' => $ requestCreatedAt ,
3568 ]);
3669
70+ foreach ($ operations as $ operation ) {
71+ $ request ->operations ()->create ($ operation );
72+ }
73+
3774 /** @var CleanBulkRequests $action */
3875 $ action = app (CleanBulkRequests::class);
3976 $ action ->clean ();
4077
41- $ this ->assertNotNull (BulkRequest::query ()->firstWhere ('bulk_uuid ' , '= ' , '::bulk-uuid-1:: ' ));
42- $ this ->assertNull (BulkRequest::query ()->firstWhere ('bulk_uuid ' , '= ' , '::bulk-uuid-2:: ' ));
78+ $ deleted = BulkRequest::query ()->firstWhere ('bulk_uuid ' , '= ' , '::bulk-uuid-1:: ' ) === null ;
79+ $ this ->assertEquals ($ shouldBeDeleted , $ deleted );
80+ }
81+
82+ /** @return array<string, mixed> */
83+ public static function cases (): array
84+ {
85+ return [
86+ 'Pending operation ' => [
87+ 'requestCreatedAt ' => now (),
88+ 'operations ' => [
89+ [
90+ 'operation_id ' => 1 ,
91+ 'status ' => null ,
92+ ],
93+ ],
94+ 'shouldBeDeleted ' => false ,
95+ ],
96+
97+ 'Completed operations ' => [
98+ 'requestCreatedAt ' => now ()->subHours (2 ),
99+ 'operations ' => [
100+ [
101+ 'operation_id ' => 1 ,
102+ 'status ' => OperationStatus::Complete,
103+ 'updated_at ' => now ()->subHours (2 ),
104+ ],
105+ ],
106+ 'shouldBeDeleted ' => true ,
107+ ],
108+
109+ 'Failed operations ' => [
110+ 'requestCreatedAt ' => now ()->subHours (1 ),
111+ 'operations ' => [
112+ [
113+ 'operation_id ' => 1 ,
114+ 'status ' => OperationStatus::RetriablyFailed,
115+ ],
116+ ],
117+ 'shouldBeDeleted ' => false ,
118+ ],
119+
120+ 'Cleanup time ' => [
121+ 'requestCreatedAt ' => now ()->subWeek (),
122+ 'operations ' => [
123+ [
124+ 'operation_id ' => 1 ,
125+ 'status ' => OperationStatus::RetriablyFailed,
126+ ],
127+ ],
128+ 'shouldBeDeleted ' => true ,
129+ ],
43130
131+ ];
44132 }
45133}
0 commit comments