Improve model factories

This commit is contained in:
Marcus Moore 2024-04-16 16:58:28 -07:00
parent ad99aa460b
commit e16c04250e
No known key found for this signature in database
5 changed files with 19 additions and 18 deletions

View file

@ -4,6 +4,7 @@ namespace Database\Factories;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel; use App\Models\AssetModel;
use App\Models\CustomField;
use App\Models\Location; use App\Models\Location;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use App\Models\Supplier; use App\Models\Supplier;
@ -353,10 +354,14 @@ class AssetFactory extends Factory
return $this->state(['requestable' => false]); return $this->state(['requestable' => false]);
} }
public function hasEncryptedCustomField() public function hasEncryptedCustomField(CustomField $field = null)
{ {
return $this->afterMaking(function (Asset $asset) { // @todo: update this so existing asset model is used if present on the asset
$asset->model_id = AssetModel::factory()->withEncryptedField()->create()->id; // (may have been created in a test case)
return $this->state(function () use ($field) {
return [
'model_id' => AssetModel::factory()->hasEncryptedCustomField($field),
];
}); });
} }

View file

@ -431,16 +431,12 @@ class AssetModelFactory extends Factory
}); });
} }
public function withEncryptedField() public function hasEncryptedCustomField(CustomField $field = null)
{ {
return $this->state(function () { return $this->state(function () use ($field) {
$field = CustomField::factory()->testEncrypted()->create(); // TODO - having to create and then 'find' the thing you just created is WEIRD
return [ return [
'fieldset_id' => function () use ($field) { 'fieldset_id' => CustomFieldset::factory()->hasEncryptedCustomField($field),
return CustomFieldset::where('name', 'Has Encrypted Custom Field')->first() ?? CustomFieldset::factory()->has_encrypted_custom_field()->hasAttached(CustomField::where('name', 'Test Encrypted')->first(), ['order' => 1, 'required' => 0], 'fields');
},
]; ];
}); });
} }
} }

View file

@ -45,12 +45,12 @@ class CustomFieldsetFactory extends Factory
}); });
} }
public function has_encrypted_custom_field() public function hasEncryptedCustomField(CustomField $field = null)
{ {
return $this->state(function () { return $this->afterCreating(function (CustomFieldset $fieldset) use ($field) {
return [ $field = $field ?? CustomField::factory()->testEncrypted()->create();
'name' => 'Has Encrypted Custom Field',
]; $fieldset->fields()->attach($field, ['order' => '1', 'required' => false]);
}); });
} }
} }

View file

@ -485,7 +485,7 @@ class AssetStoreTest extends TestCase
public function testEncryptedCustomFieldCanBeStored() public function testEncryptedCustomFieldCanBeStored()
{ {
$field = CustomField::factory()->testEncrypted()->create(); $field = CustomField::factory()->testEncrypted()->create();
$asset = Asset::factory()->hasEncryptedCustomField()->create(); $asset = Asset::factory()->hasEncryptedCustomField($field)->create();
$superuser = User::factory()->superuser()->create(); $superuser = User::factory()->superuser()->create();
//first, test that an Admin user can save the encrypted custom field //first, test that an Admin user can save the encrypted custom field

View file

@ -12,7 +12,7 @@ class AssetUpdateTest extends TestCase
public function testEncryptedCustomFieldCanBeUpdated() public function testEncryptedCustomFieldCanBeUpdated()
{ {
$field = CustomField::factory()->testEncrypted()->create(); $field = CustomField::factory()->testEncrypted()->create();
$asset = Asset::factory()->hasEncryptedCustomField()->create(); $asset = Asset::factory()->hasEncryptedCustomField($field)->create();
$superuser = User::factory()->superuser()->create(); $superuser = User::factory()->superuser()->create();
//first, test that an Admin user can save the encrypted custom field //first, test that an Admin user can save the encrypted custom field
@ -30,7 +30,7 @@ class AssetUpdateTest extends TestCase
public function testPermissionNeededToUpdateEncryptedField() public function testPermissionNeededToUpdateEncryptedField()
{ {
$field = CustomField::factory()->testEncrypted()->create(); $field = CustomField::factory()->testEncrypted()->create();
$asset = Asset::factory()->hasEncryptedCustomField()->create(); $asset = Asset::factory()->hasEncryptedCustomField($field)->create();
$normal_user = User::factory()->editAssets()->create(); $normal_user = User::factory()->editAssets()->create();
$asset->{$field->db_column_name()} = \Crypt::encrypt("encrypted value should not change"); $asset->{$field->db_column_name()} = \Crypt::encrypt("encrypted value should not change");