diff --git a/composer.json b/composer.json index 20ce219..6cc22a0 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,7 @@ "mockery/mockery": "^1.6", "nunomaduro/collision": "^8.6", "phpunit/phpunit": "^11.5", - "orchestra/testbench": "^9.9", - "laravel/legacy-factories": "^1.0" + "orchestra/testbench": "^9.9" }, "autoload": { "psr-4": { @@ -37,9 +36,10 @@ "psr-4": { "Specialtactics\\L5Api\\Tests\\": "test/tests/", "Specialtactics\\L5Api\\Test\\Mocks\\": "test/mocks/", - "App\\": "test/app/" + "App\\": "test/app/", + "Database\\": "test/database/" }, - "classmap": ["test/database/seeds/", "test/database/factories/"] + "classmap": ["test/database/seeds/"] }, "extra": { "laravel": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8790b9d..98af3fc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,6 +17,7 @@ + diff --git a/test/app/Models/User.php b/test/app/Models/User.php index ea61c0a..3edca8a 100644 --- a/test/app/Models/User.php +++ b/test/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Notifications\Notifiable; use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Passwords\CanResetPassword; @@ -9,9 +10,11 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; +use Illuminate\Support\Str; use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject; -use Hash; +use \Illuminate\Support\Facades\Hash; use App\Models\Role; +use Database\Factories\UserFactory; class User extends BaseModel implements AuthenticatableContract, @@ -19,7 +22,7 @@ class User extends BaseModel implements CanResetPasswordContract, JWTSubject { - use Authenticatable, Authorizable, CanResetPassword, Notifiable; + use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasFactory; /** * @var int Auto increments integer key @@ -40,10 +43,13 @@ class User extends BaseModel implements 'name', 'email', 'password', 'primary_role' ]; + protected $casts = [ + 'verifiable_until' => 'datetime', + 'password' => 'hashed', + ]; + /** * The attributes that should be hidden for arrays and API output - * - * @var array */ protected $hidden = [ 'password', 'remember_token', 'primary_role', @@ -56,14 +62,18 @@ public static function boot(): void { parent::boot(); - static::saving(function (User $user) { - // Hash user password, if not already hashed - if (Hash::needsRehash($user->password)) { - $user->password = Hash::make($user->password); + static::creating(function (User $user) { + if (is_null($user->password)) { + $user->password = Hash::make(Str::random(64)); } }); } + protected static function newFactory(): UserFactory + { + return new UserFactory(); + } + /** * Return the validation rules for this model * diff --git a/test/database/Factories/UserFactory.php b/test/database/Factories/UserFactory.php new file mode 100644 index 0000000..4acf843 --- /dev/null +++ b/test/database/Factories/UserFactory.php @@ -0,0 +1,27 @@ + $this->faker->name, + 'email' => $this->faker->unique()->safeEmail, + 'password' => 'secret', + 'remember_token' => Str::random(10), + ]; + } +} diff --git a/test/database/factories/UserFactory.php b/test/database/factories/UserFactory.php deleted file mode 100644 index 2be906c..0000000 --- a/test/database/factories/UserFactory.php +++ /dev/null @@ -1,24 +0,0 @@ -define(App\Models\User::class, function (Faker $faker) { - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret - 'remember_token' => Str::random(10), - ]; -}); diff --git a/test/database/seeds/UserStorySeeder.php b/test/database/seeds/UserStorySeeder.php index 2d72e20..fd63b49 100644 --- a/test/database/seeds/UserStorySeeder.php +++ b/test/database/seeds/UserStorySeeder.php @@ -15,14 +15,14 @@ public function runFake() { $roles = Role::all(); // Create an admin user - factory(App\Models\User::class)->create([ + User::factory()->create([ 'name' => 'Admin', 'email' => 'admin@admin.com', 'primary_role' => $roles->where('name', 'admin')->first()->role_id, ]); // Create regular user - factory(App\Models\User::class)->create([ + User::factory()->create([ 'name' => 'Bob', 'email' => 'bob@bob.com', 'primary_role' => $roles->where('name', 'regular')->first()->role_id, @@ -34,7 +34,7 @@ public function runFake() { // Assign fake roles to users for ($i = 0; $i < 5; ++$i) { - $user = factory(App\Models\User::class)->create([ + $user = User::factory()->create([ 'primary_role' => $roles->random()->role_id, ]); diff --git a/test/tests/SetupTestApp.php b/test/tests/SetupTestApp.php index 595d6cf..54b6daa 100644 --- a/test/tests/SetupTestApp.php +++ b/test/tests/SetupTestApp.php @@ -23,9 +23,6 @@ protected function setUp(): void // Load migrations for tests $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); - // Load factories for tests - $this->withFactories(__DIR__ . '/../database/factories'); - // Migrate and seed $this->artisan('migrate', ['--database' => 'testing', '--seed' => true]); }