|
| 1 | +<?php |
| 2 | +namespace Tests\Functional\Infra; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use Prophecy\Prophecy\ObjectProphecy; |
| 6 | +use Symfony\Component\Validator\Constraints\Collection; |
| 7 | +use Symfony\Component\Validator\ConstraintViolationInterface; |
| 8 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 9 | +use Yoanm\JsonRpcParamsSymfonyValidator\Domain\Model\MethodWithValidatedParams; |
| 10 | +use Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator; |
| 11 | +use Yoanm\JsonRpcServer\Domain\Event\Action\ValidateParamsEvent; |
| 12 | +use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * @covers \Yoanm\JsonRpcParamsSymfonyValidator\Infra\JsonRpcParamsValidator |
| 16 | + * |
| 17 | + * @group Validator |
| 18 | + */ |
| 19 | +class JsonRpcParamsValidatorTest extends TestCase |
| 20 | +{ |
| 21 | + /** @var JsonRpcParamsValidator */ |
| 22 | + private $validator; |
| 23 | + /** @var ValidatorInterface|ObjectProphecy */ |
| 24 | + private $sfValidator; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->sfValidator = $this->prophesize(ValidatorInterface::class); |
| 29 | + |
| 30 | + $this->validator = new JsonRpcParamsValidator( |
| 31 | + $this->sfValidator->reveal() |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + public function testShouldDoNothingIfMethodDoesNotImplementSpecificInterface() |
| 36 | + { |
| 37 | + $paramList = ['paramList']; |
| 38 | + /** @var JsonRpcMethodInterface|ObjectProphecy $method */ |
| 39 | + $method = $this->prophesize(JsonRpcMethodInterface::class); |
| 40 | + $event = new ValidateParamsEvent($method->reveal(), $paramList); |
| 41 | + |
| 42 | + $this->validator->validate($event); |
| 43 | + $this->assertCount(0, $event->getViolationList()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testSshouldCallSfValidator() |
| 47 | + { |
| 48 | + $paramList = ['paramList']; |
| 49 | + /** @var MethodWithValidatedParams|JsonRpcMethodInterface|ObjectProphecy $method */ |
| 50 | + $method = $this->prophesize(MethodWithValidatedParams::class); |
| 51 | + $method->willImplement(JsonRpcMethodInterface::class); |
| 52 | + /** @var Collection|ObjectProphecy $paramConstraint */ |
| 53 | + $paramConstraint = $this->prophesize(Collection::class); |
| 54 | + $event = new ValidateParamsEvent($method->reveal(), $paramList); |
| 55 | + |
| 56 | + $method->getParamsConstraint() |
| 57 | + ->willReturn($paramConstraint->reveal()) |
| 58 | + ->shouldBeCalled() |
| 59 | + ; |
| 60 | + |
| 61 | + $this->sfValidator->validate($paramList, $paramConstraint->reveal()) |
| 62 | + ->willReturn([]) |
| 63 | + ->shouldBeCalled() |
| 64 | + ; |
| 65 | + |
| 66 | + $this->validator->validate($event); |
| 67 | + $this->assertCount(0, $event->getViolationList()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testShouldNormalizeViolations() |
| 71 | + { |
| 72 | + $paramList = ['paramList']; |
| 73 | + /** @var MethodWithValidatedParams|JsonRpcMethodInterface|ObjectProphecy $method */ |
| 74 | + $method = $this->prophesize(MethodWithValidatedParams::class); |
| 75 | + $method->willImplement(JsonRpcMethodInterface::class); |
| 76 | + /** @var Collection|ObjectProphecy $paramConstraint */ |
| 77 | + $paramConstraint = $this->prophesize(Collection::class); |
| 78 | + /** @var ConstraintViolationInterface|ObjectProphecy $violation1 */ |
| 79 | + $violation1 = $this->prophesize(ConstraintViolationInterface::class); |
| 80 | + /** @var ConstraintViolationInterface|ObjectProphecy $violation2 */ |
| 81 | + $violation2 = $this->prophesize(ConstraintViolationInterface::class); |
| 82 | + /** @var ConstraintViolationInterface|ObjectProphecy $violation3 */ |
| 83 | + $violation3 = $this->prophesize(ConstraintViolationInterface::class); |
| 84 | + $violationList = [$violation1->reveal(), $violation2->reveal(), $violation3->reveal()]; |
| 85 | + $expectedNormalizedErrorList = [ |
| 86 | + [ |
| 87 | + 'path' => 'path1', |
| 88 | + 'message' => 'message1', |
| 89 | + 'code' => 'code1', |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'path' => 'path2', |
| 93 | + 'message' => 'message2', |
| 94 | + 'code' => 'code2', |
| 95 | + ], |
| 96 | + [ |
| 97 | + 'path' => 'path3', |
| 98 | + 'message' => 'message3', |
| 99 | + 'code' => 'code3', |
| 100 | + ] |
| 101 | + ]; |
| 102 | + $violation1->getPropertyPath()->willReturn($expectedNormalizedErrorList[0]['path'])->shouldBeCalled(); |
| 103 | + $violation1->getMessage()->willReturn($expectedNormalizedErrorList[0]['message'])->shouldBeCalled(); |
| 104 | + $violation1->getCode()->willReturn($expectedNormalizedErrorList[0]['code'])->shouldBeCalled(); |
| 105 | + $violation2->getPropertyPath()->willReturn($expectedNormalizedErrorList[1]['path'])->shouldBeCalled(); |
| 106 | + $violation2->getMessage()->willReturn($expectedNormalizedErrorList[1]['message'])->shouldBeCalled(); |
| 107 | + $violation2->getCode()->willReturn($expectedNormalizedErrorList[1]['code'])->shouldBeCalled(); |
| 108 | + $violation3->getPropertyPath()->willReturn($expectedNormalizedErrorList[2]['path'])->shouldBeCalled(); |
| 109 | + $violation3->getMessage()->willReturn($expectedNormalizedErrorList[2]['message'])->shouldBeCalled(); |
| 110 | + $violation3->getCode()->willReturn($expectedNormalizedErrorList[2]['code'])->shouldBeCalled(); |
| 111 | + |
| 112 | + $event = new ValidateParamsEvent($method->reveal(), $paramList); |
| 113 | + |
| 114 | + $method->getParamsConstraint() |
| 115 | + ->willReturn($paramConstraint->reveal()) |
| 116 | + ->shouldBeCalled() |
| 117 | + ; |
| 118 | + |
| 119 | + $this->sfValidator->validate($paramList, $paramConstraint->reveal()) |
| 120 | + ->willReturn($violationList) |
| 121 | + ->shouldBeCalled() |
| 122 | + ; |
| 123 | + |
| 124 | + $this->validator->validate($event); |
| 125 | + |
| 126 | + $this->assertSame($expectedNormalizedErrorList, $event->getViolationList()); |
| 127 | + } |
| 128 | +} |
0 commit comments