Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<env name="API_PREFIX" value="/"/>
<env name="JWT_SECRET" value="WrL8dp51k231ErDyUMgazU9KceL1RKXusu1U39YARuMbUWKuurPEtqGhEDCrUMoB"/>
<env name="JWT_TTL" value="7220"/>
<env name="BCRYPT_ROUNDS" value="4" />
<!-- Test App -->
</php>
<source>
Expand Down
26 changes: 18 additions & 8 deletions test/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
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,
AuthorizableContract,
CanResetPasswordContract,
JWTSubject
{
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
use Authenticatable, Authorizable, CanResetPassword, Notifiable, HasFactory;

/**
* @var int Auto increments integer key
Expand All @@ -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',
Expand All @@ -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
*
Expand Down
27 changes: 27 additions & 0 deletions test/database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Factories;

use Illuminate\Support\Str;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
protected $model = User::class;

/**
* Standard definition for factory
*
* @return array
*/
public function definition(): array
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'password' => 'secret',
'remember_token' => Str::random(10),
];
}
}
24 changes: 0 additions & 24 deletions test/database/factories/UserFactory.php

This file was deleted.

6 changes: 3 additions & 3 deletions test/database/seeds/UserStorySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
]);

Expand Down
3 changes: 0 additions & 3 deletions test/tests/SetupTestApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down