This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
generated from creasico/laravel-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): extract some factory methods to trait
Simply to make it easier to customize factory classes in app-level Signed-off-by: Fery Wardiyanto <[email protected]>
- Loading branch information
1 parent
ce3e6af
commit ae51913
Showing
9 changed files
with
113 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |