improve 4 test

This commit is contained in:
2025-05-11 11:10:40 +03:30
parent 0a2b324041
commit f2fda29041
4 changed files with 196 additions and 197 deletions

View File

@@ -1,129 +1,114 @@
<?php
//
//namespace Tests\Feature\Call;
//
//use App\Models\Call;
//use App\Models\Permission;
//use App\Models\User;
//use Illuminate\Foundation\Testing\RefreshDatabase;
//use Illuminate\Foundation\Testing\WithFaker;
//use Illuminate\Support\Facades\Config;
//use Illuminate\Testing\Fluent\AssertableJson;
//use Tests\TestCase;
//
//class ListTest extends TestCase
//{
// use RefreshDatabase, WithFaker;
//
// public function test_unauthenticated_user_cannot_access(): void
// {
// $response = $this->getJson(route('calls.list'));
// $response->assertStatus(401);
// }
//
// public function test_user_without_permission_cannot_access(): void
// {
// $user = User::factory()->create();
// $this->actingAs($user);
//
// $response = $this->getJson(route('calls.list'));
// $response->assertForbidden();
// }
//
// public function test_phone_number_must_be_string(): void
// {
// $user = User::factory()
// ->has(Permission::factory([
// 'name' => 'manage_calls',
// 'name_fa' => 'مدیریت تماس‌ها',
// ]))
// ->create();
//
// // 🟢 احراز هویت کاربر قبل از ارسال درخواست
// $this->actingAs($user);
//
// $response = $this->getJson(route('calls.list', [
// 'phone_number' => $this->faker->in,
// ]));
//
// $response->assertUnprocessable()
// ->assertInvalid([
// 'phone_number' => __('validation.string', ['attribute' => 'شماره تلفن']),
// ]);
// }
//
//
// public function test_size_must_be_integer(): void
// {
// $user = $this->createUserWithPermission();
// $this->actingAs($user);
//
// $response = $this->getJson(route('calls.list', [
// 'size' => 'not-an-integer',
// ]));
//
// $response->assertUnprocessable()
// ->assertInvalid([
// 'size' => __('validation.integer', ['attribute' => 'حداکثر تعداد']),
// ]);
// }
//
// public function test_calls_are_returned_with_limit(): void
// {
// $user = $this->createUserWithPermission();
// $this->actingAs($user);
//
// Call::factory()->count(8)->create();
//
// $response = $this->getJson(route('calls.list', ['size' => 5]));
//
// $response->assertOk()
// ->assertJsonCount(5, 'data')
// ->assertJson(fn (AssertableJson $json) =>
// $json->has('message')
// ->has('data')
// ->has('data.0', fn (AssertableJson $json) =>
// $json->hasAll([
// 'id',
// 'operator_id',
// 'operator_full_name',
// 'operator_username',
// 'telephone_id',
// 'caller_phone_number',
// 'description',
// 'category_id',
// 'category_name',
// 'subcategory_id',
// 'subcategory_name',
// 'created_at',
// ])
// )
// );
// }
//
// public function test_calls_are_filtered_by_phone_number(): void
// {
// $user = $this->createUserWithPermission();
// $this->actingAs($user);
//
// $targetPhone = '09' . $this->faker->numberBetween(100000000, 999999999);
// Call::factory()->count(4)->create(['caller_phone_number' => $targetPhone]);
// Call::factory()->count(2)->create(); // other numbers
//
// $response = $this->getJson(route('calls.list', ['phone_number' => $targetPhone]));
//
// $response->assertOk()
// ->assertJsonCount(4, 'data')
// ->assertJsonFragment(['caller_phone_number' => $targetPhone]);
// }
//
// private function createUserWithPermission(): User
// {
// return User::factory()
// ->has(Permission::factory([
// 'name' => 'manage_calls',
// 'name_fa' => 'مدیریت تماس‌ها',
// ]))
// ->create();
// }
//}
namespace Tests\Feature\Call;
use App\Models\Call;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class ListTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function test_unauthenticated_user_cannot_access(): void
{
$response = $this->getJson(route('calls.list'));
$response->assertStatus(401);
}
public function test_user_without_permission_cannot_access(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->getJson(route('calls.list'));
$response->assertForbidden();
}
public function test_size_must_be_integer(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))->create();
$this->actingAs($user);
$response = $this->getJson(route('calls.list', [
'size' => $this->faker->word,
]));
$response->assertUnprocessable()
->assertInvalid([
'size' => __('validation.integer', ['attribute' => 'حداکثر تعداد']),
]);
}
public function test_calls_are_limited_by_size_parameter(): void
{
$user = User::factory()
->has(Permission::factory()->state([
'id' => 3,
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))
->create();
Call::factory()->count(5)->create();
$response = $this->actingAs($user)->getJson(route('calls.list', [
'size' => $this->faker->numberBetween(1, 4),
]));
$response->assertOk()
->assertJson(fn (AssertableJson $json) =>
$json->has('data')
->has('data.0', fn (AssertableJson $json) => $json->hasAll([
'id',
'operator_id',
'operator_full_name',
'operator_username',
'telephone_id',
'caller_phone_number',
'description',
'category_id',
'category_name',
'subcategory_id',
'subcategory_name',
'created_at',
]))
);
}
public function test_calls_are_filtered_by_phone_number(): void
{
$user = User::factory()
->has(Permission::factory([
'name' => 'manage_calls',
'name_fa' => 'مدیریت تماس‌ها',
]))->create();
$targetPhone = '09' . $this->faker->numberBetween(100000000, 999999999);
Call::factory()->count(4)->create([
'caller_phone_number' => $targetPhone,
]);
Call::factory()->count(2)->create();
$response = $this->actingAs($user)->getJson(route('calls.list', ['phone_number' => $targetPhone]));
$response->assertOk()
->assertJsonCount(4, 'data')
->assertJsonFragment(['caller_phone_number' => $targetPhone]);
}
}