|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Integration\Validator; |
| 6 | + |
| 7 | +use PhpList\RestBundle\Entity\RequestInterface; |
| 8 | +use PhpList\RestBundle\Tests\Helpers\DummyRequestDto; |
| 9 | +use PhpList\RestBundle\Validator\RequestValidator; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use RuntimeException; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
| 15 | +use Symfony\Component\Serializer\SerializerInterface; |
| 16 | +use Symfony\Component\Validator\ConstraintViolation; |
| 17 | +use Symfony\Component\Validator\ConstraintViolationList; |
| 18 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 19 | + |
| 20 | +class RequestValidatorTest extends TestCase |
| 21 | +{ |
| 22 | + private SerializerInterface|MockObject $serializer; |
| 23 | + private ValidatorInterface|MockObject $validator; |
| 24 | + private RequestValidator $requestValidator; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->serializer = $this->createMock(SerializerInterface::class); |
| 29 | + $this->validator = $this->createMock(ValidatorInterface::class); |
| 30 | + $this->requestValidator = new RequestValidator( |
| 31 | + $this->serializer, |
| 32 | + $this->validator |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testValidateReturnsDtoWhenJsonValidAndNoViolations(): void |
| 37 | + { |
| 38 | + $dto = $this->createMock(RequestInterface::class); |
| 39 | + $json = '{"foo":"bar"}'; |
| 40 | + |
| 41 | + $this->serializer |
| 42 | + ->expects(self::once()) |
| 43 | + ->method('deserialize') |
| 44 | + ->with($json, DummyRequestDto::class, 'json') |
| 45 | + ->willReturn($dto); |
| 46 | + |
| 47 | + $this->validator |
| 48 | + ->expects(self::once()) |
| 49 | + ->method('validate') |
| 50 | + ->with($dto) |
| 51 | + ->willReturn(new ConstraintViolationList()); |
| 52 | + |
| 53 | + $request = new Request([], [], [], [], [], [], $json); |
| 54 | + |
| 55 | + $result = $this->requestValidator->validate($request, DummyRequestDto::class); |
| 56 | + self::assertSame($dto, $result); |
| 57 | + } |
| 58 | + |
| 59 | + public function testValidateThrowsOnInvalidJson(): void |
| 60 | + { |
| 61 | + $json = '{ invalid json }'; |
| 62 | + $request = new Request([], [], [], [], [], [], $json); |
| 63 | + |
| 64 | + $this->serializer |
| 65 | + ->expects(self::once()) |
| 66 | + ->method('deserialize') |
| 67 | + ->willThrowException(new RuntimeException('Syntax error')); |
| 68 | + |
| 69 | + $this->expectException(UnprocessableEntityHttpException::class); |
| 70 | + $this->expectExceptionMessage('Invalid JSON: Syntax error'); |
| 71 | + |
| 72 | + $this->requestValidator->validate($request, DummyRequestDto::class); |
| 73 | + } |
| 74 | + |
| 75 | + public function testValidateThrowsOnConstraintViolations(): void |
| 76 | + { |
| 77 | + $dto = $this->createMock(RequestInterface::class); |
| 78 | + $json = '{"email":"bad"}'; |
| 79 | + $request = new Request([], [], [], [], [], [], $json); |
| 80 | + |
| 81 | + $this->serializer |
| 82 | + ->method('deserialize') |
| 83 | + ->willReturn($dto); |
| 84 | + |
| 85 | + $violation1 = new ConstraintViolation( |
| 86 | + 'Must not be blank', |
| 87 | + '', |
| 88 | + [], |
| 89 | + null, |
| 90 | + 'email', |
| 91 | + '' |
| 92 | + ); |
| 93 | + $violation2 = new ConstraintViolation( |
| 94 | + 'Must be a valid email', |
| 95 | + '', |
| 96 | + [], |
| 97 | + null, |
| 98 | + 'email', |
| 99 | + 'bad' |
| 100 | + ); |
| 101 | + $violations = new ConstraintViolationList([$violation1, $violation2]); |
| 102 | + |
| 103 | + $this->validator |
| 104 | + ->method('validate') |
| 105 | + ->with($dto) |
| 106 | + ->willReturn($violations); |
| 107 | + |
| 108 | + $this->expectException(UnprocessableEntityHttpException::class); |
| 109 | + |
| 110 | + $this->expectExceptionMessage("email: Must not be blank\nemail: Must be a valid email"); |
| 111 | + |
| 112 | + $this->requestValidator->validate($request, DummyRequestDto::class); |
| 113 | + } |
| 114 | +} |
0 commit comments