-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a378d8a
commit 41ff50c
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |