feat: simplify validation
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Validator\Constraints;
|
||||
|
||||
use App\Form\Model\ParticipantDto;
|
||||
use App\Validator\Constraints\ApplicantEmail;
|
||||
use App\Validator\Constraints\ApplicantEmailValidator;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
class ApplicantEmailValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator(): ApplicantEmailValidator
|
||||
{
|
||||
return new ApplicantEmailValidator();
|
||||
}
|
||||
|
||||
public function testApplicantWithValidEmailPassesValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0; // Applicant
|
||||
$participant->email = '[email protected]';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testApplicantWithoutEmailFailsValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0; // Applicant
|
||||
$participant->email = null;
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->buildViolation('Bitte angeben')
|
||||
->atPath('property.path.email')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testApplicantWithEmptyEmailFailsValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0; // Applicant
|
||||
$participant->email = '';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->buildViolation('Bitte angeben')
|
||||
->atPath('property.path.email')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testApplicantWithInvalidEmailFailsValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 0; // Applicant
|
||||
$participant->email = 'invalid-email';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->buildViolation('Bitte eine gültige E-Mail Adresse angeben')
|
||||
->atPath('property.path.email')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testNonApplicantWithoutEmailPassesValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 1; // Not applicant
|
||||
$participant->email = null;
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testNonApplicantWithEmptyEmailPassesValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 2; // Not applicant
|
||||
$participant->email = '';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testNonApplicantWithValidEmailPassesValidation(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 1; // Not applicant
|
||||
$participant->email = '[email protected]';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testNonApplicantWithInvalidEmailStillValidatesFormat(): void
|
||||
{
|
||||
$participant = new ParticipantDto();
|
||||
$participant->index = 1; // Not applicant
|
||||
$participant->email = 'invalid-email';
|
||||
|
||||
$this->validator->validate($participant, new ApplicantEmail());
|
||||
|
||||
// Non-applicants don't need email, but if provided it should be valid
|
||||
// However, our validator only checks format for applicants
|
||||
// The Email constraint on the property handles format validation for all participants
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user