Add form request validation tests for various client and admin requests

This commit is contained in:
2026-09-21 05:24:50 +03:30
parent f77ff527e7
commit e987a528ef
21 changed files with 1061 additions and 23 deletions
@@ -0,0 +1,55 @@
<?php
namespace Tests\Feature\Requests\Client\Auth;
use App\Http\Requests\Client\Auth\UserRegistrationRequest;
use Tests\Concerns\ValidatesFormRequests;
use Tests\TestCase;
class UserRegistrationRequestTest extends TestCase
{
use ValidatesFormRequests;
protected function valid(array $overrides = []): array
{
return array_merge(['phone' => '09120000000', 'national_code' => '0012345678'], $overrides);
}
public function test_passes_with_phone_and_national_code(): void
{
$this->assertPasses(UserRegistrationRequest::class, $this->valid());
}
public function test_passes_with_all_optional_fields(): void
{
$this->assertPasses(UserRegistrationRequest::class, $this->valid([
'fullname' => 'f', 'specialty' => 's', 'clinic_name' => 'c', 'clinic_phone' => '021', 'province' => 'p', 'city' => 'c', 'address' => 'a', 'level' => 'دانپزشک',
]));
}
public function test_accepts_international_and_persian_digit_phones(): void
{
$this->assertPasses(UserRegistrationRequest::class, $this->valid(['phone' => '+989120000000']));
$this->assertPasses(UserRegistrationRequest::class, $this->valid(['phone' => '۰۹۱۲۰۰۰۰۰۰۰']));
}
public function test_fails_without_phone_or_national_code(): void
{
$errors = $this->formRequestErrors(UserRegistrationRequest::class, []);
$this->assertArrayHasKey('phone', $errors);
$this->assertArrayHasKey('national_code', $errors);
}
public function test_fails_with_invalid_phone(): void
{
$this->assertFailsOn('phone', UserRegistrationRequest::class, $this->valid(['phone' => 'abc']));
$this->assertFailsOn('phone', UserRegistrationRequest::class, $this->valid(['phone' => '123']));
$this->assertFailsOn('phone', UserRegistrationRequest::class, $this->valid(['phone' => '+9999120000000']));
}
public function test_fails_when_level_exceeds_limit(): void
{
$this->assertFailsOn('level', UserRegistrationRequest::class, $this->valid(['level' => str_repeat('a', 51)]));
}
}
@@ -0,0 +1,62 @@
<?php
namespace Tests\Feature\Requests\Client\Auth;
use App\Http\Requests\Client\Auth\VerifyOtpRequest;
use App\Models\OtpCode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Concerns\ValidatesFormRequests;
use Tests\TestCase;
class VerifyOtpRequestTest extends TestCase
{
use RefreshDatabase, ValidatesFormRequests;
protected function setUp(): void
{
parent::setUp();
session(['login_phone' => '09120000000']);
}
public function test_passes_with_valid_code_for_session_phone(): void
{
OtpCode::factory()->create(['phone' => '09120000000', 'code' => '1234']);
$this->assertPasses(VerifyOtpRequest::class, ['otp_code' => '1234']);
}
public function test_fails_without_code(): void
{
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, []);
}
public function test_fails_when_code_is_not_four_digits(): void
{
OtpCode::factory()->create(['phone' => '09120000000', 'code' => '123']);
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, ['otp_code' => '123']);
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, ['otp_code' => 'abcd']);
}
public function test_fails_with_wrong_code(): void
{
OtpCode::factory()->create(['phone' => '09120000000', 'code' => '1234']);
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, ['otp_code' => '0000']);
}
public function test_fails_with_expired_code(): void
{
OtpCode::factory()->expired()->create(['phone' => '09120000000', 'code' => '1234']);
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, ['otp_code' => '1234']);
}
public function test_fails_when_code_belongs_to_another_phone(): void
{
OtpCode::factory()->create(['phone' => '09130000000', 'code' => '1234']);
$this->assertFailsOn('otp_code', VerifyOtpRequest::class, ['otp_code' => '1234']);
}
}