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

Commit

Permalink
chore(tests): update test config
Browse files Browse the repository at this point in the history
Signed-off-by: Fery Wardiyanto <[email protected]>
  • Loading branch information
feryardiant committed Mar 30, 2024
1 parent d8584f1 commit 8783530
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 34 deletions.
13 changes: 6 additions & 7 deletions src/Database/Factories/Concerns/WithProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
14 changes: 6 additions & 8 deletions src/Database/Factories/PersonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('##########'),
Expand All @@ -50,7 +51,7 @@ public function definition(): array
];
}

public function withoutUser(): static
public function withCredental(): static
{
return $this->state([
'user_id' => null,
Expand Down Expand Up @@ -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');
}

Expand Down
7 changes: 0 additions & 7 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Http/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
5 changes: 0 additions & 5 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,4 @@ protected static function newFactory()
{
return UserFactory::new();
}

public function password(): Attribute
{
return Attribute::set(fn (string $value) => \bcrypt($value));
}
}
5 changes: 5 additions & 0 deletions workbench/app/Providers/WorkbenchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,10 @@ public function register(): void
]);
}
});

Factory::guessFactoryNamesUsing(function (string $modelName) {
return Factory::$namespace.\class_basename($modelName).'Factory';
});
}

/**
Expand Down
14 changes: 13 additions & 1 deletion workbench/database/Seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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' => '[email protected]',
]);
}
}

0 comments on commit 8783530

Please sign in to comment.