write new tests

This commit is contained in:
2025-05-12 17:05:42 +03:30
parent 70f3c36f58
commit db8cda55df
13 changed files with 507 additions and 18 deletions

View File

@@ -0,0 +1,157 @@
<?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 UpdateTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_the_user_is_not_authenticated()
{
$role = Role::factory()->create();
$response = $this->postJson(route('roles.update',$role));
$response->assertUnauthorized();
}
public function test_fields_are_required(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'));
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.required', ['attribute' => 'نام']),
'name_fa' => __('validation.required', ['attribute' => 'نام فارسی']),
'permissions' => __('validation.required', ['attribute' => 'دسترسی ها']),
]);
}
public function test_fields_must_be_unique(): void
{
$user = User::factory()->create();
$duplicateName = $this->faker->unique()->slug;
$duplicateNameFa = $this->faker->unique()->word;
Role::create([
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
'guard_name' => 'web',
]);
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $duplicateName,
'name_fa' => $duplicateNameFa,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.unique', ['attribute' => 'نام']),
'name_fa' => __('validation.unique', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_be_strings(): void
{
$user = User::factory()->create();
$permission = Permission::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => 123,
'name_fa' => 456,
'permissions' => [$permission->id],
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.string', ['attribute' => 'نام']),
'name_fa' => __('validation.string', ['attribute' => 'نام فارسی']),
]);
}
public function test_name_and_name_fa_must_not_exceed_max_length(): void
{
$user = User::factory()->create();
$longString = str_repeat('a', 256);
$permission = Permission::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $longString,
'name_fa' => $longString,
'permissions' => [$permission->id],
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.max.string', ['attribute' => 'نام', 'max' => 255]),
'name_fa' => __('validation.max.string', ['attribute' => 'نام فارسی', 'max' => 255]),
]);
}
public function test_permissions_must_be_array(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.store'), [
'name' => $this->faker->slug,
'name_fa' => $this->faker->word,
'permissions' => 1,
]);
$response->assertUnprocessable()
->assertInvalid([
'permissions' => __('validation.array', ['attribute' => 'دسترسی ها']),
]);
}
public function test_permissions_values_must_exist(): void
{
$user = User::factory()->create();
$role = Role::factory()->create();
$response = $this->actingAs($user)->postJson(route('roles.update', [$role->id]), [
'permissions' => $this->faker->name(),
]);
$response->assertUnprocessable()
->assertInvalid([
'permissions' => __('validation.exists', ['attribute' => 'دسترسی ها']),
]);
}
public function test_valid_role_is_update_successfully(): void
{
$user = User::factory()->create();
$permission = Permission::factory()->create();
$payload = [
'name' => $this->faker->unique()->slug,
'name_fa' => $this->faker->unique()->words(2, true),
'permissions' => [$permission->id],
];
$response = $this->actingAs($user)->postJson(route('roles.store'), $payload);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('roles', [
'name' => $payload['name'],
'name_fa' => $payload['name_fa'],
]);
$this->assertDatabaseHas('role_has_permissions', [
'permission_id' => $permission->id,
]);
}
}