Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
refactor(tests): extract some factory methods to trait
Browse files Browse the repository at this point in the history
Simply to make it easier to customize factory classes in app-level

Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Oct 14, 2023
1 parent ce3e6af commit ae51913
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 78 deletions.
19 changes: 4 additions & 15 deletions database/factories/BusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Database\Factories;

use Creasi\Base\Models\Address;
use Creasi\Base\Models\Business;
use Creasi\Base\Models\Enums\FileUploadType;
use Creasi\Base\Models\FileUpload;
use Creasi\Base\Models\Concerns\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Business>
*/
class BusinessFactory extends Factory
{
use Factories\WithAddress;
use Factories\WithFileUpload;

protected $model = Business::class;

/**
Expand All @@ -27,16 +28,4 @@ public function definition(): array
'summary' => $this->faker->sentence(4),
];
}

public function withAddress(): static
{
return $this->has(Address::factory());
}

public function withFileUpload(FileUploadType $type = null): static
{
return $this->hasAttached(FileUpload::factory(), [
'type' => $type ?? $this->faker->randomElement(FileUploadType::cases()),
], 'files');
}
}
51 changes: 5 additions & 46 deletions database/factories/PersonnelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

namespace Database\Factories;

use Creasi\Base\Models\Address;
use Creasi\Base\Models\Business;
use Creasi\Base\Models\Enums\EmploymentStatus;
use Creasi\Base\Models\Enums\EmploymentType;
use Creasi\Base\Models\Enums\FileUploadType;
use Creasi\Base\Models\Concerns\Factories;
use Creasi\Base\Models\Enums\Gender;
use Creasi\Base\Models\FileUpload;
use Creasi\Base\Models\Personnel;
use Creasi\Base\Models\Profile;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Personnel>
*/
class PersonnelFactory extends Factory
{
use Factories\AsPersonnel;
use Factories\WithAddress;
use Factories\WithFileUpload;

protected $model = Personnel::class;

/**
Expand All @@ -44,42 +41,4 @@ public function withoutUser(): static
'user_id' => null,
]);
}

public function withProfile(Gender $gender = null): static
{
return $this->has(Profile::factory(), 'profile')->state(fn () => [
'name' => $this->faker->firstName($gender?->toFaker()),
]);
}

public function withCompany(
bool $primary = null,
EmploymentType $type = null,
EmploymentStatus $status = null,
false|DateTimeInterface $startDate = null,
): static {
if (null === $startDate) {
$startDate = $this->faker->dateTime();
}

return $this->hasAttached(Business::factory(), [
'is_primary' => $primary,
'type' => $type ?? $this->faker->randomElement(EmploymentType::cases()),
'status' => $status ?? $this->faker->randomElement(EmploymentStatus::cases()),
'start_date' => $startDate?->format('Y-m-d'),
'finish_date' => null,
], 'employers');
}

public function withAddress(): static
{
return $this->has(Address::factory());
}

public function withFileUpload(FileUploadType $type = null): static
{
return $this->hasAttached(FileUpload::factory(), [
'type' => $type ?? $this->faker->randomElement(FileUploadType::cases()),
], 'files');
}
}
10 changes: 4 additions & 6 deletions database/factories/ProfileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Database\Factories;

use Creasi\Base\Models\Enums\Education;
use Creasi\Base\Models\Enums\Religion;
use Creasi\Base\Models\Enums\TaxStatus;
use Creasi\Base\Models\Enums;
use Creasi\Base\Models\Profile;
use Creasi\Nusa\Models\Regency;
use Illuminate\Contracts\Database\Query\Builder;
Expand All @@ -30,9 +28,9 @@ public function definition(): array
'nik' => $nik = $this->faker->nik(null, $birthDate = $this->faker->dateTime()),
'birth_date' => $birthDate->format('Y-m-d'),
'birth_place_code' => $birthPlace->code,
'education' => $this->faker->randomElement(Education::cases()),
'religion' => $this->faker->randomElement(Religion::cases()),
'tax_status' => $this->faker->randomElement(TaxStatus::cases()),
'education' => $this->faker->randomElement(Enums\Education::cases()),
'religion' => $this->faker->randomElement(Enums\Religion::cases()),
'tax_status' => $this->faker->randomElement(Enums\TaxStatus::cases()),
'tax_id' => $nik,
];
}
Expand Down
13 changes: 3 additions & 10 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Database\Factories;

use Creasi\Base\Models\Personnel;
use Creasi\Base\Models\Concerns\Factories\WithIdentity;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

Expand All @@ -11,6 +11,8 @@
*/
class UserFactory extends Factory
{
use WithIdentity;

public function modelName()
{
return app('creasi.base.user_model');
Expand Down Expand Up @@ -41,13 +43,4 @@ public function unverified(): static
'email_verified_at' => null,
]);
}

public function withIdentity(\Closure $cb = null): static
{
if ($cb === null) {
$cb = fn (PersonnelFactory $identity) => $identity->withProfile();
}

return $this->has($cb(Personnel::factory()), 'identity');
}
}
2 changes: 1 addition & 1 deletion src/Models/Business.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @method static \Database\Factories\BusinessFactory<static> factory()
*/
class Business extends Entity implements HasTaxInfo, Company
class Business extends Entity implements Company, HasTaxInfo
{
use AsCompany;
use WithTaxInfo;
Expand Down
40 changes: 40 additions & 0 deletions src/Models/Concerns/Factories/AsPersonnel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Creasi\Base\Models\Concerns\Factories;

use Creasi\Base\Models\Business;
use Creasi\Base\Models\Enums;
use Creasi\Base\Models\Profile;
use DateTimeInterface;

/**
* @mixin \Illuminate\Database\Eloquent\Factories\Factory
*/
trait AsPersonnel
{
public function withProfile(Enums\Gender $gender = null): static
{
return $this->has(Profile::factory(), 'profile')->state(fn () => [
'name' => $this->faker->firstName($gender?->toFaker()),
]);
}

public function withCompany(
bool $primary = null,
Enums\EmploymentType $type = null,
Enums\EmploymentStatus $status = null,
false|DateTimeInterface $startDate = null,
): static {
if ($startDate === null) {
$startDate = $this->faker->dateTime();
}

return $this->hasAttached(Business::factory(), [
'is_primary' => $primary,
'type' => $type ?? $this->faker->randomElement(Enums\EmploymentType::cases()),
'status' => $status ?? $this->faker->randomElement(Enums\EmploymentStatus::cases()),
'start_date' => $startDate?->format('Y-m-d'),
'finish_date' => null,
], 'employers');
}
}
16 changes: 16 additions & 0 deletions src/Models/Concerns/Factories/WithAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Creasi\Base\Models\Concerns\Factories;

use Creasi\Base\Models\Address;

/**
* @mixin \Illuminate\Database\Eloquent\Factories\Factory
*/
trait WithAddress
{
public function withAddress(): static
{
return $this->has(Address::factory());
}
}
19 changes: 19 additions & 0 deletions src/Models/Concerns/Factories/WithFileUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Creasi\Base\Models\Concerns\Factories;

use Creasi\Base\Models\Enums\FileUploadType;
use Creasi\Base\Models\FileUpload;

/**
* @mixin \Illuminate\Database\Eloquent\Factories\Factory
*/
trait WithFileUpload
{
public function withFileUpload(FileUploadType $type = null): static
{
return $this->hasAttached(FileUpload::factory(), [
'type' => $type ?? $this->faker->randomElement(FileUploadType::cases()),
], 'files');
}
}
21 changes: 21 additions & 0 deletions src/Models/Concerns/Factories/WithIdentity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Creasi\Base\Models\Concerns\Factories;

use Creasi\Base\Models\Personnel;
use Database\Factories\PersonnelFactory;

/**
* @mixin \Illuminate\Database\Eloquent\Factories\Factory
*/
trait WithIdentity
{
public function withIdentity(\Closure $cb = null): static
{
if ($cb === null) {
$cb = fn (PersonnelFactory $identity) => $identity->withProfile();
}

return $this->has($cb(Personnel::factory()), 'identity');
}
}

0 comments on commit ae51913

Please sign in to comment.