Skip to content

Commit

Permalink
Add Validation for Domain Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Dec 20, 2023
1 parent a378d8a commit 41ff50c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ services:
tags:
- { name: validator.constraint_validator, alias: password_change }

App\Validator\Contraints\DomainValidator:
tags:
- { name: validator.constraint_validator }

App\Validator\Constraints\EmailDomainValidator:
tags:
- { name: validator.constraint_validator }
Expand Down
7 changes: 7 additions & 0 deletions config/validator/validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ App\Entity\Alias:
minLength: 3
maxLength: 24

App\Entity\Domain:
properties:
name:
- NotNull: ~
- NotBlank: ~
- App\Validator\Constraints\Domain: ~

App\Entity\User:
properties:
email:
Expand Down
2 changes: 2 additions & 0 deletions default_translations/de/validators.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ form:
werden.
registration-recovery-token-noack: Dieses Feld muss ausgewählt werden.
invalid-token: Der Code hat ein ungültiges Format.
already-exists: "Existiert bereits."

registration:
voucher-invalid: Der Einladungscode ist ungültig.
email-domain-not-exists: Die E-Mail-Adresse ist ungültig.
Expand Down
1 change: 1 addition & 0 deletions default_translations/en/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ form:
invalid-token: "This token has an invalid format."
twofactor-secret-invalid: "The verification code is not valid."
twofactor-backup-ack-missing: "This needs to be checked."
already-exists: "Already exists."

registration:
voucher-invalid: "The invite code is invalid."
Expand Down
9 changes: 9 additions & 0 deletions src/Validator/Constraints/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class Domain extends Constraint
{
}
31 changes: 31 additions & 0 deletions src/Validator/Constraints/DomainValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Validator\Constraints;

use App\Entity\Domain;
use App\Repository\DomainRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class DomainValidator extends ConstraintValidator
{
private DomainRepository $domainRepository;

public function __construct(EntityManagerInterface $manager)
{
$this->domainRepository = $manager->getRepository(Domain::class);
}

public function validate($value, Constraint $constraint): void
{
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}

if ($this->domainRepository->findByName($value) !== null) {
$this->context->addViolation('form.already-exists');
}
}
}
56 changes: 56 additions & 0 deletions tests/Validator/Constraints/DomainValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use App\Entity\Domain as DomainEntity;
use App\Repository\DomainRepository;
use App\Validator\Constraints\Domain;
use App\Validator\Constraints\DomainValidator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class DomainValidatorTest extends ConstraintValidatorTestCase
{
private $domain = 'example.org';

protected function createValidator(): DomainValidator
{
$domainRepository = $this->getMockBuilder(DomainRepository::class)
->disableOriginalConstructor()
->getMock();
$domainRepository->method('findByName')->willReturnCallback(function ($name) {
if ($name === $this->domain) {
return new DomainEntity();
}

return null;
});
$manager = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
$manager->method('getRepository')->willReturnMap([
[DomainEntity::class, $domainRepository],
]);

return new DomainValidator($manager);
}

public function testIsNotString()
{
$this->expectException(UnexpectedTypeException::class);

$this->validator->validate(42, new Domain());
}

public function testIsValid()
{
$this->validator->validate('example.com', new Domain());

$this->assertNoViolation();
}

public function testIsNotUnique()
{
$this->validator->validate($this->domain, new Domain());

$this->buildViolation('form.already-exists')
->assertRaised();
}
}

0 comments on commit 41ff50c

Please sign in to comment.