From 32630f64006a2776d3634f054139268c972ebf23 Mon Sep 17 00:00:00 2001 From: joddyabott Date: Sat, 22 Feb 2025 17:10:14 +0330 Subject: [PATCH] crate file test for damage and fix update request and add 2 option in validation --- app/Http/Requests/V3/Damage/UpdateRequest.php | 2 +- resources/lang/fa/validation.php | 2 + tests/Feature/V3/Damage/ActivateTest.php | 12 ++ tests/Feature/V3/Damage/IndexTest.php | 66 +++++++ tests/Feature/V3/Damage/ListTest.php | 43 +++++ tests/Feature/V3/Damage/ShowTest.php | 59 ++++++ tests/Feature/V3/Damage/StoreTest.php | 156 +++++++++++++++ tests/Feature/V3/Damage/UpdateTest.php | 180 ++++++++++++++++++ 8 files changed, 519 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/V3/Damage/ActivateTest.php create mode 100644 tests/Feature/V3/Damage/IndexTest.php create mode 100644 tests/Feature/V3/Damage/ListTest.php create mode 100644 tests/Feature/V3/Damage/ShowTest.php create mode 100644 tests/Feature/V3/Damage/StoreTest.php create mode 100644 tests/Feature/V3/Damage/UpdateTest.php diff --git a/app/Http/Requests/V3/Damage/UpdateRequest.php b/app/Http/Requests/V3/Damage/UpdateRequest.php index 70b0833a..8021f716 100644 --- a/app/Http/Requests/V3/Damage/UpdateRequest.php +++ b/app/Http/Requests/V3/Damage/UpdateRequest.php @@ -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', ]; } } diff --git a/resources/lang/fa/validation.php b/resources/lang/fa/validation.php index 0f2de9dc..7f8bc492 100644 --- a/resources/lang/fa/validation.php +++ b/resources/lang/fa/validation.php @@ -202,5 +202,7 @@ return [ 'fields.*.option' => 'گزینه های فیلد', 'cmms_machine_id' => 'کد یکتا ماشین', 'contract_subitem_id' => 'کد یکتای پروژه', + 'base_price' => 'قیمت پایه', + 'unit' => 'واحد' ], ]; diff --git a/tests/Feature/V3/Damage/ActivateTest.php b/tests/Feature/V3/Damage/ActivateTest.php new file mode 100644 index 00000000..97678b63 --- /dev/null +++ b/tests/Feature/V3/Damage/ActivateTest.php @@ -0,0 +1,12 @@ +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', + ], + ]); + } +} diff --git a/tests/Feature/V3/Damage/ListTest.php b/tests/Feature/V3/Damage/ListTest.php new file mode 100644 index 00000000..ba71e7d6 --- /dev/null +++ b/tests/Feature/V3/Damage/ListTest.php @@ -0,0 +1,43 @@ +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', + ], + ], + ]); + + } +} + + diff --git a/tests/Feature/V3/Damage/ShowTest.php b/tests/Feature/V3/Damage/ShowTest.php new file mode 100644 index 00000000..9256c488 --- /dev/null +++ b/tests/Feature/V3/Damage/ShowTest.php @@ -0,0 +1,59 @@ +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, + ] + ]); + } + +} diff --git a/tests/Feature/V3/Damage/StoreTest.php b/tests/Feature/V3/Damage/StoreTest.php new file mode 100644 index 00000000..607815ad --- /dev/null +++ b/tests/Feature/V3/Damage/StoreTest.php @@ -0,0 +1,156 @@ +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); + } + +} diff --git a/tests/Feature/V3/Damage/UpdateTest.php b/tests/Feature/V3/Damage/UpdateTest.php new file mode 100644 index 00000000..850ecedf --- /dev/null +++ b/tests/Feature/V3/Damage/UpdateTest.php @@ -0,0 +1,180 @@ +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'], + ]); + } + + +}