44
55use App \Models \Actionlog as ActionLog ;
66use App \Models \Component ;
7- use Database \Factories \ComponentFactory ;
8- use Illuminate \Support \Str ;
9- use Database \Factories \UserFactory ;
10- use Database \Factories \ImportFactory ;
11- use PHPUnit \Framework \Attributes \Test ;
7+ use App \Models \Import ;
8+ use App \Models \User ;
129use Illuminate \Foundation \Testing \WithFaker ;
1310use Illuminate \Support \Facades \Notification ;
11+ use Illuminate \Support \Str ;
1412use Illuminate \Testing \TestResponse ;
15- use PHPUnit \Framework \Attributes \DataProvider ;
13+ use PHPUnit \Framework \Attributes \Test ;
14+ use Tests \Concerns \TestsPermissionsRequirement ;
15+ use Tests \Support \Importing \CleansUpImportFiles ;
1616use Tests \Support \Importing \ComponentsImportFileBuilder as ImportFileBuilder ;
1717
18- class ImportComponentsTest extends ImportDataTestCase
18+ class ImportComponentsTest extends ImportDataTestCase implements TestsPermissionsRequirement
1919{
20+ use CleansUpImportFiles;
2021 use WithFaker;
2122
2223 protected function importFileResponse (array $ parameters = []): TestResponse
@@ -29,24 +30,19 @@ protected function importFileResponse(array $parameters = []): TestResponse
2930 }
3031
3132 #[Test]
32- #[DataProvider('permissionsTestData ' )]
33- public function onlyUserWithPermissionCanImportComponents (array |string $ permissions ): void
33+ public function testRequiresPermission ()
3434 {
35- $ permissions = collect ((array ) $ permissions )
36- ->map (fn (string $ permission ) => [$ permission => '1 ' ])
37- ->toJson ();
38-
39- $ this ->actingAsForApi (UserFactory::new ()->create (['permissions ' => $ permissions ]));
35+ $ this ->actingAsForApi (User::factory ()->create ());
4036
4137 $ this ->importFileResponse (['import ' => 44 ])->assertForbidden ();
4238 }
4339
4440 #[Test]
4541 public function userWithImportAssetsPermissionCanImportComponents (): void
4642 {
47- $ this ->actingAsForApi (UserFactory:: new ()->canImport ()->create ());
43+ $ this ->actingAsForApi (User:: factory ()->canImport ()->create ());
4844
49- $ import = ImportFactory:: new ()->component ()->create ();
45+ $ import = Import:: factory ()->component ()->create ();
5046
5147 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
5248 }
@@ -58,9 +54,9 @@ public function importComponents(): void
5854
5955 $ importFileBuilder = ImportFileBuilder::new ();
6056 $ row = $ importFileBuilder ->firstRow ();
61- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
57+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
6258
63- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
59+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
6460 $ this ->importFileResponse (['import ' => $ import ->id ])
6561 ->assertOk ()
6662 ->assertExactJson ([
@@ -79,21 +75,21 @@ public function importComponents(): void
7975 ->where ('item_id ' , $ newComponent ->id )
8076 ->sole ();
8177
82- $ this ->assertEquals ($ activityLog ->action_type , ' create ' );
83- $ this ->assertEquals ($ activityLog ->action_source , ' importer ' );
84- $ this ->assertEquals ($ activityLog -> company_id , $ newComponent ->company ->id );
78+ $ this ->assertEquals (' create ' , $ activityLog ->action_type );
79+ $ this ->assertEquals (' importer ' , $ activityLog ->action_source );
80+ $ this ->assertEquals ($ newComponent ->company ->id , $ activityLog -> company_id );
8581
86- $ this ->assertEquals ($ newComponent -> name , $ row ['itemName ' ]);
87- $ this ->assertEquals ($ newComponent ->company ->name , $ row [ ' companyName ' ] );
88- $ this ->assertEquals ($ newComponent ->category ->name , $ row [ ' category ' ] );
89- $ this ->assertEquals ($ newComponent ->location ->name , $ row [ ' location ' ] );
82+ $ this ->assertEquals ($ row ['itemName ' ], $ newComponent -> name );
83+ $ this ->assertEquals ($ row [ ' companyName ' ], $ newComponent ->company ->name );
84+ $ this ->assertEquals ($ row [ ' category ' ], $ newComponent ->category ->name );
85+ $ this ->assertEquals ($ row [ ' location ' ], $ newComponent ->location ->name );
9086 $ this ->assertNull ($ newComponent ->supplier_id );
91- $ this ->assertEquals ($ newComponent -> qty , $ row ['quantity ' ]);
92- $ this ->assertEquals ($ newComponent -> order_number , $ row ['orderNumber ' ]);
93- $ this ->assertEquals ($ newComponent ->purchase_date ->toDateString (), $ row [ ' purchaseDate ' ] );
94- $ this ->assertEquals ($ newComponent -> purchase_cost , $ row ['purchaseCost ' ]);
87+ $ this ->assertEquals ($ row ['quantity ' ], $ newComponent -> qty );
88+ $ this ->assertEquals ($ row ['orderNumber ' ], $ newComponent -> order_number );
89+ $ this ->assertEquals ($ row [ ' purchaseDate ' ], $ newComponent ->purchase_date ->toDateString ());
90+ $ this ->assertEquals ($ row ['purchaseCost ' ], $ newComponent -> purchase_cost );
9591 $ this ->assertNull ($ newComponent ->min_amt );
96- $ this ->assertEquals ($ newComponent -> serial , $ row ['serialNumber ' ]);
92+ $ this ->assertEquals ($ row ['serialNumber ' ], $ newComponent -> serial );
9793 $ this ->assertNull ($ newComponent ->image );
9894 $ this ->assertNull ($ newComponent ->notes );
9995 }
@@ -106,26 +102,26 @@ public function willIgnoreUnknownColumnsWhenFileContainsUnknownColumns(): void
106102
107103 $ importFileBuilder = new ImportFileBuilder ([$ row ]);
108104
109- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
105+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
110106
111- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
107+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
112108
113109 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
114110 }
115111
116112 #[Test]
117113 public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExists (): void
118114 {
119- $ component = ComponentFactory:: new ()->create ();
115+ $ component = Component:: factory ()->create ();
120116
121117 $ importFileBuilder = ImportFileBuilder::times (4 )->replace ([
122118 'itemName ' => $ component ->name ,
123119 'serialNumber ' => $ component ->serial
124120 ]);
125121
126- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
122+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
127123
128- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
124+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
129125 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
130126
131127 $ probablyNewComponents = Component::query ()
@@ -141,9 +137,9 @@ public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExi
141137 public function willNotCreateNewCompanyWhenCompanyExists (): void
142138 {
143139 $ importFileBuilder = ImportFileBuilder::times (4 )->replace (['companyName ' => Str::random ()]);
144- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
140+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
145141
146- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
142+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
147143 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
148144
149145 $ newComponents = Component::query ()
@@ -157,9 +153,9 @@ public function willNotCreateNewCompanyWhenCompanyExists(): void
157153 public function willNotCreateNewLocationWhenLocationExists (): void
158154 {
159155 $ importFileBuilder = ImportFileBuilder::times (4 )->replace (['location ' => Str::random ()]);
160- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
156+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
161157
162- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
158+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
163159 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
164160
165161 $ newComponents = Component::query ()
@@ -173,9 +169,9 @@ public function willNotCreateNewLocationWhenLocationExists(): void
173169 public function willNotCreateNewCategoryWhenCategoryExists (): void
174170 {
175171 $ importFileBuilder = ImportFileBuilder::times (4 )->replace (['category ' => $ this ->faker ->company ]);
176- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
172+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
177173
178- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
174+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
179175 $ this ->importFileResponse (['import ' => $ import ->id ])->assertOk ();
180176
181177 $ newComponents = Component::query ()
@@ -193,9 +189,9 @@ public function whenRequiredColumnsAreMissingInImportFile(): void
193189 ->forget (['quantity ' ]);
194190
195191 $ row = $ importFileBuilder ->firstRow ();
196- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
192+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
197193
198- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
194+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
199195
200196 $ this ->importFileResponse (['import ' => $ import ->id ])
201197 ->assertInternalServerError ()
@@ -222,35 +218,35 @@ public function whenRequiredColumnsAreMissingInImportFile(): void
222218 #[Test]
223219 public function updateComponentFromImport (): void
224220 {
225- $ component = ComponentFactory:: new ()->create ();
221+ $ component = Component:: factory ()->create ();
226222 $ importFileBuilder = ImportFileBuilder::new ([
227223 'itemName ' => $ component ->name ,
228224 'serialNumber ' => $ component ->serial
229225 ]);
230226
231227 $ row = $ importFileBuilder ->firstRow ();
232- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
228+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
233229
234- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
230+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
235231 $ this ->importFileResponse (['import ' => $ import ->id , 'import-update ' => true ])->assertOk ();
236232
237233 $ updatedComponent = Component::query ()
238234 ->with (['location ' , 'category ' ])
239235 ->where ('serial ' , $ row ['serialNumber ' ])
240236 ->sole ();
241237
242- $ this ->assertEquals ($ updatedComponent -> name , $ row ['itemName ' ]);
243- $ this ->assertEquals ($ updatedComponent ->category ->name , $ row [ ' category ' ] );
244- $ this ->assertEquals ($ updatedComponent ->location ->name , $ row [ ' location ' ] );
245- $ this ->assertEquals ($ updatedComponent ->supplier_id , $ component ->supplier_id );
246- $ this ->assertEquals ($ updatedComponent -> qty , $ row ['quantity ' ]);
247- $ this ->assertEquals ($ updatedComponent -> order_number , $ row ['orderNumber ' ]);
248- $ this ->assertEquals ($ updatedComponent ->purchase_date ->toDateString (), $ row [ ' purchaseDate ' ] );
249- $ this ->assertEquals ($ updatedComponent -> purchase_cost , $ row ['purchaseCost ' ]);
250- $ this ->assertEquals ($ updatedComponent ->min_amt , $ component ->min_amt );
251- $ this ->assertEquals ($ updatedComponent -> serial , $ row ['serialNumber ' ]);
252- $ this ->assertEquals ($ updatedComponent ->image , $ component ->image );
253- $ this ->assertEquals ($ updatedComponent ->notes , $ component ->notes );
238+ $ this ->assertEquals ($ row ['itemName ' ], $ updatedComponent -> name );
239+ $ this ->assertEquals ($ row [ ' category ' ], $ updatedComponent ->category ->name );
240+ $ this ->assertEquals ($ row [ ' location ' ], $ updatedComponent ->location ->name );
241+ $ this ->assertEquals ($ component ->supplier_id , $ updatedComponent ->supplier_id );
242+ $ this ->assertEquals ($ row ['quantity ' ], $ updatedComponent -> qty );
243+ $ this ->assertEquals ($ row ['orderNumber ' ], $ updatedComponent -> order_number );
244+ $ this ->assertEquals ($ row [ ' purchaseDate ' ], $ updatedComponent ->purchase_date ->toDateString ());
245+ $ this ->assertEquals ($ row ['purchaseCost ' ], $ updatedComponent -> purchase_cost );
246+ $ this ->assertEquals ($ component ->min_amt , $ updatedComponent ->min_amt );
247+ $ this ->assertEquals ($ row ['serialNumber ' ], $ updatedComponent -> serial );
248+ $ this ->assertEquals ($ component ->image , $ updatedComponent ->image );
249+ $ this ->assertEquals ($ component ->notes , $ updatedComponent ->notes );
254250 }
255251
256252 #[Test]
@@ -270,9 +266,9 @@ public function customColumnMapping(): void
270266 ];
271267
272268 $ importFileBuilder = new ImportFileBuilder ([$ row ]);
273- $ import = ImportFactory:: new ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
269+ $ import = Import:: factory ()->component ()->create (['file_path ' => $ importFileBuilder ->saveToImportsDirectory ()]);
274270
275- $ this ->actingAsForApi (UserFactory:: new ()->superuser ()->create ());
271+ $ this ->actingAsForApi (User:: factory ()->superuser ()->create ());
276272
277273 $ this ->importFileResponse ([
278274 'import ' => $ import ->id ,
@@ -294,14 +290,14 @@ public function customColumnMapping(): void
294290 ->where ('serial ' , $ importFileBuilder ->firstRow ()['category ' ])
295291 ->sole ();
296292
297- $ this ->assertEquals ($ newComponent -> name , $ row ['quantity ' ]);
298- $ this ->assertEquals ($ newComponent ->category ->name , $ row [ ' purchaseCost ' ] );
299- $ this ->assertEquals ($ newComponent ->location ->name , $ row [ ' serialNumber ' ] );
293+ $ this ->assertEquals ($ row ['quantity ' ], $ newComponent -> name );
294+ $ this ->assertEquals ($ row [ ' purchaseCost ' ], $ newComponent ->category ->name );
295+ $ this ->assertEquals ($ row [ ' serialNumber ' ], $ newComponent ->location ->name );
300296 $ this ->assertNull ($ newComponent ->supplier_id );
301- $ this ->assertEquals ($ newComponent -> qty , $ row ['companyName ' ]);
302- $ this ->assertEquals ($ newComponent -> order_number , $ row ['orderNumber ' ]);
303- $ this ->assertEquals ($ newComponent ->purchase_date ->toDateString (), $ row [ ' itemName ' ] );
304- $ this ->assertEquals ($ newComponent -> purchase_cost , $ row ['location ' ]);
297+ $ this ->assertEquals ($ row ['companyName ' ], $ newComponent -> qty );
298+ $ this ->assertEquals ($ row ['orderNumber ' ], $ newComponent -> order_number );
299+ $ this ->assertEquals ($ row [ ' itemName ' ], $ newComponent ->purchase_date ->toDateString ());
300+ $ this ->assertEquals ($ row ['location ' ], $ newComponent -> purchase_cost );
305301 $ this ->assertNull ($ newComponent ->min_amt );
306302 $ this ->assertNull ($ newComponent ->image );
307303 $ this ->assertNull ($ newComponent ->notes );
0 commit comments