From a84b1f946921dc994ca96fc64e9e8e7f7f86d78f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 17 Nov 2024 14:13:25 +0330 Subject: [PATCH] write test for azmayesh type entity --- .../V3/Azmayesh/AzmayeshTypeController.php | 4 +- app/Models/AzmayeshField.php | 1 + app/Models/AzmayeshType.php | 1 + resources/lang/fa/validation.php | 7 +- storage/sql-files/edarate_shahri.sql | 1 + tests/Feature/V3/AzmayeshSample/StoreTest.php | 5 +- .../Feature/V3/AzmayeshSample/UpdateTest.php | 4 +- tests/Feature/V3/AzmayeshType/DeleteTest.php | 43 ++++ tests/Feature/V3/AzmayeshType/FieldsTest.php | 9 +- tests/Feature/V3/AzmayeshType/IndexTest.php | 8 +- tests/Feature/V3/AzmayeshType/ListTest.php | 35 +++ tests/Feature/V3/AzmayeshType/ShowTest.php | 51 ++++ tests/Feature/V3/AzmayeshType/StoreTest.php | 208 +++++++++++++++++ tests/Feature/V3/AzmayeshType/UpdateTest.php | 220 ++++++++++++++++++ 14 files changed, 588 insertions(+), 9 deletions(-) create mode 100644 tests/Feature/V3/AzmayeshType/DeleteTest.php create mode 100644 tests/Feature/V3/AzmayeshType/ListTest.php create mode 100644 tests/Feature/V3/AzmayeshType/ShowTest.php create mode 100644 tests/Feature/V3/AzmayeshType/StoreTest.php create mode 100644 tests/Feature/V3/AzmayeshType/UpdateTest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index f521c273..d5445f45 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -46,9 +46,7 @@ class AzmayeshTypeController extends Controller public function fields(AzmayeshType $azmayeshType): JsonResponse { - $fields = $azmayeshType->azmayeshFields->select('id', 'name', 'unit', 'type', 'option'); - - return $this->successResponse($fields); + return $this->successResponse($azmayeshType->azmayeshFields); } public function store(StoreRequest $request): JsonResponse diff --git a/app/Models/AzmayeshField.php b/app/Models/AzmayeshField.php index efb49a4a..186d1ba4 100644 --- a/app/Models/AzmayeshField.php +++ b/app/Models/AzmayeshField.php @@ -12,6 +12,7 @@ class AzmayeshField extends Model use HasFactory; protected $guarded = []; + protected $hidden = ['pivot']; protected function option(): Attribute { diff --git a/app/Models/AzmayeshType.php b/app/Models/AzmayeshType.php index 0a5db09d..6ae6e396 100644 --- a/app/Models/AzmayeshType.php +++ b/app/Models/AzmayeshType.php @@ -12,6 +12,7 @@ class AzmayeshType extends Model use HasFactory; protected $guarded = []; + protected $hidden = ['pivot']; public function azmayeshes(): HasMany { diff --git a/resources/lang/fa/validation.php b/resources/lang/fa/validation.php index 377a776d..f35716e0 100644 --- a/resources/lang/fa/validation.php +++ b/resources/lang/fa/validation.php @@ -194,6 +194,11 @@ return [ 'applicant' => 'متقاضی', 'province_id' => 'استان', 'azmayesh_id' => 'آزمایش', - 'data' => 'داده' + 'data' => 'داده', + 'fields' => 'فیلد ها', + 'fields.*.name' => 'نام فیلد', + 'fields.*.unit' => 'واحد فیلد', + 'fields.*.type' => 'نوع فیلد', + 'fields.*.option' => 'گزینه های فیلد', ], ]; diff --git a/storage/sql-files/edarate_shahri.sql b/storage/sql-files/edarate_shahri.sql index b3a50c74..b61474b3 100644 --- a/storage/sql-files/edarate_shahri.sql +++ b/storage/sql-files/edarate_shahri.sql @@ -519,3 +519,4 @@ INSERT INTO edarate_shahri (name_fa,province_id,created_at,updated_at) VALUES INSERT INTO edarate_shahri (name_fa,province_id,created_at,updated_at) VALUES ('اداره خمام',28,'2023-06-17 08:43:14','2023-06-17 08:43:14'), ('اداره اصلاندوز',3,'2023-08-15 09:33:55','2023-08-15 09:33:55'); + ('اداره گالیکش',27,'2024-11-16 14:33:55','2024-11-16 14:33:55'); diff --git a/tests/Feature/V3/AzmayeshSample/StoreTest.php b/tests/Feature/V3/AzmayeshSample/StoreTest.php index 28a6e1e0..25772e48 100644 --- a/tests/Feature/V3/AzmayeshSample/StoreTest.php +++ b/tests/Feature/V3/AzmayeshSample/StoreTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\V3\AzmayeshSample; use App\Models\Azmayesh; +use App\Models\AzmayeshSample; use App\Models\AzmayeshType; use App\Models\Province; use App\Models\User; @@ -73,7 +74,9 @@ class StoreTest extends TestCase $this->assertDatabaseHas('azmayesh_samples', [ 'azmayesh_id' => $data['azmayesh_id'], - 'data' => $data['data'], +// 'data' => $data['data'], ]); + + $this->assertEquals(AzmayeshSample::first()->data, $data['data']); } } diff --git a/tests/Feature/V3/AzmayeshSample/UpdateTest.php b/tests/Feature/V3/AzmayeshSample/UpdateTest.php index c79fef01..1ec0f42e 100644 --- a/tests/Feature/V3/AzmayeshSample/UpdateTest.php +++ b/tests/Feature/V3/AzmayeshSample/UpdateTest.php @@ -76,7 +76,9 @@ class UpdateTest extends TestCase $this->assertDatabaseHas('azmayesh_samples', [ 'azmayesh_id' => $data['azmayesh_id'], - 'data' => $data['data'], +// 'data' => $data['data'], ]); + + $this->assertEquals(AzmayeshSample::first()->data, $data['data']); } } diff --git a/tests/Feature/V3/AzmayeshType/DeleteTest.php b/tests/Feature/V3/AzmayeshType/DeleteTest.php new file mode 100644 index 00000000..05b49035 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/DeleteTest.php @@ -0,0 +1,43 @@ +create(); + + $azmayeshType = AzmayeshType::factory()->create(); + $azmayeshField = AzmayeshField::factory()->create(); + + $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.delete', [$azmayeshType->id])); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseMissing('azmayesh_types', [ + 'name' => $azmayeshType->name, + ]); + + $this->assertDatabaseMissing('azmayesh_field_azmayesh_type', [ + 'azmayesh_type_id' => $azmayeshType->id, + 'azmayesh_field_id' => $azmayeshField->id, + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/FieldsTest.php b/tests/Feature/V3/AzmayeshType/FieldsTest.php index c6e5f7c4..7e4832ff 100644 --- a/tests/Feature/V3/AzmayeshType/FieldsTest.php +++ b/tests/Feature/V3/AzmayeshType/FieldsTest.php @@ -24,25 +24,28 @@ class FieldsTest extends TestCase 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], [ 'id' => 2, 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], [ 'id' => 3, 'name' => $this->faker->name, 'unit' => $this->faker->name, 'type' => $this->faker->name, + 'option' => json_encode([$this->faker->name]), ], ) ->create(); $azmayeshType = AzmayeshType::factory()->create(); - $azmayeshType->azmayeshFields()->attach(1); + $azmayeshType->azmayeshFields()->attach([1, 2, 3]); $user = User::factory()->create(); @@ -56,7 +59,9 @@ class FieldsTest extends TestCase 'name', 'unit', 'type', - 'option' + 'option', + "created_at", + "updated_at" ], ] ]); diff --git a/tests/Feature/V3/AzmayeshType/IndexTest.php b/tests/Feature/V3/AzmayeshType/IndexTest.php index e96695f2..d78ebe24 100644 --- a/tests/Feature/V3/AzmayeshType/IndexTest.php +++ b/tests/Feature/V3/AzmayeshType/IndexTest.php @@ -20,13 +20,19 @@ class IndexTest extends TestCase $user = User::factory()->create(); - $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index')); + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.index', [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([]), + ])); $response->assertOk(); $response->assertJsonStructure([ 'data' => [ '*' => [ + 'id', 'name', ], ] diff --git a/tests/Feature/V3/AzmayeshType/ListTest.php b/tests/Feature/V3/AzmayeshType/ListTest.php new file mode 100644 index 00000000..5aed890b --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/ListTest.php @@ -0,0 +1,35 @@ +create(); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.list')); + + $response->assertOk(); + + $response->assertJsonStructure([ + 'data' => [ + '*' => [ + 'name', + ], + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/ShowTest.php b/tests/Feature/V3/AzmayeshType/ShowTest.php new file mode 100644 index 00000000..f682f9e8 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/ShowTest.php @@ -0,0 +1,51 @@ +create(); + $azmayeshField = AzmayeshField::factory()->create(); + + $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]); + + $user = User::factory()->create(); + + $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.show', [$azmayeshType->id])); + + $response->assertOk(); + + $response->assertExactJson([ + 'data' => [ + "id" => $azmayeshType->id, + "name" => $azmayeshType->name, + "created_at" => $azmayeshType->created_at, + "updated_at" => $azmayeshType->updated_at, + "azmayesh_fields" => [ + [ + "id" => $azmayeshField->id, + "name" => $azmayeshField->name, + "unit" => $azmayeshField->unit, + "type" => $azmayeshField->type, + "option" => $azmayeshField->option, + "created_at" => $azmayeshField->created_at, + "updated_at" => $azmayeshField->updated_at, + ] + ] + ] + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/StoreTest.php b/tests/Feature/V3/AzmayeshType/StoreTest.php new file mode 100644 index 00000000..18cee337 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/StoreTest.php @@ -0,0 +1,208 @@ +create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.required', ['attribute' => "نام"]) + ]); + } + + public function test_name_attribute_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.string', ['attribute' => "نام"]) + ]); + } + + public function test_fields_attribute_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store')); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.required', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_fields_attribute_should_be_array(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.array', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_field_name_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.required', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_name_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['name' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.string', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_unit_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['unit' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.unit' => __('validation.string', ['attribute' => 'واحد فیلد']) + ]); + } + + public function test_field_type_is_required(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.required', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_type_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['type' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.string', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_option_should_be_string(): void + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), [ + 'fields' => [ + ['option' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.option' => __('validation.string', ['attribute' => 'گزینه های فیلد']) + ]); + } + + public function test_user_can_store_fields_for_an_azmayesh_type(): void + { + $user = User::factory()->create(); + + $data = [ + 'name' => $this->faker->name, + 'fields' => [ + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + 'option' => json_encode(['test']), + ], + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + ], + ] + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.store'), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][0]['name'], + 'type' => $data['fields'][0]['type'], + 'unit' => $data['fields'][0]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][1]['name'], + 'type' => $data['fields'][1]['type'], + 'unit' => $data['fields'][1]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_types', [ + 'name' => $data['name'], + ]); + } +} diff --git a/tests/Feature/V3/AzmayeshType/UpdateTest.php b/tests/Feature/V3/AzmayeshType/UpdateTest.php new file mode 100644 index 00000000..7cc6ba40 --- /dev/null +++ b/tests/Feature/V3/AzmayeshType/UpdateTest.php @@ -0,0 +1,220 @@ +create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.required', ['attribute' => "نام"]) + ]); + } + + public function test_name_attribute_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'name' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'name' => __('validation.string', ['attribute' => "نام"]) + ]); + } + + public function test_fields_attribute_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id])); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.required', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_fields_attribute_should_be_array(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => $this->faker->numberBetween(1, 10) + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields' => __('validation.array', ['attribute' => 'فیلد ها']) + ]); + } + + public function test_field_name_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.required', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_name_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['name' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.name' => __('validation.string', ['attribute' => 'نام فیلد']) + ]); + } + + public function test_field_unit_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['unit' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.unit' => __('validation.string', ['attribute' => 'واحد فیلد']) + ]); + } + + public function test_field_type_is_required(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + [] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.required', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_type_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['type' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.type' => __('validation.string', ['attribute' => 'نوع فیلد']) + ]); + } + + public function test_field_option_should_be_string(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), [ + 'fields' => [ + ['option' => $this->faker->numberBetween(1, 10)] + ] + ]); + + $response->assertUnprocessable() + ->assertInvalid([ + 'fields.0.option' => __('validation.string', ['attribute' => 'گزینه های فیلد']) + ]); + } + + public function test_user_can_update_fields_for_an_azmayesh_type(): void + { + $user = User::factory()->create(); + $azmayeshType = AzmayeshType::factory()->create(); + + $data = [ + 'name' => $this->faker->name, + 'fields' => [ + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + 'option' => json_encode(['test']), + ], + [ + 'name' => $this->faker->name, + 'type' => $this->faker->randomElement(['input', 'select', 'date']), + 'unit' => $this->faker->name, + ], + ] + ]; + + $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.update', [$azmayeshType->id]), $data); + + $response->assertOk() + ->assertExactJson([ + 'message' => __('messages.successful') + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][0]['name'], + 'type' => $data['fields'][0]['type'], + 'unit' => $data['fields'][0]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_fields', [ + 'name' => $data['fields'][1]['name'], + 'type' => $data['fields'][1]['type'], + 'unit' => $data['fields'][1]['unit'], + ]); + + $this->assertDatabaseHas('azmayesh_types', [ + 'name' => $data['name'], + ]); + } +}