From fbb4db5e608647a49f9802fd1af09b9a4bfafffd Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Nov 2024 10:52:57 +0330 Subject: [PATCH 1/3] create panel for azmayesh type crud --- .../V3/Azmayesh/AzmayeshSampleController.php | 6 +- .../V3/Azmayesh/AzmayeshTypeController.php | 69 +++++++++++++++++++ .../Requests/V3/AzmayeshType/StoreRequest.php | 29 ++++++++ .../V3/AzmayeshType/UpdateRequest.php | 29 ++++++++ routes/v3.php | 4 ++ 5 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 app/Http/Requests/V3/AzmayeshType/StoreRequest.php create mode 100644 app/Http/Requests/V3/AzmayeshType/UpdateRequest.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php index 2dee40af..9aa4303d 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshSampleController.php @@ -59,7 +59,7 @@ class AzmayeshSampleController extends Controller /** * Display the specified resource. */ - public function show(AzmayeshSample $azmayeshSample) + public function show(AzmayeshSample $azmayeshSample): JsonResponse { return $this->successResponse($azmayeshSample); } @@ -67,7 +67,7 @@ class AzmayeshSampleController extends Controller /** * Update the specified resource in storage. */ - public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample) + public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample): JsonResponse { $azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id); @@ -83,7 +83,7 @@ class AzmayeshSampleController extends Controller /** * Remove the specified resource from storage. */ - public function destroy(AzmayeshSample $azmayeshSample) + public function destroy(AzmayeshSample $azmayeshSample): JsonResponse { $azmayeshSample->delete(); diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index 1f9f0b8d..87d7af75 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -3,10 +3,14 @@ namespace App\Http\Controllers\V3\Azmayesh; use App\Http\Controllers\Controller; +use App\Http\Requests\V3\AzmayeshType\StoreRequest; +use App\Http\Requests\V3\AzmayeshType\UpdateRequest; use App\Http\Traits\ApiResponse; +use App\Models\AzmayeshField; use App\Models\AzmayeshType; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class AzmayeshTypeController extends Controller { @@ -25,4 +29,69 @@ class AzmayeshTypeController extends Controller return $this->successResponse($fields); } + + public function store(StoreRequest $request): JsonResponse + { + DB::transaction(function () use ($request) + { + $azmayeshType = AzmayeshType::query()->create([ + 'name' => $request->name + ]); + + $fieldsID = []; + + foreach ($request->fields as $field) + { + $azmayeshField = AzmayeshField::query()->firstOrCreate($field, $field); + $fieldsID[] = $azmayeshField->id; + } + + $azmayeshType->azmayeshFields()->attach($fieldsID); + }); + + return $this->successResponse(); + } + + /** + * Display the specified resource. + */ + public function show(AzmayeshType $azmayeshType): JsonResponse + { + return $this->successResponse($azmayeshType->load(['azmayeshFields'])); + } + + /** + * Update the specified resource in storage. + */ + public function update(UpdateRequest $request, AzmayeshType $azmayeshType): JsonResponse + { + DB::transaction(function () use ($request, $azmayeshType) + { + $azmayeshType->update([ + 'name' => $request->name + ]); + + $fieldsID = []; + + foreach ($request->fields as $field) + { + $azmayeshField = AzmayeshField::query()->updateOrCreate($field, $field); + $fieldsID[] = $azmayeshField->id; + } + + $azmayeshType->azmayeshFields()->sync($fieldsID); + }); + + return $this->successResponse(); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(AzmayeshType $azmayeshType): JsonResponse + { + $azmayeshType->delete(); + + return $this->successResponse(); + } } diff --git a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php new file mode 100644 index 00000000..e575eb20 --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string', + 'fields' => 'required|array', + ]; + } +} diff --git a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php new file mode 100644 index 00000000..deb49567 --- /dev/null +++ b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => 'required|string', + 'fields' => 'required|array', + ]; + } +} diff --git a/routes/v3.php b/routes/v3.php index 7dc98798..4a75b278 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -42,6 +42,10 @@ Route::prefix('azmayesh_types') ->group(function () { Route::get('/', 'index')->name('index'); Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); + Route::get('/{azmayeshType}', 'show')->name('show'); + Route::post('/store', 'store')->name('store'); + Route::post('/update/{azmayeshType}', 'update')->name('update'); + Route::post('/delete/{azmayeshType}', 'destroy')->name('delete'); }); Route::prefix('azmayesh_samples') From 117bf0269e24c7923c01614d2a3dd019c460ca96 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sat, 9 Nov 2024 11:36:16 +0330 Subject: [PATCH 2/3] add nested array validation to form requests --- .../V3/Azmayesh/AzmayeshTypeController.php | 23 ++++++++++++++++++- .../Requests/V3/AzmayeshType/StoreRequest.php | 4 ++++ .../V3/AzmayeshType/UpdateRequest.php | 4 ++++ ...ate_azmayesh_field_azmayesh_type_table.php | 4 ++-- routes/v3.php | 1 + 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index 87d7af75..f521c273 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\V3\Azmayesh; +use App\Facades\DataTable\DataTableFacade; use App\Http\Controllers\Controller; use App\Http\Requests\V3\AzmayeshType\StoreRequest; use App\Http\Requests\V3\AzmayeshType\UpdateRequest; @@ -16,7 +17,27 @@ class AzmayeshTypeController extends Controller { use ApiResponse; - public function index(): JsonResponse + public function index(Request $request): JsonResponse + { + $columns = array( + 'id', 'name' + ); + + $allowedFilters = $columns; + $allowedSortings = $columns; + + $query = AzmayeshType::query(); + + $data = DataTableFacade::run( + $query, + $request, + allowedFilters: $allowedFilters, + allowedSortings: $allowedSortings); + + return response()->json($data); + } + + public function list(): JsonResponse { $allTypes = AzmayeshType::all(); diff --git a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php index e575eb20..2c22ffc1 100644 --- a/app/Http/Requests/V3/AzmayeshType/StoreRequest.php +++ b/app/Http/Requests/V3/AzmayeshType/StoreRequest.php @@ -24,6 +24,10 @@ class StoreRequest extends FormRequest return [ 'name' => 'required|string', 'fields' => 'required|array', + 'fields.*.name' => 'required|string', + 'fields.*.unit' => 'string', + 'fields.*.type' => 'required|string', + 'fields.*.option' => 'string', ]; } } diff --git a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php index deb49567..b7af342e 100644 --- a/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php +++ b/app/Http/Requests/V3/AzmayeshType/UpdateRequest.php @@ -24,6 +24,10 @@ class UpdateRequest extends FormRequest return [ 'name' => 'required|string', 'fields' => 'required|array', + 'fields.*.name' => 'required|string', + 'fields.*.unit' => 'string', + 'fields.*.type' => 'required|string', + 'fields.*.option' => 'string', ]; } } diff --git a/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php index 5507811c..8d1505ac 100644 --- a/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php +++ b/database/migrations/2024_10_22_152052_create_azmayesh_field_azmayesh_type_table.php @@ -13,8 +13,8 @@ return new class extends Migration { Schema::create('azmayesh_field_azmayesh_type', function (Blueprint $table) { $table->id(); - $table->foreignId('azmayesh_field_id')->constrained('azmayesh_fields'); - $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types'); + $table->foreignId('azmayesh_field_id')->constrained('azmayesh_fields')->cascadeOnDelete(); + $table->foreignId('azmayesh_type_id')->constrained('azmayesh_types')->cascadeOnDelete(); $table->timestamps(); }); } diff --git a/routes/v3.php b/routes/v3.php index 4a75b278..8a43ad37 100644 --- a/routes/v3.php +++ b/routes/v3.php @@ -41,6 +41,7 @@ Route::prefix('azmayesh_types') ->controller(AzmayeshTypeController::class) ->group(function () { Route::get('/', 'index')->name('index'); + Route::get('/list', 'list')->name('list'); Route::get('/fields/{azmayeshType}', 'fields')->name('fields'); Route::get('/{azmayeshType}', 'show')->name('show'); Route::post('/store', 'store')->name('store'); From a84b1f946921dc994ca96fc64e9e8e7f7f86d78f Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 17 Nov 2024 14:13:25 +0330 Subject: [PATCH 3/3] 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'], + ]); + } +}