Skip to content

Commit

Permalink
use FamilyMember model
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarkfen committed Apr 5, 2024
1 parent 23681bc commit 3761de2
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/Models/Dafac.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public function ethnicity_type()
{
return $this->belongsTo(EthnicityType::class);
}

public function members()
{
return $this->hasMany(FamilyMember::class);
}
}
21 changes: 21 additions & 0 deletions app/Models/FamilyMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use App\Enums\Gender;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class FamilyMember extends Model
{
use HasFactory;

protected $casts = [
'sex' => Gender::class,
];

public function dafac()
{
return $this->belongsTo(Dafac::class);
}
}
19 changes: 18 additions & 1 deletion database/factories/DafacFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories;

use App\Enums\Gender;
use App\Models\Dafac;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand Down Expand Up @@ -40,12 +41,28 @@ 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),
'house_ownership' => fake()->word(),
'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();
});
}
}
31 changes: 31 additions & 0 deletions database/factories/FamilyMemberFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Factories;

use App\Enums\Gender;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\FamilyMember>
*/
class FamilyMemberFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
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(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('family_members', function (Blueprint $table) {
$table->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');
}
};
41 changes: 41 additions & 0 deletions tests/Feature/Models/FamilyMemberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Feature\Models;

use App\Models\FamilyMember;
use Database\Factories\FamilyMemberFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class FamilyMemberTest extends TestCase
{
use RefreshDatabase;
/**
* @dataProvider dafacAttributesProvider
* @testdox $_dataName is not null
*/
public function test_attribute_not_null($attribute)
{
// arrange - create model
$model = FamilyMemberFactory::new()->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'],
];
}
}

0 comments on commit 3761de2

Please sign in to comment.