Skip to content

Commit

Permalink
Merge pull request #15616 from marcusmoore/test/importer-test-updates
Browse files Browse the repository at this point in the history
Improve importer tests
  • Loading branch information
snipe authored Oct 7, 2024
2 parents 32551d5 + 8035326 commit 56e7ea6
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 583 deletions.
3 changes: 3 additions & 0 deletions app/Models/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Import extends Model
{
use HasFactory;

protected $casts = [
'header_row' => 'array',
'first_row' => 'array',
Expand Down
4 changes: 2 additions & 2 deletions database/factories/ImportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function accessory()
$fileBuilder = Importing\AccessoriesImportFileBuilder::new();

$attributes['name'] = "{$attributes['name']} Accessories";
$attributes['import_type'] = 'Accessories';
$attributes['import_type'] = 'accessory';
$attributes['header_row'] = $fileBuilder->toCsv()[0];
$attributes['first_row'] = $fileBuilder->firstRow();

Expand Down Expand Up @@ -76,7 +76,7 @@ public function asset()
public function component()
{
return $this->state(function (array $attributes) {
$fileBuilder = Importing\ComponentsImportFileBuilder::times();
$fileBuilder = Importing\ComponentsImportFileBuilder::new();

$attributes['name'] = "{$attributes['name']} Components";
$attributes['import_type'] = 'component';
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/Importing/Api/GeneralImportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Feature\Importing\Api;

use App\Models\User;

class GeneralImportTest extends ImportDataTestCase
{
public function testRequiresExistingImport()
{
$this->actingAsForApi(User::factory()->canImport()->create());

$this->importFileResponse(['import' => 9999, 'import-type' => 'accessory'])
->assertStatusMessageIs('import-errors');
}
}
176 changes: 85 additions & 91 deletions tests/Feature/Importing/Api/ImportAccessoriesTest.php

Large diffs are not rendered by default.

294 changes: 144 additions & 150 deletions tests/Feature/Importing/Api/ImportAssetsTest.php

Large diffs are not rendered by default.

130 changes: 63 additions & 67 deletions tests/Feature/Importing/Api/ImportComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

use App\Models\Actionlog as ActionLog;
use App\Models\Component;
use Database\Factories\ComponentFactory;
use Illuminate\Support\Str;
use Database\Factories\UserFactory;
use Database\Factories\ImportFactory;
use PHPUnit\Framework\Attributes\Test;
use App\Models\Import;
use App\Models\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\Support\Importing\CleansUpImportFiles;
use Tests\Support\Importing\ComponentsImportFileBuilder as ImportFileBuilder;

class ImportComponentsTest extends ImportDataTestCase
class ImportComponentsTest extends ImportDataTestCase implements TestsPermissionsRequirement
{
use CleansUpImportFiles;
use WithFaker;

protected function importFileResponse(array $parameters = []): TestResponse
Expand All @@ -29,24 +30,19 @@ protected function importFileResponse(array $parameters = []): TestResponse
}

#[Test]
#[DataProvider('permissionsTestData')]
public function onlyUserWithPermissionCanImportComponents(array|string $permissions): void
public function testRequiresPermission()
{
$permissions = collect((array) $permissions)
->map(fn (string $permission) => [$permission => '1'])
->toJson();

$this->actingAsForApi(UserFactory::new()->create(['permissions' => $permissions]));
$this->actingAsForApi(User::factory()->create());

$this->importFileResponse(['import' => 44])->assertForbidden();
}

#[Test]
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();
}
Expand All @@ -58,9 +54,9 @@ public function importComponents(): void

$importFileBuilder = ImportFileBuilder::new();
$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])
->assertOk()
->assertExactJson([
Expand All @@ -79,21 +75,21 @@ public function importComponents(): void
->where('item_id', $newComponent->id)
->sole();

$this->assertEquals($activityLog->action_type, 'create');
$this->assertEquals($activityLog->action_source, 'importer');
$this->assertEquals($activityLog->company_id, $newComponent->company->id);
$this->assertEquals('create', $activityLog->action_type);
$this->assertEquals('importer', $activityLog->action_source);
$this->assertEquals($newComponent->company->id, $activityLog->company_id);

$this->assertEquals($newComponent->name, $row['itemName']);
$this->assertEquals($newComponent->company->name, $row['companyName']);
$this->assertEquals($newComponent->category->name, $row['category']);
$this->assertEquals($newComponent->location->name, $row['location']);
$this->assertEquals($row['itemName'], $newComponent->name);
$this->assertEquals($row['companyName'], $newComponent->company->name);
$this->assertEquals($row['category'], $newComponent->category->name);
$this->assertEquals($row['location'], $newComponent->location->name);
$this->assertNull($newComponent->supplier_id);
$this->assertEquals($newComponent->qty, $row['quantity']);
$this->assertEquals($newComponent->order_number, $row['orderNumber']);
$this->assertEquals($newComponent->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($newComponent->purchase_cost, $row['purchaseCost']);
$this->assertEquals($row['quantity'], $newComponent->qty);
$this->assertEquals($row['orderNumber'], $newComponent->order_number);
$this->assertEquals($row['purchaseDate'], $newComponent->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $newComponent->purchase_cost);
$this->assertNull($newComponent->min_amt);
$this->assertEquals($newComponent->serial, $row['serialNumber']);
$this->assertEquals($row['serialNumber'], $newComponent->serial);
$this->assertNull($newComponent->image);
$this->assertNull($newComponent->notes);
}
Expand All @@ -106,26 +102,26 @@ public function willIgnoreUnknownColumnsWhenFileContainsUnknownColumns(): void

$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();
}

#[Test]
public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExists(): void
{
$component = ComponentFactory::new()->create();
$component = Component::factory()->create();

$importFileBuilder = ImportFileBuilder::times(4)->replace([
'itemName' => $component->name,
'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();

$probablyNewComponents = Component::query()
Expand All @@ -141,9 +137,9 @@ public function willNotCreateNewComponentWhenComponentWithNameAndSerialNumberExi
public function willNotCreateNewCompanyWhenCompanyExists(): void
{
$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();

$newComponents = Component::query()
Expand All @@ -157,9 +153,9 @@ public function willNotCreateNewCompanyWhenCompanyExists(): void
public function willNotCreateNewLocationWhenLocationExists(): void
{
$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();

$newComponents = Component::query()
Expand All @@ -173,9 +169,9 @@ public function willNotCreateNewLocationWhenLocationExists(): void
public function willNotCreateNewCategoryWhenCategoryExists(): void
{
$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();

$newComponents = Component::query()
Expand All @@ -193,9 +189,9 @@ public function whenRequiredColumnsAreMissingInImportFile(): void
->forget(['quantity']);

$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])
->assertInternalServerError()
Expand All @@ -222,35 +218,35 @@ public function whenRequiredColumnsAreMissingInImportFile(): void
#[Test]
public function updateComponentFromImport(): void
{
$component = ComponentFactory::new()->create();
$component = Component::factory()->create();
$importFileBuilder = ImportFileBuilder::new([
'itemName' => $component->name,
'serialNumber' => $component->serial
]);

$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();

$updatedComponent = Component::query()
->with(['location', 'category'])
->where('serial', $row['serialNumber'])
->sole();

$this->assertEquals($updatedComponent->name, $row['itemName']);
$this->assertEquals($updatedComponent->category->name, $row['category']);
$this->assertEquals($updatedComponent->location->name, $row['location']);
$this->assertEquals($updatedComponent->supplier_id, $component->supplier_id);
$this->assertEquals($updatedComponent->qty, $row['quantity']);
$this->assertEquals($updatedComponent->order_number, $row['orderNumber']);
$this->assertEquals($updatedComponent->purchase_date->toDateString(), $row['purchaseDate']);
$this->assertEquals($updatedComponent->purchase_cost, $row['purchaseCost']);
$this->assertEquals($updatedComponent->min_amt, $component->min_amt);
$this->assertEquals($updatedComponent->serial, $row['serialNumber']);
$this->assertEquals($updatedComponent->image, $component->image);
$this->assertEquals($updatedComponent->notes, $component->notes);
$this->assertEquals($row['itemName'], $updatedComponent->name);
$this->assertEquals($row['category'], $updatedComponent->category->name);
$this->assertEquals($row['location'], $updatedComponent->location->name);
$this->assertEquals($component->supplier_id, $updatedComponent->supplier_id);
$this->assertEquals($row['quantity'], $updatedComponent->qty);
$this->assertEquals($row['orderNumber'], $updatedComponent->order_number);
$this->assertEquals($row['purchaseDate'], $updatedComponent->purchase_date->toDateString());
$this->assertEquals($row['purchaseCost'], $updatedComponent->purchase_cost);
$this->assertEquals($component->min_amt, $updatedComponent->min_amt);
$this->assertEquals($row['serialNumber'], $updatedComponent->serial);
$this->assertEquals($component->image, $updatedComponent->image);
$this->assertEquals($component->notes, $updatedComponent->notes);
}

#[Test]
Expand All @@ -270,9 +266,9 @@ public function customColumnMapping(): void
];

$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([
'import' => $import->id,
Expand All @@ -294,14 +290,14 @@ public function customColumnMapping(): void
->where('serial', $importFileBuilder->firstRow()['category'])
->sole();

$this->assertEquals($newComponent->name, $row['quantity']);
$this->assertEquals($newComponent->category->name, $row['purchaseCost']);
$this->assertEquals($newComponent->location->name, $row['serialNumber']);
$this->assertEquals($row['quantity'], $newComponent->name);
$this->assertEquals($row['purchaseCost'], $newComponent->category->name);
$this->assertEquals($row['serialNumber'], $newComponent->location->name);
$this->assertNull($newComponent->supplier_id);
$this->assertEquals($newComponent->qty, $row['companyName']);
$this->assertEquals($newComponent->order_number, $row['orderNumber']);
$this->assertEquals($newComponent->purchase_date->toDateString(), $row['itemName']);
$this->assertEquals($newComponent->purchase_cost, $row['location']);
$this->assertEquals($row['companyName'], $newComponent->qty);
$this->assertEquals($row['orderNumber'], $newComponent->order_number);
$this->assertEquals($row['itemName'], $newComponent->purchase_date->toDateString());
$this->assertEquals($row['location'], $newComponent->purchase_cost);
$this->assertNull($newComponent->min_amt);
$this->assertNull($newComponent->image);
$this->assertNull($newComponent->notes);
Expand Down
Loading

0 comments on commit 56e7ea6

Please sign in to comment.