From 878353084fe132e3523ca49e62af581f00624eb1 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Sat, 30 Mar 2024 20:35:45 +0700 Subject: [PATCH] chore(tests): update test config Signed-off-by: Fery Wardiyanto --- src/Database/Factories/Concerns/WithProfile.php | 13 ++++++------- src/Database/Factories/PersonFactory.php | 14 ++++++-------- src/ServiceProvider.php | 7 ------- tests/Feature/Http/Auth/AuthenticationTest.php | 8 ++++---- tests/TestCase.php | 4 ++-- workbench/app/Models/User.php | 5 ----- .../app/Providers/WorkbenchServiceProvider.php | 5 +++++ workbench/database/Seeders/DatabaseSeeder.php | 14 +++++++++++++- 8 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/Database/Factories/Concerns/WithProfile.php b/src/Database/Factories/Concerns/WithProfile.php index 3779441..003bdf8 100644 --- a/src/Database/Factories/Concerns/WithProfile.php +++ b/src/Database/Factories/Concerns/WithProfile.php @@ -2,19 +2,18 @@ namespace Creasi\Base\Database\Factories\Concerns; -use Creasi\Base\Database\Models\Person; +use Creasi\Base\Database\Factories\PersonFactory; /** * @mixin \Illuminate\Database\Eloquent\Factories\Factory */ trait WithProfile { - public function withIdentity(?\Closure $cb = null): static + public function withProfile(PersonFactory $profile): static { - if ($cb === null) { - $cb = fn ($profile) => $profile; - } - - return $this->has($cb(Person::factory()), 'profile'); + return $this->has($profile->state(fn ($_, $user) => [ + 'name' => $user->name, + 'email' => $user->email, + ]), 'profile'); } } diff --git a/src/Database/Factories/PersonFactory.php b/src/Database/Factories/PersonFactory.php index 2ce24ba..b90d14c 100644 --- a/src/Database/Factories/PersonFactory.php +++ b/src/Database/Factories/PersonFactory.php @@ -39,6 +39,7 @@ public function definition(): array $gender = \fake()->randomElement(Gender::cases()); return [ + 'user_id' => null, 'name' => \fake()->firstName($gender->toFaker()), 'email' => \fake()->safeEmail(), 'phone' => '08'.\fake()->numerify('##########'), @@ -50,7 +51,7 @@ public function definition(): array ]; } - public function withoutUser(): static + public function withCredental(): static { return $this->state([ 'user_id' => null, @@ -98,19 +99,16 @@ public function withOrganization( ?StakeholderType $type = null, ?StakeholderStatus $status = null, ?PersonnelStatus $employmentStatus = null, - false|DateTimeInterface|null $startDate = null, + ?DateTimeInterface $startDate = null, + ?DateTimeInterface $finishDate = null, ): static { - if ($startDate === null) { - $startDate = \fake()->dateTime(); - } - return $this->hasAttached(Organization::factory(), [ 'is_primary' => $primary, 'type' => $type ?? \fake()->randomElement(StakeholderType::cases()), 'status' => $status ?? \fake()->randomElement(StakeholderStatus::cases()), 'personnel_status' => $employmentStatus ?? \fake()->randomElement(PersonnelStatus::cases()), - 'start_date' => $startDate?->format('Y-m-d'), - 'finish_date' => null, + 'start_date' => ($startDate ?: \fake()->dateTimeBetween('-2 years', '-5 months'))->format('Y-m-d'), + 'finish_date' => $finishDate, ], 'employers'); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 19eee5c..b2d9edb 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -8,7 +8,6 @@ use Illuminate\Auth\Events\Login; use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Auth\Notifications\VerifyEmail; -use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Http\Request; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Event; @@ -75,12 +74,6 @@ public function register(): void $this->mergeConfigFrom(self::LIB_PATH.'/config/creasico.php', 'creasi.base'); } - if (app()->environment('testing')) { - Factory::guessFactoryNamesUsing(function (string $modelName) { - return Factory::$namespace.\class_basename($modelName).'Factory'; - }); - } - $this->registerBindings(); $this->booting(function (): void { diff --git a/tests/Feature/Http/Auth/AuthenticationTest.php b/tests/Feature/Http/Auth/AuthenticationTest.php index 03a29e3..cf14ccb 100644 --- a/tests/Feature/Http/Auth/AuthenticationTest.php +++ b/tests/Feature/Http/Auth/AuthenticationTest.php @@ -61,11 +61,11 @@ public function user_can_send_login_request(): void UserDeviceRegistered::class, ]); - $user = $this->user(['password' => $password = 'secret']); + $user = $this->user(); $response = $this->postJson('auth/login', [ 'credential' => $user->email, - 'password' => $password, + 'password' => 'password', ]); Event::assertDispatched(CredentialTokenCreated::class); @@ -82,12 +82,12 @@ public function user_can_send_login_request_and_register_new_device(): void UserDeviceRegistered::class, ]); - $user = $this->user(['password' => $password = 'secret']); + $user = $this->user(); $token = Str::random(); $response = $this->postJson('auth/login', [ 'credential' => $user->email, - 'password' => $password, + 'password' => 'password', 'device_token' => $token, ]); diff --git a/tests/TestCase.php b/tests/TestCase.php index 8763d1b..491c7be 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,7 +3,7 @@ namespace Creasi\Tests; use Closure; -use Creasi\Base\Database\Factories\PersonFactory; +use Creasi\Base\Database\Models\Person; use Creasi\Base\Enums\PersonnelStatus; use Illuminate\Foundation\Testing\DatabaseMigrations; use Orchestra\Testbench\Concerns\WithWorkbench; @@ -24,7 +24,7 @@ final protected function user( ): User { if (! $this->currentUser?->exists) { $this->currentUser = User::factory() - ->withIdentity(fn (PersonFactory $f) => $f->asEmployee($status, $isPrimary)) + ->withProfile(Person::factory()->asEmployee($status, $isPrimary)) ->createOne($attrs); } diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php index 05736ea..9c5a9d8 100644 --- a/workbench/app/Models/User.php +++ b/workbench/app/Models/User.php @@ -42,9 +42,4 @@ protected static function newFactory() { return UserFactory::new(); } - - public function password(): Attribute - { - return Attribute::set(fn (string $value) => \bcrypt($value)); - } } diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php index 2bb493c..8560e7d 100644 --- a/workbench/app/Providers/WorkbenchServiceProvider.php +++ b/workbench/app/Providers/WorkbenchServiceProvider.php @@ -3,6 +3,7 @@ namespace Workbench\App\Providers; use Illuminate\Config\Repository; +use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Workbench\App\Models\User; @@ -33,6 +34,10 @@ public function register(): void ]); } }); + + Factory::guessFactoryNamesUsing(function (string $modelName) { + return Factory::$namespace.\class_basename($modelName).'Factory'; + }); } /** diff --git a/workbench/database/Seeders/DatabaseSeeder.php b/workbench/database/Seeders/DatabaseSeeder.php index 9079f2d..3156e3c 100644 --- a/workbench/database/Seeders/DatabaseSeeder.php +++ b/workbench/database/Seeders/DatabaseSeeder.php @@ -2,7 +2,9 @@ namespace Workbench\Database\Seeders; +use Creasi\Base\Database\Models\Person; use Illuminate\Database\Seeder; +use Workbench\App\Models\User; class DatabaseSeeder extends Seeder { @@ -11,6 +13,16 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // + $this->createInitialUser(); + } + + private function createInitialUser(): void + { + User::factory()->withProfile( + Person::factory()->asEmployee() + )->createOne([ + 'name' => 'creasi', + 'email' => 'developers@creasi.dev', + ]); } }