Add form request validation tests for various client and admin requests
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Requests\Admin;
|
||||
|
||||
use App\Enums\PetGenderEnum;
|
||||
use App\Enums\PetTypeEnum;
|
||||
use App\Http\Requests\Admin\PetRequest;
|
||||
use Tests\Concerns\ValidatesFormRequests;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PetRequestTest extends TestCase
|
||||
{
|
||||
use ValidatesFormRequests;
|
||||
|
||||
protected function valid(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'name' => 'Rex',
|
||||
'type' => PetTypeEnum::Dog->value,
|
||||
'gender' => PetGenderEnum::Male->value,
|
||||
'breed' => 'ژرمن',
|
||||
'weight' => '20',
|
||||
'sterilized' => true,
|
||||
'birthday' => '2020-01-01',
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function test_passes_with_valid_payload(): void
|
||||
{
|
||||
$this->assertPasses(PetRequest::class, $this->valid());
|
||||
}
|
||||
|
||||
public function test_converts_persian_digits_in_weight(): void
|
||||
{
|
||||
$this->assertPasses(PetRequest::class, $this->valid(['weight' => '۲۰']));
|
||||
}
|
||||
|
||||
public function test_fails_without_required_fields(): void
|
||||
{
|
||||
$errors = $this->formRequestErrors(PetRequest::class, ['weight' => '1']);
|
||||
|
||||
foreach (['name', 'type', 'gender', 'breed', 'sterilized', 'birthday'] as $field) {
|
||||
$this->assertArrayHasKey($field, $errors);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_fails_without_weight(): void
|
||||
{
|
||||
$this->assertFailsOn('weight', PetRequest::class, $this->valid(['weight' => null]));
|
||||
}
|
||||
|
||||
public function test_fails_when_type_or_gender_is_a_key_instead_of_value(): void
|
||||
{
|
||||
$this->assertFailsOn('type', PetRequest::class, $this->valid(['type' => PetTypeEnum::Dog->name]));
|
||||
$this->assertFailsOn('gender', PetRequest::class, $this->valid(['gender' => PetGenderEnum::Male->name]));
|
||||
}
|
||||
|
||||
public function test_fails_with_non_integer_weight_or_invalid_birthday(): void
|
||||
{
|
||||
$this->assertFailsOn('weight', PetRequest::class, $this->valid(['weight' => 'heavy']));
|
||||
$this->assertFailsOn('birthday', PetRequest::class, $this->valid(['birthday' => 'x']));
|
||||
}
|
||||
|
||||
public function test_fails_with_non_boolean_sterilized(): void
|
||||
{
|
||||
$this->assertFailsOn('sterilized', PetRequest::class, $this->valid(['sterilized' => 'yes']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user