write test for permission controller

This commit is contained in:
2025-03-11 11:47:43 +03:30
parent f394f9cd0c
commit 6ec1f6c2b4
9 changed files with 375 additions and 17 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Feature\V3\Permission;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class IndexTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_user_should_have_manage_permission(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->getJson(route('v3.permissions.index'));
$response->assertForbidden();
}
public function test_all_permissions_returned_successfully(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->getJson(route('v3.permissions.index', [
'start' => 0,
'size' => 10,
'filters' => json_encode([]),
'sorting' => json_encode([]),
]));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'name',
'name_fa',
'type',
'type_fa',
'description',
],
],
"meta" => [
"totalRowCount"
]
]);
}
}

View File

@@ -0,0 +1,297 @@
<?php
namespace Tests\Feature\V3\Permission;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class StoreTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_user_should_have_manage_permission(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertForbidden();
}
public function test_name_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.required', ['attribute' => 'نام'])
]);
}
public function test_name_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.string', ['attribute' => 'نام'])
]);
}
public function test_name_attribute_should_be_unique(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$permission = Permission::factory()->create([
'name' => $this->faker->name()
]);
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name' => $permission->name,
]);
$response->assertUnprocessable()
->assertInvalid([
'name' => __('validation.unique', ['attribute' => 'نام'])
]);
}
public function test_name_fa_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'name_fa' => __('validation.required', ['attribute' => 'name fa'])
]);
}
public function test_name_fa_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'name_fa' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'name_fa' => __('validation.string', ['attribute' => 'name fa'])
]);
}
public function test_description_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'description' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'description' => __('validation.string', ['attribute' => 'توضیحات'])
]);
}
public function test_type_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'type' => __('validation.required', ['attribute' => 'type'])
]);
}
public function test_type_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'type' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'type' => __('validation.string', ['attribute' => 'type'])
]);
}
public function test_type_fa_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'type_fa' => __('validation.required', ['attribute' => 'type fa'])
]);
}
public function test_type_fa_attribute_should_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'type_fa' => $this->faker->numberBetween(1, 10)
]);
$response->assertUnprocessable()
->assertInvalid([
'type_fa' => __('validation.string', ['attribute' => 'type fa'])
]);
}
public function test_need_province_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'need_province' => __('validation.required', ['attribute' => 'need province'])
]);
}
public function test_need_province_attribute_should_be_1_or_0(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'need_province' => $this->faker->numberBetween(3, 9),
]);
$response->assertUnprocessable()
->assertInvalid([
'need_province' => __('validation.in', ['attribute' => 'need province'])
]);
}
public function test_need_edare_shahri_attribute_is_required(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'));
$response->assertUnprocessable()
->assertInvalid([
'need_edare_shahri' => __('validation.required', ['attribute' => 'need edare shahri'])
]);
}
public function test_need_edare_shahri_attribute_should_be_1_or_0(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
'need_edare_shahri' => $this->faker->numberBetween(3, 9),
]);
$response->assertUnprocessable()
->assertInvalid([
'need_edare_shahri' => __('validation.in', ['attribute' => 'need edare shahri'])
]);
}
public function test_user_can_create_a_permission()
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-permissions'
]))
->create();
$inputData = [
'name' => $this->faker->name(),
'name_fa' => $this->faker->name(),
'description' => $this->faker->lexify(),
'type' => $this->faker->name(),
'type_fa' => $this->faker->name(),
'need_province' => $this->faker->numberBetween(0, 1),
'need_edare_shahri' => $this->faker->numberBetween(0, 1),
];
$response = $this->actingAs($user)->postJson(route('v3.permissions.store'), $inputData);
$response->assertOk()
->assertExactJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('permissions', $inputData);
}
}