|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Test\Happyr\Validator\Constraint; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Doctrine\ORM\EntityRepository; |
| 9 | +use Happyr\Validator\Constraint\EntityExist; |
| 10 | +use Happyr\Validator\Constraint\EntityExistValidator; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 14 | +use Symfony\Component\Validator\Context\ExecutionContextInterface; |
| 15 | +use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; |
| 16 | + |
| 17 | +class EntityExistValidatorTest extends TestCase |
| 18 | +{ |
| 19 | + /** @var MockObject */ |
| 20 | + private $entityManager; |
| 21 | + |
| 22 | + /** @var MockObject */ |
| 23 | + private $context; |
| 24 | + |
| 25 | + /** @var EntityExistValidator */ |
| 26 | + private $validator; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
| 31 | + $this->context = $this->getMockBuilder(ExecutionContextInterface::class)->getMock(); |
| 32 | + |
| 33 | + $this->validator = new EntityExistValidator($this->entityManager); |
| 34 | + $this->validator->initialize($this->context); |
| 35 | + } |
| 36 | + |
| 37 | + public function testValidateWithWrongConstraint() |
| 38 | + { |
| 39 | + $this->expectException(\LogicException::class); |
| 40 | + $this->validator->validate('foo', new NotNull()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testValidateWithNoEntity() |
| 44 | + { |
| 45 | + $constraint = new EntityExist(); |
| 46 | + |
| 47 | + $this->expectException(\LogicException::class); |
| 48 | + $this->validator->validate('foobar', $constraint); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider getValidValues |
| 53 | + */ |
| 54 | + public function testValidateValidEntity($value) |
| 55 | + { |
| 56 | + $this->context->expects($this->never())->method('buildViolation'); |
| 57 | + $constraint = new EntityExist(); |
| 58 | + $constraint->entity = 'App\Entity\User'; |
| 59 | + |
| 60 | + $repository = $this->getMockBuilder(EntityRepository::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $repository |
| 64 | + ->expects($this->once()) |
| 65 | + ->method('findOneBy') |
| 66 | + ->with(['id' => $value]) |
| 67 | + ->willReturn('my_user'); |
| 68 | + |
| 69 | + $this->entityManager |
| 70 | + ->expects($this->once()) |
| 71 | + ->method('getRepository') |
| 72 | + ->with('App\Entity\User') |
| 73 | + ->willReturn($repository); |
| 74 | + |
| 75 | + $this->validator->validate($value, $constraint); |
| 76 | + } |
| 77 | + |
| 78 | + public function getValidValues() |
| 79 | + { |
| 80 | + yield ['foobar']; |
| 81 | + yield ['']; |
| 82 | + yield [null]; |
| 83 | + } |
| 84 | + |
| 85 | + public function testValidateValidEntityWithCustomProperty() |
| 86 | + { |
| 87 | + $this->context->expects($this->never())->method('buildViolation'); |
| 88 | + $constraint = new EntityExist(); |
| 89 | + $constraint->entity = 'App\Entity\User'; |
| 90 | + $constraint->property = 'uuid'; |
| 91 | + |
| 92 | + $repository = $this->getMockBuilder(EntityRepository::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->getMock(); |
| 95 | + $repository |
| 96 | + ->expects($this->once()) |
| 97 | + ->method('findOneBy') |
| 98 | + ->with(['uuid' => 'foobar']) |
| 99 | + ->willReturn('my_user'); |
| 100 | + |
| 101 | + $this->entityManager |
| 102 | + ->expects($this->once()) |
| 103 | + ->method('getRepository') |
| 104 | + ->with('App\Entity\User') |
| 105 | + ->willReturn($repository); |
| 106 | + |
| 107 | + $this->validator->validate('foobar', $constraint); |
| 108 | + } |
| 109 | + |
| 110 | + public function testValidateInvalidEntity() |
| 111 | + { |
| 112 | + $violationBuilder = $this->getMockBuilder(ConstraintViolationBuilderInterface::class)->getMock(); |
| 113 | + $violationBuilder->method('setParameter')->will($this->returnSelf()); |
| 114 | + |
| 115 | + $this->context->expects($this->once())->method('buildViolation')->willReturn($violationBuilder); |
| 116 | + $constraint = new EntityExist(); |
| 117 | + $constraint->entity = 'App\Entity\User'; |
| 118 | + |
| 119 | + $repository = $this->getMockBuilder(EntityRepository::class) |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->getMock(); |
| 122 | + $repository |
| 123 | + ->expects($this->once()) |
| 124 | + ->method('findOneBy') |
| 125 | + ->willReturn(null); |
| 126 | + |
| 127 | + $this->entityManager |
| 128 | + ->expects($this->once()) |
| 129 | + ->method('getRepository') |
| 130 | + ->willReturn($repository); |
| 131 | + |
| 132 | + $this->validator->validate('foobar', $constraint); |
| 133 | + } |
| 134 | +} |
0 commit comments