Fixed category factory methods

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2021-12-02 13:40:16 -08:00
parent 44b6907919
commit 00b63fe7c7
5 changed files with 185 additions and 170 deletions

View file

@ -114,6 +114,7 @@ class Asset extends Depreciable
'purchase_cost' => 'numeric|nullable', 'purchase_cost' => 'numeric|nullable',
'next_audit_date' => 'date|nullable', 'next_audit_date' => 'date|nullable',
'last_audit_date' => 'date|nullable', 'last_audit_date' => 'date|nullable',
'supplier_id' => 'exists:suppliers,id|nullable',
]; ];
/** /**

View file

@ -30,7 +30,7 @@ class AssetModel extends SnipeModel
'name' => 'required|min:1|max:255', 'name' => 'required|min:1|max:255',
'model_number' => 'max:255|nullable', 'model_number' => 'max:255|nullable',
'category_id' => 'required|integer|exists:categories,id', 'category_id' => 'required|integer|exists:categories,id',
'manufacturer_id' => 'required|integer|exists:manufacturers,id', 'manufacturer_id' => 'integer|exists:manufacturers,id|nullable',
'eol' => 'integer:min:0|max:240|nullable', 'eol' => 'integer:min:0|max:240|nullable',
]; ];

View file

@ -3,6 +3,7 @@
namespace Database\Factories; namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -38,154 +39,143 @@ class CategoryFactory extends Factory
]; ];
} }
// usage: Category::factory()->assetLaptopCategory();
public function assetLaptopCategory() public function assetLaptopCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Laptops', 'name' => 'Laptops',
'category_type' => 'asset', 'category_type' => 'asset',
'require_acceptance' => true, 'require_acceptance' => true,
]; ]);
});
} }
// usage: Category::factory()->assetDesktopCategory();
public function assetDesktopCategory() public function assetDesktopCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Desktops', 'name' => 'Desktops',
'category_type' => 'asset', 'category_type' => 'asset',
]; 'require_acceptance' => true,
}); ]);
} }
// usage: Category::factory()->assetDisplayCategory();
public function assetDisplayCategory() public function assetDisplayCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Displays', 'name' => 'Displays',
'category_type' => 'asset', 'category_type' => 'asset',
]; ]);
});
} }
// usage: Category::factory()->assetTabletCategory();
public function assetTabletCategory() public function assetTabletCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Tablets', 'name' => 'Tablets',
'category_type' => 'asset', 'category_type' => 'asset',
]; ]);
});
} }
// usage: Category::factory()->assetMobileCategory();
public function assetMobileCategory() public function assetMobileCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Mobile Phones', 'name' => 'Mobile Phones',
'category_type' => 'asset', 'category_type' => 'asset',
]; ]);
});
} }
// usage: Category::factory()->assetConferenceCategory();
public function assetConferenceCategory() public function assetConferenceCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Conference Phones', 'name' => 'Conference Phones',
'category_type' => 'asset', 'category_type' => 'asset',
]; ]);
});
} }
// usage: Category::factory()->assetVoipCategory();
public function assetVoipCategory() public function assetVoipCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'VOIP Phones', 'name' => 'VOIP Phones',
'category_type' => 'asset', 'category_type' => 'asset',
]; ]);
});
} }
// usage: Category::factory()->accessoryKeyboardCategory();
public function accessoryKeyboardCategory() public function accessoryKeyboardCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [ 'name' => 'Keyboardss',
'name' => 'Keyboards',
'category_type' => 'accessory', 'category_type' => 'accessory',
]; ]);
});
} }
// usage: Category::factory()->accessoryMouseCategory();
public function accessoryMouseCategory() public function accessoryMouseCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Mouse', 'name' => 'Mouse',
'category_type' => 'accessory', 'category_type' => 'accessory',
]; ]);
});
} }
// usage: Category::factory()->componentHddCategory();
public function componentHddCategory() public function componentHddCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'HDD/SSD', 'name' => 'HDD/SSD',
'category_type' => 'component', 'category_type' => 'component',
]; ]);
});
} }
// usage: Category::factory()->componentRamCategory();
public function componentRamCategory() public function componentRamCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'RAM', 'name' => 'RAM',
'category_type' => 'component', 'category_type' => 'component',
]; ]);
});
} }
// usage: Category::factory()->consumablePaperCategory();
public function consumablePaperCategory() public function consumablePaperCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Printer Paper', 'name' => 'Printer Paper',
'category_type' => 'consumable', 'category_type' => 'consumable',
]; ]);
});
} }
// usage: Category::factory()->consumableInkCategory();
public function consumableInkCategory() public function consumableInkCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Printer Ink', 'name' => 'Printer Ink',
'category_type' => 'consumable', 'category_type' => 'consumable',
]; ]);
});
} }
// usage: Category::factory()->licenseGraphicsCategory();
public function licenseGraphicsCategory() public function licenseGraphicsCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Graphics Software', 'name' => 'Graphics Software',
'category_type' => 'license', 'category_type' => 'license',
]; ]);
});
} }
// usage: Category::factory()->licenseGraphicsCategory();
public function licenseOfficeCategory() public function licenseOfficeCategory()
{ {
return $this->state(function () { return Category::factory()->create([
return [
'name' => 'Office Software', 'name' => 'Office Software',
'category_type' => 'license', 'category_type' => 'license',
]; ]);
});
} }
} }

View file

@ -29,35 +29,35 @@ class BaseTest extends TestCase
return $user; return $user;
} }
protected function createValidAssetModel($state = 'mbp-13-model', $overrides = []) protected function createValidAssetModel()
{ {
return \App\Models\AssetModel::factory()->state($state)->create(array_merge([ return \App\Models\AssetModel::factory()->create([
'category_id' => $this->createValidCategory(), 'category_id' => $this->createValidCategory(),
'manufacturer_id' => $this->createValidManufacturer(), 'manufacturer_id' => $this->createValidManufacturer(),
'depreciation_id' => $this->createValidDepreciation(), 'depreciation_id' => $this->createValidDepreciation(),
], $overrides)); ]);
} }
protected function createValidCategory($state = 'asset-laptop-category', $overrides = []) protected function createValidCategory()
{ {
return \App\Models\Category::factory()->state()->create($overrides); return \App\Models\Category::factory()->make();
} }
protected function createValidCompany($overrides = []) protected function createValidCompany()
{ {
return \App\Models\Company::factory()->create($overrides); return \App\Models\Company::factory()->create();
} }
protected function createValidDepartment($state = 'engineering', $overrides = []) protected function createValidDepartment($state = 'engineering', $overrides = [])
{ {
return \App\Models\Department::factory()->state()->create(array_merge([ return \App\Models\Department::factory()->create(array_merge([
'location_id' => $this->createValidLocation()->id, 'location_id' => $this->createValidLocation()->id,
], $overrides)); ], $overrides));
} }
protected function createValidDepreciation($state = 'computer', $overrides = []) protected function createValidDepreciation()
{ {
return \App\Models\Depreciation::factory()->state()->create($overrides); return \App\Models\Depreciation::factory()->create();
} }
protected function createValidLocation($overrides = []) protected function createValidLocation($overrides = [])
@ -65,9 +65,9 @@ class BaseTest extends TestCase
return \App\Models\Location::factory()->create($overrides); return \App\Models\Location::factory()->create($overrides);
} }
protected function createValidManufacturer($state = 'apple', $overrides = []) protected function createValidManufacturer()
{ {
return \App\Models\Manufacturer::factory()->state()->create($overrides); return \App\Models\Manufacturer::factory()->create();
} }
protected function createValidSupplier($overrides = []) protected function createValidSupplier($overrides = [])

View file

@ -6,6 +6,9 @@ use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\Unit\BaseTest; use Tests\Unit\BaseTest;
use App\Models\AssetModel;
use App\Models\Asset;
use App\Models\Accessory;
class CategoryTest extends BaseTest class CategoryTest extends BaseTest
{ {
@ -32,43 +35,64 @@ class CategoryTest extends BaseTest
public function testACategoryCanHaveAssets() public function testACategoryCanHaveAssets()
{ {
$this->createValidAssetModel(); //This will seed various things to make the following work better. $category = Category::factory()->assetDesktopCategory()->create();
$category = $this->createValidCategory('asset-desktop-category');
$models = \App\Models\AssetModel::factory()->count(5)->mbp13Model()->create(['category_id' => $category->id]);
$this->assertEquals(5, $category->models->count()); // Generate 5 models via factory
$this->assertCount(5, $category->models); $models = AssetModel::factory()
->mbp13Model()
->count(5)
->create(
[
'category_id' => $category->id
]
);
// Loop through the models and create 2 assets in each model
$models->each(function ($model) { $models->each(function ($model) {
// This is intentionally run twice to generate the ten imagined assets, done this way to keep it in sync with createValidAsset rather than using the factory directly. $asset = Asset::factory()
$this->createValidAsset(['model_id' => $model->id]); ->count(2)
$this->createValidAsset(['model_id' => $model->id]); ->create(
[
'model_id' => $model->id,
]
);
dd($asset);
}); });
$this->assertCount(5, $category->models);
$this->assertCount(5, $category->models);
$this->assertEquals(10, $category->itemCount()); $this->assertEquals(10, $category->itemCount());
} }
public function testACategoryCanHaveAccessories()
{
$category = $this->createValidCategory('accessory-keyboard-category');
\App\Models\Accessory::factory()->count(5)->appleBtKeyboard()->create(['category_id' => $category->id]);
$this->assertCount(5, $category->accessories); // public function testACategoryCanHaveAccessories()
$this->assertEquals(5, $category->itemCount()); // {
} // $category = Category::factory()->assetDesktopCategory()->create();
// Accessory::factory()->count(5)->appleBtKeyboard()->create(
// [
// 'category_id' => $category->id
// ]
// );
public function testACategoryCanHaveConsumables() // $this->assertCount(5, $category->accessories);
{ // $this->assertEquals(5, $category->itemCount());
$category = $this->createValidCategory('consumable-paper-category'); // }
\App\Models\Consumable::factory()->count(5)->cardstock()->create(['category_id' => $category->id]);
$this->assertCount(5, $category->consumables);
$this->assertEquals(5, $category->itemCount());
}
public function testACategoryCanHaveComponents() // public function testACategoryCanHaveConsumables()
{ // {
$category = $this->createValidCategory('component-ram-category'); // $category = $this->createValidCategory('consumable-paper-category');
\App\Models\Component::factory()->count(5)->ramCrucial4()->create(['category_id' => $category->id]); // \App\Models\Consumable::factory()->count(5)->cardstock()->create(['category_id' => $category->id]);
$this->assertCount(5, $category->components); // $this->assertCount(5, $category->consumables);
$this->assertEquals(5, $category->itemCount()); // $this->assertEquals(5, $category->itemCount());
} // }
// public function testACategoryCanHaveComponents()
// {
// $category = $this->createValidCategory('component-ram-category');
// \App\Models\Component::factory()->count(5)->ramCrucial4()->create(['category_id' => $category->id]);
// $this->assertCount(5, $category->components);
// $this->assertEquals(5, $category->itemCount());
// }
} }