crate file test for damage and fix update request and add 2 option in validation

This commit is contained in:
2025-02-22 17:10:14 +03:30
parent c85b826563
commit 32630f6400
8 changed files with 519 additions and 1 deletions

View File

@@ -25,7 +25,7 @@ class UpdateRequest extends FormRequest
return [
'title' =>['required', 'string', 'max:255',Rule::unique('damages','title')->ignore($this->damage->id)],
'unit' =>'required|string',
'base_price' =>'required|numeric',
'base_price' =>'required|integer',
];
}
}

View File

@@ -202,5 +202,7 @@ return [
'fields.*.option' => 'گزینه های فیلد',
'cmms_machine_id' => 'کد یکتا ماشین',
'contract_subitem_id' => 'کد یکتای پروژه',
'base_price' => 'قیمت پایه',
'unit' => 'واحد'
],
];

View File

@@ -0,0 +1,12 @@
<?php
namespace Tests\Feature\V3\Damage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ActivateTest extends TestCase
{
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature\V3\Damage;
use App\Models\Damage;
use App\Models\Permission;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class IndexTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_user_should_have_manage_damage_permission_index(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->getJson(route('v3.damages.index'));
$response->assertForbidden();
}
public function test_user_cannot_access_authentication(): void
{
$response = $this->getJson(route('v3.damages.index'));
$response->assertStatus(401);
}
public function test_all_damages_returned_successfully_for_index(): void
{
$user = User::factory()
->has(factory: Permission::factory([
'name' => 'manage-damage'
]))
->create();
Damage::factory()->count(5)->create();
$response = $this->actingAs($user)->getJson(route('v3.damages.index', [
'start' => 0,
'size' => 10,
'filters' => json_encode([]),
'sorting' => json_encode([]),
]));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'title',
'unit',
'base_price',
'status',
],
],
'meta' => [
'totalRowCount',
],
]);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Tests\Feature\V3\Damage;
use App\Models\Damage;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;
class ListTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_user_cannot_access_authentication(): void
{
$response = $this->getJson(route('v3.damages.list'));
$response->assertStatus(401);
}
public function test_all_damages_returned_successfully_for_list(): void
{
$user = User::factory()->create();
Damage::factory()->count(5)->create();
$response = $this->actingAs($user)->getJson(route('v3.damages.list'));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'title',
'unit',
'base_price',
'status',
],
],
]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Feature\V3\Damage;
use App\Models\Damage;
use App\Models\Permission;
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_unauthenticated_user_cannot_view_damage(): void
{
$damage = Damage::factory()->create();;
$response = $this->getJson(route('v3.damages.show', [$damage->id]));
$response->assertStatus(401);
}
public function test_user_without_permission_cannot_view_damage(): void
{
$user = User::factory()->create();
$damage = Damage::factory()->create();;
$response = $this->actingAs($user)->getJson(route('v3.damages.show', [$damage->id]));
$response->assertForbidden();
}
public function test_user_with_permission_can_view_damage(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->getJson(route('v3.damages.show', [$damage->id]));
$response->assertOk()
->assertJson([
'data' => [
'id' => $damage->id,
'title' => $damage->title,
'unit' => $damage->unit,
'base_price' => $damage->base_price,
'status' => $damage->status,
]
]);
}
}

View File

@@ -0,0 +1,156 @@
<?php
namespace Tests\Feature\V3\Damage;
use App\Models\Damage;
use App\Models\Permission;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
class StoreTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_unauthenticated_user_cannot_store_damage(): void
{
$response = $this->postJson(route('v3.damages.store'));
$response->assertStatus(401);
}
public function test_user_without_permission_cannot_store_damage(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.store'));
$response->assertForbidden();
}
public function test_validation_fails_when_required_fields_are_missing(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.store'));
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.required', ['attribute' => "عنوان"]),
'unit' => __('validation.required', ['attribute' => 'واحد']),
'base_price' => __('validation.required', ['attribute' => 'قیمت پایه']),
]);
}
public function test_title_must_be_unique(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
'title' => $damage->title,
'unit' => 'kg',
'base_price' => 5000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.unique', ['attribute' => "عنوان"]),
]);
}
public function test_store_title_and_unit_must_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
'title' => 12345,
'unit' => 67890,
'base_price' => 10000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.string', ['attribute' => "عنوان"]),
'unit' => __('validation.string', ['attribute' => 'واحد']),
]);
}
public function test_store_title_must_not_exceed_255_characters(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$longTitle = $this->faker->text(500);
$response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
'title' => $longTitle,
'unit' => 'kg',
'base_price' => 10000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.max.string',['attribute' => 'عنوان', 'max' => 255]),
]);
}
public function test_store_base_price_must_be_intger(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.store'), [
'title' => 'Valid Title',
'unit' => 'm',
'base_price' => 'valid intger',
]);
$response->assertUnprocessable()
->assertInvalid([
'base_price' => __('validation.integer', ['attribute' => 'قیمت پایه']),
]);
}
public function test_user_with_permission_can_store_damage(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$data = [
'title' => 'New Damage',
'unit' => 'm',
'base_price' => 10000,
];
$response = $this->actingAs($user)->postJson(route('v3.damages.store'), $data);
$response->assertOk()
->assertJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('damages', $data);
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace Tests\Feature\V3\Damage;
use App\Models\Damage;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use WithFaker, RefreshDatabase;
public function test_unauthenticated_user_cannot_update_damage(): void
{
$damage = Damage::factory()->create();;
$response = $this->getJson(route('v3.damages.update', [$damage->id]));
$response->assertStatus(401);
}
public function test_user_without_permission_cannot_update_damage(): void
{
$user = User::factory()->create();
$damage = Damage::factory()->create();;
$response = $this->actingAs($user)->getJson(route('v3.damages.update', [$damage->id]));
$response->assertForbidden();
}
public function test_validation_fails_when_required_fields_are_missing(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]));
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.required', ['attribute' => "عنوان"]),
'unit' => __('validation.required', ['attribute' => 'واحد']),
'base_price' => __('validation.required', ['attribute' => 'قیمت پایه']),
]);
}
public function test_title_must_be_unique(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$existingDamage = Damage::factory()->create([
'title' => 'Existing Title'
]);
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]),[
'title' => $existingDamage->title,
'unit' => 'm',
'base_price' => 5000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.unique', ['attribute' => "عنوان"]),
]);
}
public function test_title_and_unit_must_be_string(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
'title' => 12345,
'unit' => 67890,
'base_price' => 10000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.string', ['attribute' => "عنوان"]),
'unit' => __('validation.string', ['attribute' => 'واحد']),
]);
}
public function test_title_must_not_exceed_255_characters(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$longTitle = $this->faker->text(500);
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
'title' => $longTitle,
'unit' => 'kg',
'base_price' => 10000,
]);
$response->assertUnprocessable()
->assertInvalid([
'title' => __('validation.max.string',['attribute' => 'عنوان', 'max' => 255]),
]);
}
public function test_base_price_must_be_integer(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), [
'title' => 'Valid Title',
'unit' => 'kg',
'base_price' => 'invalid_price',]);
$response->assertUnprocessable()
->assertInvalid([
'base_price' => __('validation.integer', ['attribute' => 'قیمت پایه']),
]);
}
public function test_user_with_permission_can_update_damage(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage-damage'
]))
->create();
$damage = Damage::factory()->create();
$updateData = [
'title' => 'Updated Damage',
'unit' => 'litre',
'base_price' => 20000,
];
$response = $this->actingAs($user)->postJson(route('v3.damages.update', [$damage->id]), $updateData);
$response->assertStatus(200)
->assertJson([
'message' => __('messages.successful'),
]);
$this->assertDatabaseHas('damages', [
'id' => $damage->id,
'title' => $updateData['title'],
'unit' => $updateData['unit'],
'base_price' => $updateData['base_price'],
]);
}
}