ERP-API/tests/Feature/Auth/User/StoreTest.php

88 lines
2.1 KiB
PHP

<?php
namespace Tests\Feature\Auth\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\Role;
use App\Models\Permission;
use App\Models\User;
use Laravel\Sanctum\Sanctum;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class StoreTest extends TestCase
{
use DatabaseTransactions;
/**
* A basic feature test example.
*/
public function test_success(): void
{
$permission = Permission::where("code","auth.user:create")->first();
$role = Role::factory()->create();
$role->permissions()->attach($permission->id);
$user = User::factory()->create();
$user->roles()->attach($role->id);
Sanctum::actingAs($user);
$response = $this->post('/auth/user/',[
"name" => "new user",
"email" => "user@gmail.com"
]);
$response->assertStatus(201);
$response->assertJson([
"data" => [
"name" => "new user",
"email" => "user@gmail.com"
]
]);
}
/**
* A basic feature test example.
*/
public function test_with_roles_success(): void
{
$permission = Permission::where("code","auth.user:create")->first();
$role = Role::factory()->create();
$role->permissions()->attach($permission->id);
$user = User::factory()->create();
$user->roles()->attach($role->id);
$role2 = Role::factory()->create();
Sanctum::actingAs($user);
$response = $this->post('/auth/user/',[
"name" => "new user",
"email" => "user@gmail.com",
"role_ids" => [$role->id, $role2->id]
]);
$response->assertStatus(201);
$response->assertJson([
"data" => [
"name" => "new user",
"email" => "user@gmail.com",
"roles" => [
[
"id" => $role->id
],
[
"id" => $role2->id
],
]
]
]);
}
}