Files
backend/tests/Feature/Role/ShowTest.php
2025-05-12 17:05:42 +03:30

52 lines
1.3 KiB
PHP

<?php
namespace Tests\Feature\Role;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ShowTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_guest_cannot_view_role(): void
{
$role = Role::factory()->create();
$response = $this->postJson(route('roles.show',[$role->id]));
$response->assertUnauthorized();
}
public function test_a_role_can_be_returned_properly():void
{
$user = User::factory()->create();
$role = Role::factory()->create();
$permission = Permission::factory()->create([
'id' => 2,
'name' => $this->faker->name,
'name_fa' => $this->faker->name
]);
$role->syncPermissions($permission);
$role = Role::factory()->create();
$response = $this->actingAs($user)->getJson(route('roles.show', [$role->id]));
$response->assertOk()
->assertJson([
'data' => [
'id' => $role->id,
'name' => $role->name,
'name_fa' => $role->name_fa,
'permissions' => $role->permissions->pluck('name')->toArray(),
]
]);
}
}