From c9ec75388e2f248b7eaad20559e5f01eeefc2526 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 7 Apr 2024 10:04:19 +0330 Subject: [PATCH] complete the tests of store function --- .../Dashboard/RoadPatrolProject/StoreTest.php | 353 ++++++++++++++++++ 1 file changed, 353 insertions(+) diff --git a/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php b/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php index 984493fc..88ad238b 100644 --- a/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php +++ b/tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php @@ -9,6 +9,7 @@ use App\Models\EdarateShahri; use App\Models\InfoItem; use App\Models\LogList; use App\Models\Permission; +use App\Models\Province; use App\Models\User; use App\Services\NominatimService; use Carbon\Carbon; @@ -712,4 +713,356 @@ class StoreTest extends TestCase 'start_lon' => 12, ]); } + + public function test_end_point_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_end_point' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.end_point" => __('validation.required', ['attribute' => 'end_point']), + ]); + } + + public function test_before_image_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'after_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_image' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.before_image" => __('validation.required', ['attribute' => 'before_image']), + ]); + } + + public function test_after_image_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'before_image' => UploadedFile::fake()->create('image.jpg', 10), + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + 'needs_image' => 1 + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.after_image" => __('validation.required', ['attribute' => 'after_image']), + ]); + } + + public function test_amount_is_required_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertUnprocessable() + ->assertInvalid([ + "observed_items.0.amount" => __('validation.required', ['attribute' => 'amount']), + ]); + } + + public function test_can_store_a_road_patrol_when_instant_action_is_1(): void + { + $city = City::factory()->create(); + $province = Province::factory()->create(); + $edarateShari = EdarateShahri::factory()->create(); + $logList = LogList::factory()->create([ + 'log_unique_code' => 1147 + ]); + + $user = User::factory() + ->has(Permission::factory([ + 'name' => 'add-road-patrol' + ])) + ->create([ + 'city_id' => $city->id, + 'province_id' => $province->id, + 'edarate_shahri_id' => $edarateShari->id + ]); + + $attributes = [ + 'start_point' => '12,12', + 'end_point' => '13,13', + 'officer_name' => $this->faker->name, + 'officer_plaque' => $this->faker->numerify(), + 'officer_phone_number' => $this->faker->numerify("###########"), + 'start_time' => '2016-01-23 11:53', + 'end_time' => '2017-01-23 11:53', + 'observed_items' => [ + [ + 'item_id' => $this->faker->randomNumber(), + 'sub_item_id' => $this->faker->randomNumber(), + 'local_name' => $this->faker->name(), + 'instant_action' => 1, + 'start_point' => '12,12', + 'priority' => $this->faker->numberBetween(1, 3), + 'priority_fa' => $this->faker->name, + 'amount' => $this->faker->numberBetween(1, 10) + ] + ] + ]; + + $infoItem = InfoItem::factory()->create([ + 'item' => $attributes['observed_items'][0]['item_id'], + 'sub_item' => $attributes['observed_items'][0]['sub_item_id'], + ]); + + $mock = $this->mock(NominatimService::class, function (MockInterface $mock) { + $mock->shouldReceive('get_way_id_from_nominatim')->andReturn(0); + }); + + Sms::shouldReceive('sendSms'); + + $response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', $attributes); + + $response->assertOk(); + + $this->assertDatabaseHas('road_patrols', [ + 'start_lat' => 12, + 'start_lon' => 12, + 'end_lat' => 13, + 'end_lon' => 13, + 'officer_name' => $attributes['officer_name'], + 'officer_phone_number' => $attributes['officer_phone_number'], + 'officer_plaque' => $attributes['officer_plaque'], + 'operator_id' => $user->id, + 'operator_name' => $user->name, + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'edare_id' => $user->edarate_shahri_id, + 'edare_name' => $user->edarate_shahri_name, + 'start_way_id' => 0, + 'end_way_id' => 0, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'start_time' => $attributes['start_time'], + 'end_time' => $attributes['end_time'], + 'description' => null, + ]); + + $this->assertDatabaseHas('observed_items', [ + 'item_id' => $infoItem->item, + 'item_name' => $infoItem->item_str, + 'sub_item_id' => $infoItem->sub_item, + 'sub_item_name' => $infoItem->sub_item_str, + 'local_name' => $attributes['observed_items'][0]['local_name'], + 'description' => null, + 'start_lat' => 12, + 'start_lon' => 12, + ]); + + $this->assertDatabaseHas('road_items_projects', [ + 'start_lat' => 12, + 'start_lng' => 12, + 'end_lat' => 13, + 'end_lng' => 13, + 'item' => $infoItem->item, + 'item_fa' => $infoItem->item_fa, + 'sub_item' => $infoItem->sub_item, + 'sub_item_fa' => $infoItem->sub_item_fa, + 'sub_item_data' => $attributes['observed_items'][0]['amount'], + 'province_id' => $user->province_id, + 'province_fa' => $user->province_fa, + 'city_id' => $user->city_id, + 'city_fa' => $user->city_fa, + 'user_name' => $user->name, + 'start_way_id' => 0, + 'end_way_id' => 0, + 'info_id' => $infoItem->id, + 'status' => 0, + 'status_fa' => 'در حال بررسی', + 'edarat_id' => $user->edarate_shahri_id, + 'edarat_type' => EdarateShahri::class, + 'is_new' => 1, + ]); + } }