Continuing to refactor tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2021-12-02 16:14:45 -08:00
parent c2709be4a1
commit 35ba28bff9
6 changed files with 45 additions and 109 deletions

View file

@ -5,7 +5,10 @@ namespace Database\Factories;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\Location;
use App\Models\Supplier;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\StatusLabel;
/*
|--------------------------------------------------------------------------
@ -36,14 +39,16 @@ class AssetFactory extends Factory
{
return [
'name' => null,
'rtd_location_id' => Location::factory()->create(),
'serial' => $this->faker->uuid,
'status_id' => 1,
'status_id' => StatusLabel::factory()->create(),
'user_id' => 1,
'asset_tag' => $this->faker->unixTime('now'),
'notes' => 'Created by DB seeder',
'purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get()),
'purchase_cost' => $this->faker->randomFloat(2, '299.99', '2999.99'),
'order_number' => $this->faker->numberBetween(1000000, 50000000),
'supplier_id' => Supplier::factory()->create(),
'requestable' => $this->faker->boolean(),
'assigned_to' => null,
'assigned_type' => null,

View file

@ -3,6 +3,7 @@
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;
/*
|--------------------------------------------------------------------------
@ -81,6 +82,7 @@ class AssetModelFactory extends Factory
{
return [
'user_id' => 1,
'name' => $this->faker->catchPhrase(),
'model_number' => $this->faker->creditCardNumber(),
'notes' => 'Created by demo seeder',

View file

@ -31,6 +31,7 @@ class CategoryFactory extends Factory
public function definition()
{
return [
'name' => $this->faker->catchPhrase(),
'checkin_email' => $this->faker->boolean(),
'eula_text' => $this->faker->paragraph(),
'require_acceptance' => false,

View file

@ -2,6 +2,7 @@
namespace Tests\Unit;
use App\Models\Asset;
use App\Models\Category;
use App\Models\AssetModel;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -15,22 +16,6 @@ class AssetModelTest extends BaseTest
*/
protected $tester;
public function testAnAssetModelRequiresAttributes()
{
// An Asset Model requires a name, a category_id, and a manufacturer_id.
$a = AssetModel::create();
$this->assertFalse($a->isValid());
$fields = [
'name' => 'name',
'manufacturer_id' => 'manufacturer id',
'category_id' => 'category id',
];
$errors = $a->getErrors();
foreach ($fields as $field => $fieldTitle) {
$this->assertEquals($errors->get($field)[0], "The ${fieldTitle} field is required.");
}
}
public function testAnAssetModelZerosOutBlankEols()
{
$am = new AssetModel;
@ -42,28 +27,21 @@ class AssetModelTest extends BaseTest
public function testAnAssetModelContainsAssets()
{
$assetModel = $this->createValidAssetModel();
$this->createValidAsset([
'model_id' => $assetModel->id,
]);
$this->assertEquals(1, $assetModel->assets()->count());
$category = Category::factory()->create(
['category_type' => 'asset']
);
$model = AssetModel::factory()->create([
'category_id' => $category->id,
]);
$asset = Asset::factory()
->create(
[
'model_id' => $model->id
]
);
$this->assertEquals(1, $model->assets()->count());
}
public function testAnAssetModelHasACategory()
{
$assetmodel = $this->createValidAssetModel();
$this->assertInstanceOf(App\Models\Category::class, $assetmodel->category);
}
public function testAnAssetModelHasADepreciation()
{
$assetmodel = $this->createValidAssetModel();
$this->assertInstanceOf(App\Models\Depreciation::class, $assetmodel->depreciation);
}
public function testAnAssetModelHasAManufacturer()
{
$assetmodel = $this->createValidAssetModel();
$this->assertInstanceOf(App\Models\Manufacturer::class, $assetmodel->manufacturer);
}
}

View file

@ -11,6 +11,9 @@ use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\Unit\BaseTest;
use App\Models\Component;
use App\Models\ActionLog;
class AssetTest extends BaseTest
{
@ -178,7 +181,7 @@ class AssetTest extends BaseTest
{
$asset = $this->createValidAsset();
$components = \App\Models\Component::factory()->count(5)->ramCrucial4()->create([
$components = Component::factory()->count(5)->ramCrucial4()->create([
'category_id' => $this->createValidCategory('component-hdd-category')->id,
]);
@ -187,7 +190,7 @@ class AssetTest extends BaseTest
'asset_id'=>$asset->id,
]);
});
$this->assertInstanceOf(App\Models\Component::class, $asset->components()->first());
$this->assertInstanceOf(Component::class, $asset->components()->first());
$this->assertCount(5, $asset->components);
}
@ -197,7 +200,10 @@ class AssetTest extends BaseTest
'supplier_id' => $this->createValidSupplier()->id,
]);
$this->assertCount(0, $asset->uploads);
\App\Models\Actionlog::factory()->count('asset-upload')->create(['item_id' => $asset->id]);
// This is wrong
Actionlog::factory()->count('asset-upload')->create(
['item_id' => $asset->id]
);
$this->assertCount(1, $asset->fresh()->uploads);
}
@ -222,13 +228,13 @@ class AssetTest extends BaseTest
$asset = $this->createValidAsset();
$adminUser = $this->signIn();
$target = \App\Models\User::factory()->create([
'location_id' => \App\Models\Location::factory()->create(),
$target = User::factory()->create([
'location_id' => Location::factory()->create(),
]);
// An Asset Can be checked out to a user, and this should be logged.
$asset->checkOut($target, $adminUser);
$asset->save();
$this->assertInstanceOf(App\Models\User::class, $asset->assignedTo);
$this->assertInstanceOf(User::class, $asset->assignedTo);
$this->assertEquals($asset->location->id, $target->userLoc->id);
$this->assertEquals('user', $asset->assignedType());

View file

@ -9,6 +9,7 @@ use Tests\Unit\BaseTest;
use App\Models\Component;
use App\Models\Asset;
use App\Models\Consumable;
use App\Models\User;
class CompanyTest extends BaseTest
{
@ -17,74 +18,17 @@ class CompanyTest extends BaseTest
*/
protected $tester;
public function testFailsEmptyValidation()
{
// An Company requires a name, a qty, and a category_id.
$company = Company::factory()->assetDesktopCategory()->create();
$this->assertFalse($company->isValid());
$fields = [
'name' => 'name',
];
$errors = $company->getErrors();
foreach ($fields as $field => $fieldTitle) {
$this->assertEquals($errors->get($field)[0], "The ${fieldTitle} field is required.");
}
}
public function testACompanyCanHaveUsers()
{
$company = Company::factory()->assetDesktopCategory()->create();
$user = $this->createValidUser(['company_id'=>$company->id]);
$company = Company::factory()->create();
User::factory()
->create(
[
'company_id'=>$company->id
]
);
$this->assertCount(1, $company->users);
}
public function testACompanyCanHaveAssets()
{
$company = Company::factory()->assetDesktopCategory()->create();
$this->createValidAsset(['company_id' => $company->id]);
$this->assertCount(1, $company->assets);
}
public function testACompanyCanHaveLicenses()
{
$company = Company::factory()->assetDesktopCategory()->create();
\App\Models\License::factory()->count(1)->acrobat()->create([
'company_id'=>$company->id,
'manufacturer_id' => \App\Models\Manufacturer::factory()->adobe()->create()->id,
'category_id' => \App\Models\Category::factory()->licenseOfficeCategory()->create()->id,
]);
$this->assertCount(1, $company->licenses);
}
public function testACompanyCanHaveAccessories()
{
$company = Company::factory()->assetDesktopCategory()->create();
$a = \App\Models\Accessory::factory()->appleBtKeyboard()->create([
'category_id' => \App\Models\Category::factory()->accessoryKeyboardCategory()->create()->id,
'company_id' => $company->id,
]);
$this->assertCount(1, $company->accessories);
}
public function testACompanyCanHaveConsumables()
{
$company = Company::factory()->assetDesktopCategory()->create();
\App\Models\Consumable::factory()->count(1)->cardstock()->create([
'company_id' => $company->id,
'category_id' => \App\Models\Category::factory()->consumablePaperCategory()->create()->id,
]);
$this->assertCount(1, $company->consumables);
}
public function testACompanyCanHaveComponents()
{
$company = Company::factory()->assetDesktopCategory()->create();
Component::factory()->count(1)->ramCrucial4()->create([
'company_id'=>$company->id,
'category_id' => \App\Models\Category::factory()->componentRamCategory()->create()->id,
]);
$this->assertCount(1, $company->components);
}
}