Swap factory syntax

This commit is contained in:
Marcus Moore 2024-10-03 13:59:58 -07:00
parent 88d549e7c5
commit e213053775
No known key found for this signature in database
7 changed files with 172 additions and 162 deletions

View file

@ -2,10 +2,13 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Import extends Model class Import extends Model
{ {
use HasFactory;
protected $casts = [ protected $casts = [
'header_row' => 'array', 'header_row' => 'array',
'first_row' => 'array', 'first_row' => 'array',

View file

@ -5,6 +5,7 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\Actionlog; use App\Models\Actionlog;
use App\Models\Company; use App\Models\Company;
use App\Models\Import;
use App\Models\User; use App\Models\User;
use Database\Factories\AccessoryFactory; use Database\Factories\AccessoryFactory;
use Database\Factories\CompanyFactory; use Database\Factories\CompanyFactory;
@ -43,9 +44,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function userWithImportAccessoryPermissionCanImportAccessories(): void public function userWithImportAccessoryPermissionCanImportAccessories(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->accessory()->create(); $import = Import::factory()->accessory()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -55,9 +56,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
{ {
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -108,9 +109,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -119,9 +120,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willFormatDate(): void public function willFormatDate(): void
{ {
$importFileBuilder = ImportFileBuilder::new(['purchaseDate' => '2022/10/10']); $importFileBuilder = ImportFileBuilder::new(['purchaseDate' => '2022/10/10']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$accessory = Accessory::query() $accessory = Accessory::query()
@ -135,9 +136,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewCategoryWhenCategoryExists(): void public function willNotCreateNewCategoryWhenCategoryExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query() $newAccessories = Accessory::query()
@ -150,11 +151,11 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function willNotCreateNewAccessoryWhenAccessoryWithNameExists(): void public function willNotCreateNewAccessoryWhenAccessoryWithNameExists(): void
{ {
$accessory = AccessoryFactory::new()->create(['name' => Str::random()]); $accessory = Accessory::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::times(2)->replace(['itemName' => $accessory->name]); $importFileBuilder = ImportFileBuilder::times(2)->replace(['itemName' => $accessory->name]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewAccessories = Accessory::query() $probablyNewAccessories = Accessory::query()
@ -169,9 +170,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewCompanyWhenCompanyAlreadyExists(): void public function willNotCreateNewCompanyWhenCompanyAlreadyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query() $newAccessories = Accessory::query()
@ -185,9 +186,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewLocationWhenLocationAlreadyExists(): void public function willNotCreateNewLocationWhenLocationAlreadyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query() $newAccessories = Accessory::query()
@ -201,9 +202,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewManufacturerWhenManufacturerAlreadyExists(): void public function willNotCreateNewManufacturerWhenManufacturerAlreadyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query() $newAccessories = Accessory::query()
@ -217,9 +218,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewSupplierWhenSupplierAlreadyExists(): void public function willNotCreateNewSupplierWhenSupplierAlreadyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessories = Accessory::query() $newAccessories = Accessory::query()
@ -233,9 +234,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function whenColumnsAreMissingInImportFile(): void public function whenColumnsAreMissingInImportFile(): void
{ {
$importFileBuilder = ImportFileBuilder::new()->forget(['minimumAmount', 'purchaseCost', 'purchaseDate']); $importFileBuilder = ImportFileBuilder::new()->forget(['minimumAmount', 'purchaseCost', 'purchaseDate']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAccessory = Accessory::query() $newAccessory = Accessory::query()
@ -251,9 +252,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
public function whenRequiredColumnsAreMissingInImportFile(): void public function whenRequiredColumnsAreMissingInImportFile(): void
{ {
$importFileBuilder = ImportFileBuilder::new()->forget(['itemName', 'quantity', 'category']); $importFileBuilder = ImportFileBuilder::new()->forget(['itemName', 'quantity', 'category']);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
->assertExactJson([ ->assertExactJson([
@ -274,12 +275,12 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function updateAccessoryFromImport(): void public function updateAccessoryFromImport(): void
{ {
$accessory = AccessoryFactory::new()->create(['name' => Str::random()])->refresh(); $accessory = Accessory::factory()->create(['name' => Str::random()])->refresh();
$importFileBuilder = ImportFileBuilder::new(['itemName' => $accessory->name]); $importFileBuilder = ImportFileBuilder::new(['itemName' => $accessory->name]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedAccessory = Accessory::query()->find($accessory->id); $updatedAccessory = Accessory::query()->find($accessory->id);
@ -312,7 +313,7 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function whenImportFileContainsEmptyValues(): void public function whenImportFileContainsEmptyValues(): void
{ {
$accessory = AccessoryFactory::new()->create(['name' => Str::random()]); $accessory = Accessory::factory()->create(['name' => Str::random()]);
$accessory->refresh(); $accessory->refresh();
$importFileBuilder = ImportFileBuilder::new([ $importFileBuilder = ImportFileBuilder::new([
@ -332,9 +333,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
'modelNumber' => '' 'modelNumber' => ''
]); ]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
->assertExactJson([ ->assertExactJson([
@ -352,7 +353,7 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder->replace(['itemName' => $accessory->name]); $importFileBuilder->replace(['itemName' => $accessory->name]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
@ -382,9 +383,9 @@ class ImportAccessoriesTest extends ImportDataTestCase implements TestsPermissio
]; ];
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->accessory()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,
'column-mappings' => [ 'column-mappings' => [

View file

@ -5,6 +5,7 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActionLog; use App\Models\Actionlog as ActionLog;
use App\Models\Asset; use App\Models\Asset;
use App\Models\CustomField; use App\Models\CustomField;
use App\Models\Import;
use App\Models\User; use App\Models\User;
use App\Notifications\CheckoutAssetNotification; use App\Notifications\CheckoutAssetNotification;
use Carbon\Carbon; use Carbon\Carbon;
@ -46,9 +47,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
#[Test] #[Test]
public function userWithImportAssetsPermissionCanImportAssets(): void public function userWithImportAssetsPermissionCanImportAssets(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->asset()->create(); $import = Import::factory()->asset()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -60,9 +61,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -151,9 +152,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -161,11 +162,11 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
#[Test] #[Test]
public function willNotCreateNewAssetWhenAssetWithSameTagAlreadyExists(): void public function willNotCreateNewAssetWhenAssetWithSameTagAlreadyExists(): void
{ {
$asset = AssetFactory::new()->create(['asset_tag' => $this->faker->uuid]); $asset = Asset::factory()->create(['asset_tag' => $this->faker->uuid]);
$importFileBuilder = ImportFileBuilder::times(4)->replace(['tag' => $asset->asset_tag]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['tag' => $asset->asset_tag]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
->assertExactJson([ ->assertExactJson([
@ -191,9 +192,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateNewCompanyWhenCompanyExists(): void public function willNotCreateNewCompanyWhenCompanyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -207,9 +208,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateNewLocationWhenLocationExists(): void public function willNotCreateNewLocationWhenLocationExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -223,9 +224,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateNewSupplierWhenSupplierExists(): void public function willNotCreateNewSupplierWhenSupplierExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['supplierName' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -239,9 +240,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateNewManufacturerWhenManufacturerExists(): void public function willNotCreateNewManufacturerWhenManufacturerExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -256,9 +257,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateCategoryWhenCategoryExists(): void public function willNotCreateCategoryWhenCategoryExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -273,9 +274,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
public function willNotCreateNewAssetModelWhenAssetModelExists(): void public function willNotCreateNewAssetModelWhenAssetModelExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['model' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['model' => Str::random()]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAssets = Asset::query() $newAssets = Asset::query()
@ -295,9 +296,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
'status' 'status'
]); ]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query() $newAsset = Asset::query()
@ -318,9 +319,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
'purchaseDate' => '2022/10/10' 'purchaseDate' => '2022/10/10'
]); ]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query() $newAsset = Asset::query()
@ -339,9 +340,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
->replace(['model' => '']); ->replace(['model' => '']);
$rows = $importFileBuilder->all(); $rows = $importFileBuilder->all();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
->assertJson([ ->assertJson([
@ -381,12 +382,12 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
#[Test] #[Test]
public function updateAssetFromImport(): void public function updateAssetFromImport(): void
{ {
$asset = AssetFactory::new()->create()->refresh(); $asset = Asset::factory()->create()->refresh();
$importFileBuilder = ImportFileBuilder::times(1)->replace(['tag' => $asset->asset_tag]); $importFileBuilder = ImportFileBuilder::times(1)->replace(['tag' => $asset->asset_tag]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedAsset = Asset::query() $updatedAsset = Asset::query()
@ -458,9 +459,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
]; ];
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,
@ -546,7 +547,7 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew(); $customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew();
if (!$customField->exists) { if (!$customField->exists) {
$customField = CustomFieldFactory::new()->macAddress()->create(['db_column' => '_snipeit_mac_address_1']); $customField = CustomField::factory()->macAddress()->create(['db_column' => '_snipeit_mac_address_1']);
} }
if ($customField->field_encrypted) { if ($customField->field_encrypted) {
@ -554,9 +555,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$customField->save(); $customField->save();
} }
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newAsset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole(); $newAsset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();
@ -576,7 +577,7 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew(); $customField = CustomField::query()->where('name', 'Mac Address')->firstOrNew();
if (!$customField->exists) { if (!$customField->exists) {
$customField = CustomFieldFactory::new()->macAddress()->create(); $customField = CustomField::factory()->macAddress()->create();
} }
if (!$customField->field_encrypted) { if (!$customField->field_encrypted) {
@ -584,9 +585,9 @@ class ImportAssetsTest extends ImportDataTestCase implements TestsPermissionsReq
$customField->save(); $customField->save();
} }
$import = ImportFactory::new()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->asset()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$asset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole(); $asset = Asset::query()->where('serial', $importFileBuilder->firstRow()['serialNumber'])->sole();

View file

@ -4,6 +4,7 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActionLog; use App\Models\Actionlog as ActionLog;
use App\Models\Component; use App\Models\Component;
use App\Models\Import;
use App\Models\User; use App\Models\User;
use Database\Factories\ComponentFactory; use Database\Factories\ComponentFactory;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -41,9 +42,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
#[Test] #[Test]
public function userWithImportAssetsPermissionCanImportComponents(): void public function userWithImportAssetsPermissionCanImportComponents(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->component()->create(); $import = Import::factory()->component()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -55,9 +56,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -103,9 +104,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -113,16 +114,16 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
#[Test] #[Test]
public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExists(): void public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExists(): void
{ {
$component = ComponentFactory::new()->create(); $component = Component::factory()->create();
$importFileBuilder = ImportFileBuilder::times(4)->replace([ $importFileBuilder = ImportFileBuilder::times(4)->replace([
'itemName' => $component->name, 'itemName' => $component->name,
'serialNumber' => $component->serial 'serialNumber' => $component->serial
]); ]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewComponents = Component::query() $probablyNewComponents = Component::query()
@ -138,9 +139,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
public function willNotCreateNewCompanyWhenCompanyExists(): void public function willNotCreateNewCompanyWhenCompanyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query() $newComponents = Component::query()
@ -154,9 +155,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
public function willNotCreateNewLocationWhenLocationExists(): void public function willNotCreateNewLocationWhenLocationExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query() $newComponents = Component::query()
@ -170,9 +171,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
public function willNotCreateNewCategoryWhenCategoryExists(): void public function willNotCreateNewCategoryWhenCategoryExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newComponents = Component::query() $newComponents = Component::query()
@ -190,9 +191,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
->forget(['quantity']); ->forget(['quantity']);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
@ -219,16 +220,16 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
#[Test] #[Test]
public function updateComponentFromImport(): void public function updateComponentFromImport(): void
{ {
$component = ComponentFactory::new()->create(); $component = Component::factory()->create();
$importFileBuilder = ImportFileBuilder::new([ $importFileBuilder = ImportFileBuilder::new([
'itemName' => $component->name, 'itemName' => $component->name,
'serialNumber' => $component->serial 'serialNumber' => $component->serial
]); ]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedComponent = Component::query() $updatedComponent = Component::query()
@ -267,9 +268,9 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
]; ];
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->component()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,

View file

@ -4,6 +4,7 @@ namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActivityLog; use App\Models\Actionlog as ActivityLog;
use App\Models\Consumable; use App\Models\Consumable;
use App\Models\Import;
use App\Models\User; use App\Models\User;
use Database\Factories\ConsumableFactory; use Database\Factories\ConsumableFactory;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -41,9 +42,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function userWithImportAssetsPermissionCanImportConsumables(): void public function userWithImportAssetsPermissionCanImportConsumables(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->consumable()->create(); $import = Import::factory()->consumable()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -55,9 +56,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -105,9 +106,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -115,11 +116,11 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function willNotCreateNewConsumableWhenConsumableNameAlreadyExist(): void public function willNotCreateNewConsumableWhenConsumableNameAlreadyExist(): void
{ {
$consumable = ConsumableFactory::new()->create(['name' => Str::random()]); $consumable = Consumable::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]); $importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewConsumables = Consumable::query() $probablyNewConsumables = Consumable::query()
@ -134,9 +135,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewCompanyWhenCompanyExists(): void public function willNotCreateNewCompanyWhenCompanyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query() $newConsumables = Consumable::query()
@ -150,9 +151,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewLocationWhenLocationExists(): void public function willNotCreateNewLocationWhenLocationExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['location' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query() $newConsumables = Consumable::query()
@ -166,9 +167,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
public function willNotCreateNewCategoryWhenCategoryExists(): void public function willNotCreateNewCategoryWhenCategoryExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => Str::random()]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newConsumables = Consumable::query() $newConsumables = Consumable::query()
@ -184,9 +185,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder = ImportFileBuilder::new(['category' => ''])->forget(['quantity', 'name']); $importFileBuilder = ImportFileBuilder::new(['category' => ''])->forget(['quantity', 'name']);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
@ -212,13 +213,13 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
#[Test] #[Test]
public function updateConsumableFromImport(): void public function updateConsumableFromImport(): void
{ {
$consumable = ConsumableFactory::new()->create(['name' => Str::random()]); $consumable = Consumable::factory()->create(['name' => Str::random()]);
$importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]); $importFileBuilder = ImportFileBuilder::new(['itemName' => $consumable->name]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedConsumable = Consumable::query() $updatedConsumable = Consumable::query()
@ -262,9 +263,9 @@ class ImportConsumablesTest extends ImportDataTestCase implements TestsPermissio
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->consumable()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,

View file

@ -3,6 +3,7 @@
namespace Tests\Feature\Importing\Api; namespace Tests\Feature\Importing\Api;
use App\Models\Actionlog as ActivityLog; use App\Models\Actionlog as ActivityLog;
use App\Models\Import;
use App\Models\License; use App\Models\License;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -40,9 +41,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
#[Test] #[Test]
public function userWithImportAssetsPermissionCanImportLicenses(): void public function userWithImportAssetsPermissionCanImportLicenses(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->license()->create(); $import = Import::factory()->license()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -52,9 +53,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
{ {
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -106,9 +107,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -116,16 +117,16 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
#[Test] #[Test]
public function willNotCreateNewLicenseWhenNameAndSerialNumberAlreadyExist(): void public function willNotCreateNewLicenseWhenNameAndSerialNumberAlreadyExist(): void
{ {
$license = LicenseFactory::new()->create(); $license = License::factory()->create();
$importFileBuilder = ImportFileBuilder::times(4)->replace([ $importFileBuilder = ImportFileBuilder::times(4)->replace([
'itemName' => $license->name, 'itemName' => $license->name,
'serialNumber' => $license->serial 'serialNumber' => $license->serial
]); ]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewLicenses = License::query() $probablyNewLicenses = License::query()
@ -143,9 +144,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
'expirationDate' => '2022/10/10' 'expirationDate' => '2022/10/10'
]); ]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newLicense = License::query() $newLicense = License::query()
@ -159,9 +160,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
public function willNotCreateNewCompanyWhenCompanyExists(): void public function willNotCreateNewCompanyWhenCompanyExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['companyName' => Str::random()]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query() $newLicenses = License::query()
@ -175,9 +176,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
public function willNotCreateNewManufacturerWhenManufacturerExists(): void public function willNotCreateNewManufacturerWhenManufacturerExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => Str::random()]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['manufacturerName' => Str::random()]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query() $newLicenses = License::query()
@ -191,9 +192,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
public function willNotCreateNewCategoryWhenCategoryExists(): void public function willNotCreateNewCategoryWhenCategoryExists(): void
{ {
$importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['category' => $this->faker->company]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newLicenses = License::query() $newLicenses = License::query()
@ -211,9 +212,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
->forget(['seats']); ->forget(['seats']);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
@ -239,16 +240,16 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
#[Test] #[Test]
public function updateLicenseFromImport(): void public function updateLicenseFromImport(): void
{ {
$license = LicenseFactory::new()->create(); $license = License::factory()->create();
$importFileBuilder = ImportFileBuilder::new([ $importFileBuilder = ImportFileBuilder::new([
'licenseName' => $license->name, 'licenseName' => $license->name,
'serialNumber' => $license->serial 'serialNumber' => $license->serial
]); ]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedLicense = License::query() $updatedLicense = License::query()
@ -302,9 +303,9 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
]; ];
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->license()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,

View file

@ -2,6 +2,8 @@
namespace Tests\Feature\Importing\Api; namespace Tests\Feature\Importing\Api;
use App\Models\Asset;
use App\Models\Import;
use App\Models\Location; use App\Models\Location;
use App\Models\User; use App\Models\User;
use Database\Factories\AssetFactory; use Database\Factories\AssetFactory;
@ -42,9 +44,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
#[Test] #[Test]
public function userWithImportAssetsPermissionCanImportUsers(): void public function userWithImportAssetsPermissionCanImportUsers(): void
{ {
$this->actingAsForApi(UserFactory::new()->canImport()->create()); $this->actingAsForApi(User::factory()->canImport()->create());
$import = ImportFactory::new()->users()->create(); $import = Import::factory()->users()->create();
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -56,9 +58,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
$importFileBuilder = ImportFileBuilder::new(); $importFileBuilder = ImportFileBuilder::new();
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'send-welcome' => 1]) $this->importFileResponse(['import' => $import->id, 'send-welcome' => 1])
->assertOk() ->assertOk()
->assertExactJson([ ->assertExactJson([
@ -125,9 +127,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
} }
@ -135,11 +137,11 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
#[Test] #[Test]
public function willNotCreateNewUserWhenUserWithUserNameAlreadyExist(): void public function willNotCreateNewUserWhenUserWithUserNameAlreadyExist(): void
{ {
$user = UserFactory::new()->create(['username' => Str::random()]); $user = User::factory()->create(['username' => Str::random()]);
$importFileBuilder = ImportFileBuilder::times(4)->replace(['username' => $user->username]); $importFileBuilder = ImportFileBuilder::times(4)->replace(['username' => $user->username]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$probablyNewUsers = User::query() $probablyNewUsers = User::query()
@ -154,9 +156,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
{ {
$importFileBuilder = ImportFileBuilder::new()->forget('username'); $importFileBuilder = ImportFileBuilder::new()->forget('username');
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id])->assertOk(); $this->importFileResponse(['import' => $import->id])->assertOk();
$newUser = User::query() $newUser = User::query()
@ -171,12 +173,12 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
#[Test] #[Test]
public function willUpdateLocationOfAllAssetsAssignedToUser(): void public function willUpdateLocationOfAllAssetsAssignedToUser(): void
{ {
$user = UserFactory::new()->create(['username' => Str::random()]); $user = User::factory()->create(['username' => Str::random()]);
$assetsAssignedToUser = AssetFactory::new()->create(['assigned_to' => $user->id, 'assigned_type' => User::class]); $assetsAssignedToUser = Asset::factory()->create(['assigned_to' => $user->id, 'assigned_type' => User::class]);
$importFileBuilder = ImportFileBuilder::new(['username' => $user->username]); $importFileBuilder = ImportFileBuilder::new(['username' => $user->username]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$userLocation = Location::query()->where('name', $importFileBuilder->firstRow()['location'])->sole(['id']); $userLocation = Location::query()->where('name', $importFileBuilder->firstRow()['location'])->sole(['id']);
@ -191,9 +193,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
public function whenRequiredColumnsAreMissingInImportFile(): void public function whenRequiredColumnsAreMissingInImportFile(): void
{ {
$importFileBuilder = ImportFileBuilder::new(['firstName' => ''])->forget(['username']); $importFileBuilder = ImportFileBuilder::new(['firstName' => ''])->forget(['username']);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id]) $this->importFileResponse(['import' => $import->id])
->assertInternalServerError() ->assertInternalServerError()
@ -219,13 +221,13 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
#[Test] #[Test]
public function updateUserFromImport(): void public function updateUserFromImport(): void
{ {
$user = UserFactory::new()->create(['username' => Str::random()])->refresh(); $user = User::factory()->create(['username' => Str::random()])->refresh();
$importFileBuilder = ImportFileBuilder::new(['username' => $user->username]); $importFileBuilder = ImportFileBuilder::new(['username' => $user->username]);
$row = $importFileBuilder->firstRow(); $row = $importFileBuilder->firstRow();
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk();
$updatedUser = User::query()->with(['company', 'location'])->find($user->id); $updatedUser = User::query()->with(['company', 'location'])->find($user->id);
@ -267,9 +269,9 @@ class ImportUsersTest extends ImportDataTestCase implements TestsPermissionsRequ
]; ];
$importFileBuilder = new ImportFileBuilder([$row]); $importFileBuilder = new ImportFileBuilder([$row]);
$import = ImportFactory::new()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);
$this->actingAsForApi(UserFactory::new()->superuser()->create()); $this->actingAsForApi(User::factory()->superuser()->create());
$this->importFileResponse([ $this->importFileResponse([
'import' => $import->id, 'import' => $import->id,