write test for store road patrol
This commit is contained in:
612
tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php
Normal file
612
tests/Feature/V2/Dashboard/RoadPatrolProject/StoreTest.php
Normal file
@@ -0,0 +1,612 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\V2\Dashboard\RoadPatrolProject;
|
||||
|
||||
use App\Models\City;
|
||||
use App\Models\EdarateOstani;
|
||||
use App\Models\EdarateShahri;
|
||||
use App\Models\InfoItem;
|
||||
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_add_road_patrol_permission(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_user_should_not_have_city_id(): void
|
||||
{
|
||||
$edarateOstani = EdarateOstani::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'edarate_ostani_id' => $edarateOstani->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertStatus(400)
|
||||
->assertExactJson([
|
||||
'message' => 'امکان ثبت فعالیت برای ادارات استانی وجود ندارد!'
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_should_not_have_edarate_shahri_id(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertStatus(400)
|
||||
->assertExactJson([
|
||||
'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!'
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_start_point_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'start_point' => __('validation.required', ['attribute' => 'start point'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_end_point_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'end_point' => __('validation.required', ['attribute' => 'end point'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_distance_should_be_numeric(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'distance' => $this->faker->name
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'distance' => __('validation.numeric', ['attribute' => 'distance'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_officer_name_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'officer_name' => __('validation.required', ['attribute' => 'officer name'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_officer_plaque_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'officer_plaque' => __('validation.required', ['attribute' => 'officer plaque'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_officer_phone_number_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'officer_phone_number' => __('validation.required', ['attribute' => 'officer phone number'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_start_time_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'start_time' => __('validation.required', ['attribute' => 'ساعت شروع گشت'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_start_time_should_match_the_format(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'start_time' => $this->faker->date('d-m-Y-H-i-s')
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'start_time' => __('validation.date_format', ['attribute' => 'ساعت شروع گشت', 'format' => 'Y-m-d H:i'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_end_time_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store');
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'end_time' => __('validation.required', ['attribute' => 'ساعت پایان گشت'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_end_time_should_match_the_format(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'end_time' => $this->faker->date('d-m-Y-H-i-s')
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'end_time' => __('validation.date_format', ['attribute' => 'ساعت پایان گشت', 'format' => 'Y-m-d H:i'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_description_should_be_string(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'description' => $this->faker->numberBetween(1, 10)
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'description' => __('validation.string', ['attribute' => 'توضیحات'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_should_be_array(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $this->faker->numberBetween(1, 10)
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items' => __('validation.array', ['attribute' => 'observed items'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_item_id_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.item_id' => __('validation.required', ['attribute' => 'observed_items.0.item_id'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_sub_item_id_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.sub_item_id' => __('validation.required', ['attribute' => 'observed_items.0.sub_item_id'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_local_name_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.local_name' => __('validation.required', ['attribute' => 'observed_items.0.local_name'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_instant_action_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.instant_action' => __('validation.required', ['attribute' => 'observed_items.0.instant_action'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_start_point_is_required(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.start_point' => __('validation.required', ['attribute' => 'observed_items.0.start_point'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_amount_should_be_numeric(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
['amount' => $this->faker->name]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.amount' => __('validation.numeric', ['attribute' => 'observed_items.0.amount'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_before_image_should_be_image(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
['before_image' => $this->faker->name]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.before_image' => __('validation.image', ['attribute' => 'observed_items.0.before_image'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_observed_items_after_image_should_be_image(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
['after_image' => $this->faker->name]
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.after_image' => __('validation.image', ['attribute' => 'observed_items.0.after_image'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_can_store_a_road_patrol(): void
|
||||
{
|
||||
$city = City::factory()->create();
|
||||
$edarateShari = EdarateShahri::factory()->create();
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'add-road-patrol'
|
||||
]))
|
||||
->create([
|
||||
'city_id' => $city->id,
|
||||
'edarate_shahri_id' => $edarateShari->id
|
||||
]);
|
||||
|
||||
$observed_items = [
|
||||
[
|
||||
'after_image' => $this->faker->name,
|
||||
'item_id' => $this->faker->randomNumber(),
|
||||
'sub_item_id' => $this->faker->randomNumber(),
|
||||
'local_name' => $this->faker->name(),
|
||||
'instant_action' => $this->faker->randomElements([0 , 1]),
|
||||
'start_point' => $this->faker->randomNumber(),
|
||||
]
|
||||
];
|
||||
|
||||
$infoItem = InfoItem::factory()->create([
|
||||
'item' => $observed_items[0]['item_id'],
|
||||
'sub_item' => $observed_items[0]['sub_item_id'],
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson('v2/road_patrols/operator/store', [
|
||||
'start_point' => $this->faker->randomNumber(),
|
||||
'end_point' => $this->faker->randomNumber(),
|
||||
'officer_name' => $this->faker->name(),
|
||||
'officer_plaque' => $this->faker->randomNumber(),
|
||||
'officer_phone_number' => $this->faker->randomNumber(),
|
||||
'observed_items' => $observed_items
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'observed_items.0.after_image' => __('validation.image', ['attribute' => 'observed_items.0.after_image'])
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user