From 3761de2dc6d3b5f013e80290b40dd6bbb2573f47 Mon Sep 17 00:00:00 2001 From: jmarkfen <119049235+jmarkfen@users.noreply.github.com> Date: Fri, 5 Apr 2024 20:35:43 +0800 Subject: [PATCH] use FamilyMember model --- app/Models/Dafac.php | 5 +++ app/Models/FamilyMember.php | 21 ++++++++++ database/factories/DafacFactory.php | 19 ++++++++- database/factories/FamilyMemberFactory.php | 31 ++++++++++++++ .../2024_04_05_031443_create_dafacs_table.php | 1 - ..._05_085134_create_family_members_table.php | 35 ++++++++++++++++ tests/Feature/Models/FamilyMemberTest.php | 41 +++++++++++++++++++ 7 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 app/Models/FamilyMember.php create mode 100644 database/factories/FamilyMemberFactory.php create mode 100644 database/migrations/2024_04_05_085134_create_family_members_table.php create mode 100644 tests/Feature/Models/FamilyMemberTest.php diff --git a/app/Models/Dafac.php b/app/Models/Dafac.php index 7af883b..564c959 100644 --- a/app/Models/Dafac.php +++ b/app/Models/Dafac.php @@ -73,4 +73,9 @@ public function ethnicity_type() { return $this->belongsTo(EthnicityType::class); } + + public function members() + { + return $this->hasMany(FamilyMember::class); + } } diff --git a/app/Models/FamilyMember.php b/app/Models/FamilyMember.php new file mode 100644 index 0000000..8a8d520 --- /dev/null +++ b/app/Models/FamilyMember.php @@ -0,0 +1,21 @@ + Gender::class, + ]; + + public function dafac() + { + return $this->belongsTo(Dafac::class); + } +} diff --git a/database/factories/DafacFactory.php b/database/factories/DafacFactory.php index b3bcfcf..6a97bd9 100644 --- a/database/factories/DafacFactory.php +++ b/database/factories/DafacFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Enums\Gender; +use App\Models\Dafac; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -40,7 +41,6 @@ public function definition(): array 'is_4ps_beneficiary' => fake()->boolean(), 'is_ip' => true, 'ethnicity_type_id' => EthnicityTypeFactory::new(), - 'members' => fake()->word(), 'older_persons_count' => fake()->numberBetween(1, 9), 'pregnant_and_lactating_mothers_count' => fake()->numberBetween(1, 9), 'pwd_and_with_medical_conditions_count' => fake()->numberBetween(1, 9), @@ -48,4 +48,21 @@ public function definition(): array 'housing_condition' => fake()->word(), ]; } + + /** + * Configure the model factory. + */ + public function configure(): static + { + return $this->afterCreating(function (Dafac $dafac) { + $dafac->refresh(); + if ($dafac->members->count() === 0) { + FamilyMemberFactory::new() + ->count(fake()->numberBetween(1, 5)) + ->for($dafac) + ->create(); + } + $dafac->refresh(); + }); + } } diff --git a/database/factories/FamilyMemberFactory.php b/database/factories/FamilyMemberFactory.php new file mode 100644 index 0000000..e72ab9f --- /dev/null +++ b/database/factories/FamilyMemberFactory.php @@ -0,0 +1,31 @@ + + */ +class FamilyMemberFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'dafac_id' => DafacFactory::new(), + 'name' => fake()->name(), + 'relation_to_head' => fake()->word(), + 'age' => fake()->numberBetween(0, 100), + 'sex' => fake()->randomElement(Gender::cases()), + 'educational_attainment' => fake()->word(), + 'occupational_skills' => fake()->word(), + 'remarks' => fake()->word(), + ]; + } +} diff --git a/database/migrations/2024_04_05_031443_create_dafacs_table.php b/database/migrations/2024_04_05_031443_create_dafacs_table.php index 1e2dcc7..6412ff6 100644 --- a/database/migrations/2024_04_05_031443_create_dafacs_table.php +++ b/database/migrations/2024_04_05_031443_create_dafacs_table.php @@ -28,7 +28,6 @@ public function up(): void $table->string('permanent_address'); $table->string('is_4ps_beneficiary'); $table->string('is_ip'); - $table->string('members'); $table->string('older_persons_count'); $table->string('pregnant_and_lactating_mothers_count'); $table->string('pwd_and_with_medical_conditions_count'); diff --git a/database/migrations/2024_04_05_085134_create_family_members_table.php b/database/migrations/2024_04_05_085134_create_family_members_table.php new file mode 100644 index 0000000..0530260 --- /dev/null +++ b/database/migrations/2024_04_05_085134_create_family_members_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignId('dafac_id')->constrained(); + $table->string('name'); + $table->string('relation_to_head'); + $table->unsignedInteger('age'); + $table->string('sex'); + $table->string('educational_attainment'); + $table->string('occupational_skills'); + $table->string('remarks'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('family_members'); + } +}; diff --git a/tests/Feature/Models/FamilyMemberTest.php b/tests/Feature/Models/FamilyMemberTest.php new file mode 100644 index 0000000..b654719 --- /dev/null +++ b/tests/Feature/Models/FamilyMemberTest.php @@ -0,0 +1,41 @@ +create(); + // act - retrieve model + $value = FamilyMember::find($model->getKey())->$attribute; + // assert - attribute is not null + $this->assertNotNull($value); + } + + public static function dafacAttributesProvider() + { + // * use $_dataName on @testdox to get key + return [ + 'name of family member' => ['name'], + 'relation to family head' => ['relation_to_head'], + 'age' => ['age'], + 'sex' => ['sex'], + 'educational attainment' => ['educational_attainment'], + 'occupational skills' => ['occupational_skills'], + 'remarks' => ['remarks'], + ]; + } +}