Files
ddd-example/tests/Support/WithLogin.php
T
2022-09-25 03:46:14 +08:00

51 lines
1.5 KiB
PHP

<?php
namespace Tests\Support;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Testing\TestResponse;
use Src\Zone\User\Application\Mappers\UserMapper;
use Src\Zone\User\Domain\Factories\UserFactory;
trait WithLogin
{
use WithFaker;
/**
* Create a new user instance.
*/
private function validCredentials(array $attributes = null): array
{
$password = $this->faker->password(8);
$user = UserFactory::new($attributes);
$userEloquentModel = UserMapper::toEloquent($user);
$userEloquentModel->password = $password;
$userEloquentModel->save();
return [
'id' => $userEloquentModel->id,
'email' => $user->email,
'password' => $password,
];
}
private function newLoggedAdmin(): array
{
$credentials = $this->validCredentials(['is_admin' => true]);
$response = $this->post('auth/login', $credentials);
return ['token' => $this->getToken($response), ...$credentials];
}
private function newLoggedUser(): array
{
$credentials = $this->validCredentials(['is_admin' => false]);
$response = $this->post('auth/login', $credentials);
return ['token' => $this->getToken($response), ...$credentials];
}
private function getToken(TestResponse $response)
{
$arResponse = json_decode($response->getContent(), true);
return $arResponse['accessToken'];
}
}