Skip to content

Commit 7c92752

Browse files
committed
feat: add AuthenticationSupport trait for user login in tests
1 parent e9a01c9 commit 7c92752

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Feature;
6+
7+
use Contexts\Authorization\Domain\UserIdentity\Models\UserStatus;
8+
use Contexts\Authorization\Infrastructure\Records\UserRecord;
9+
10+
trait AuthenticationSupport
11+
{
12+
protected function loginAsUser(array $attributes = []): void
13+
{
14+
$plainPassword = $attributes['password'] ?? 'password';
15+
$userRecord = UserRecord::factory()->create([
16+
'email' => $attributes['email'] ?? 'logged-in-user@email.com',
17+
'display_name' => $attributes['display_name'] ?? 'Logged In User',
18+
'password' => password_hash($plainPassword, PASSWORD_ARGON2ID),
19+
'status' => UserRecord::mapStatusToRecord($attributes['status'] ?? UserStatus::active()),
20+
]);
21+
22+
$this->actingAs($userRecord);
23+
}
24+
}

tests/Pest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Tests\Feature\AuthenticationSupport;
67

78
/*
89
|--------------------------------------------------------------------------
@@ -15,8 +16,8 @@
1516
|
1617
*/
1718

18-
uses(Tests\TestCase::class, RefreshDatabase::class)->in(__DIR__.'/../contexts/*/Tests/Feature');
19-
uses(Tests\TestCase::class, RefreshDatabase::class)->in(__DIR__.'/../tests/Feature');
19+
uses(Tests\TestCase::class, RefreshDatabase::class, AuthenticationSupport::class)->in(__DIR__.'/../contexts/*/Tests/Feature');
20+
uses(Tests\TestCase::class, RefreshDatabase::class, AuthenticationSupport::class)->in(__DIR__.'/../tests/Feature');
2021

2122
/*
2223
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)