write test for azmayesh type entity

This commit is contained in:
2024-11-17 14:13:25 +03:30
parent 117bf0269e
commit a84b1f9469
14 changed files with 588 additions and 9 deletions

View File

@@ -46,9 +46,7 @@ class AzmayeshTypeController extends Controller
public function fields(AzmayeshType $azmayeshType): JsonResponse public function fields(AzmayeshType $azmayeshType): JsonResponse
{ {
$fields = $azmayeshType->azmayeshFields->select('id', 'name', 'unit', 'type', 'option'); return $this->successResponse($azmayeshType->azmayeshFields);
return $this->successResponse($fields);
} }
public function store(StoreRequest $request): JsonResponse public function store(StoreRequest $request): JsonResponse

View File

@@ -12,6 +12,7 @@ class AzmayeshField extends Model
use HasFactory; use HasFactory;
protected $guarded = []; protected $guarded = [];
protected $hidden = ['pivot'];
protected function option(): Attribute protected function option(): Attribute
{ {

View File

@@ -12,6 +12,7 @@ class AzmayeshType extends Model
use HasFactory; use HasFactory;
protected $guarded = []; protected $guarded = [];
protected $hidden = ['pivot'];
public function azmayeshes(): HasMany public function azmayeshes(): HasMany
{ {

View File

@@ -194,6 +194,11 @@ return [
'applicant' => 'متقاضی', 'applicant' => 'متقاضی',
'province_id' => 'استان', 'province_id' => 'استان',
'azmayesh_id' => 'آزمایش', 'azmayesh_id' => 'آزمایش',
'data' => 'داده' 'data' => 'داده',
'fields' => 'فیلد ها',
'fields.*.name' => 'نام فیلد',
'fields.*.unit' => 'واحد فیلد',
'fields.*.type' => 'نوع فیلد',
'fields.*.option' => 'گزینه های فیلد',
], ],
]; ];

View File

@@ -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 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'), ('اداره خمام',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'); ('اداره اصلاندوز',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');

View File

@@ -3,6 +3,7 @@
namespace Tests\Feature\V3\AzmayeshSample; namespace Tests\Feature\V3\AzmayeshSample;
use App\Models\Azmayesh; use App\Models\Azmayesh;
use App\Models\AzmayeshSample;
use App\Models\AzmayeshType; use App\Models\AzmayeshType;
use App\Models\Province; use App\Models\Province;
use App\Models\User; use App\Models\User;
@@ -73,7 +74,9 @@ class StoreTest extends TestCase
$this->assertDatabaseHas('azmayesh_samples', [ $this->assertDatabaseHas('azmayesh_samples', [
'azmayesh_id' => $data['azmayesh_id'], 'azmayesh_id' => $data['azmayesh_id'],
'data' => $data['data'], // 'data' => $data['data'],
]); ]);
$this->assertEquals(AzmayeshSample::first()->data, $data['data']);
} }
} }

View File

@@ -76,7 +76,9 @@ class UpdateTest extends TestCase
$this->assertDatabaseHas('azmayesh_samples', [ $this->assertDatabaseHas('azmayesh_samples', [
'azmayesh_id' => $data['azmayesh_id'], 'azmayesh_id' => $data['azmayesh_id'],
'data' => $data['data'], // 'data' => $data['data'],
]); ]);
$this->assertEquals(AzmayeshSample::first()->data, $data['data']);
} }
} }

View File

@@ -0,0 +1,43 @@
<?php
namespace Tests\Feature\V3\AzmayeshType;
use App\Models\AzmayeshField;
use App\Models\AzmayeshType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class DeleteTest extends TestCase
{
use RefreshDatabase;
/**
* A basic feature test example.
*/
public function test_user_can_delete_azmayesh_type(): void
{
$user = User::factory()->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,
]);
}
}

View File

@@ -24,25 +24,28 @@ class FieldsTest extends TestCase
'name' => $this->faker->name, 'name' => $this->faker->name,
'unit' => $this->faker->name, 'unit' => $this->faker->name,
'type' => $this->faker->name, 'type' => $this->faker->name,
'option' => json_encode([$this->faker->name]),
], ],
[ [
'id' => 2, 'id' => 2,
'name' => $this->faker->name, 'name' => $this->faker->name,
'unit' => $this->faker->name, 'unit' => $this->faker->name,
'type' => $this->faker->name, 'type' => $this->faker->name,
'option' => json_encode([$this->faker->name]),
], ],
[ [
'id' => 3, 'id' => 3,
'name' => $this->faker->name, 'name' => $this->faker->name,
'unit' => $this->faker->name, 'unit' => $this->faker->name,
'type' => $this->faker->name, 'type' => $this->faker->name,
'option' => json_encode([$this->faker->name]),
], ],
) )
->create(); ->create();
$azmayeshType = AzmayeshType::factory()->create(); $azmayeshType = AzmayeshType::factory()->create();
$azmayeshType->azmayeshFields()->attach(1); $azmayeshType->azmayeshFields()->attach([1, 2, 3]);
$user = User::factory()->create(); $user = User::factory()->create();
@@ -56,7 +59,9 @@ class FieldsTest extends TestCase
'name', 'name',
'unit', 'unit',
'type', 'type',
'option' 'option',
"created_at",
"updated_at"
], ],
] ]
]); ]);

View File

@@ -20,13 +20,19 @@ class IndexTest extends TestCase
$user = User::factory()->create(); $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->assertOk();
$response->assertJsonStructure([ $response->assertJsonStructure([
'data' => [ 'data' => [
'*' => [ '*' => [
'id',
'name', 'name',
], ],
] ]

View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Feature\V3\AzmayeshType;
use App\Models\AzmayeshType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ListTest extends TestCase
{
use RefreshDatabase;
/**
* A basic feature test example.
*/
public function test_azmayesh_type_list_returned_successfully(): void
{
AzmayeshType::factory(5)->create();
$user = User::factory()->create();
$response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.list'));
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'*' => [
'name',
],
]
]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Tests\Feature\V3\AzmayeshType;
use App\Models\AzmayeshField;
use App\Models\AzmayeshType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ShowTest extends TestCase
{
use RefreshDatabase;
/**
* A basic feature test example.
*/
public function test_azmayesh_type_details_returned_successfully(): void
{
$azmayeshType = AzmayeshType::factory()->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,
]
]
]
]);
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace Tests\Feature\V3\AzmayeshType;
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_name_attribute_is_required(): void
{
$user = User::factory()->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'],
]);
}
}

View File

@@ -0,0 +1,220 @@
<?php
namespace Tests\Feature\V3\AzmayeshType;
use App\Models\AzmayeshType;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class UpdateTest extends TestCase
{
use RefreshDatabase, WithFaker;
/**
* A basic feature test example.
*/
public function test_name_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([
'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'],
]);
}
}