Add form request validation tests for various client and admin requests
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Requests\Client;
|
||||
|
||||
use App\Http\Requests\Client\ProfileUpdateRequest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\Concerns\ValidatesFormRequests;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProfileUpdateRequestTest extends TestCase
|
||||
{
|
||||
use ValidatesFormRequests;
|
||||
|
||||
protected function valid(array $overrides = []): array
|
||||
{
|
||||
return array_merge(['fullname' => 'علی رضایی', 'national_code' => '0012345678'], $overrides);
|
||||
}
|
||||
|
||||
public function test_passes_with_required_fields_only(): void
|
||||
{
|
||||
$this->assertPasses(ProfileUpdateRequest::class, $this->valid());
|
||||
}
|
||||
|
||||
public function test_passes_with_all_optional_fields_and_png_avatar(): void
|
||||
{
|
||||
$this->assertPasses(ProfileUpdateRequest::class, $this->valid([
|
||||
'birthdate' => '1990-01-01',
|
||||
'email' => 'a@b.com',
|
||||
'province' => 'تهران',
|
||||
'city' => 'تهران',
|
||||
'address' => 'خیابان',
|
||||
]), ['avatar' => UploadedFile::fake()->image('a.png')]);
|
||||
}
|
||||
|
||||
public function test_fails_without_fullname_or_national_code(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(ProfileUpdateRequest::class, []);
|
||||
|
||||
$this->assertArrayHasKey('fullname', $errors);
|
||||
$this->assertArrayHasKey('national_code', $errors);
|
||||
}
|
||||
|
||||
public function test_fails_with_invalid_birthdate_or_email(): void
|
||||
{
|
||||
$this->assertFailsOn('birthdate', ProfileUpdateRequest::class, $this->valid(['birthdate' => 'not-a-date']));
|
||||
$this->assertFailsOn('email', ProfileUpdateRequest::class, $this->valid(['email' => 'not-an-email']));
|
||||
}
|
||||
|
||||
public function test_fails_when_national_code_or_address_too_long(): void
|
||||
{
|
||||
$this->assertFailsOn('national_code', ProfileUpdateRequest::class, $this->valid(['national_code' => str_repeat('1', 21)]));
|
||||
$this->assertFailsOn('address', ProfileUpdateRequest::class, $this->valid(['address' => str_repeat('a', 501)]));
|
||||
}
|
||||
|
||||
public function test_fails_with_non_image_or_disallowed_avatar_type(): void
|
||||
{
|
||||
$this->assertFailsOn('avatar', ProfileUpdateRequest::class, $this->valid(), ['avatar' => UploadedFile::fake()->create('a.pdf', 10, 'application/pdf')]);
|
||||
$this->assertFailsOn('avatar', ProfileUpdateRequest::class, $this->valid(), ['avatar' => UploadedFile::fake()->image('a.gif')]);
|
||||
}
|
||||
|
||||
public function test_fails_with_avatar_over_ten_megabytes(): void
|
||||
{
|
||||
$this->assertFailsOn('avatar', ProfileUpdateRequest::class, $this->valid(), ['avatar' => UploadedFile::fake()->image('a.jpg')->size(10241)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user