|
17 | 17 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
|
18 | 18 | use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
|
19 | 19 | use ApiPlatform\Core\Tests\Fixtures\DummyEntity;
|
| 20 | +use Psr\Container\ContainerInterface; |
20 | 21 | use Symfony\Component\HttpFoundation\Request;
|
21 | 22 | use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
|
22 | 23 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
@@ -63,6 +64,49 @@ public function testValidatorIsCalled()
|
63 | 64 | $validationViewListener->onKernelView($event);
|
64 | 65 | }
|
65 | 66 |
|
| 67 | + public function testGetGroupsFromCallable() |
| 68 | + { |
| 69 | + $data = new DummyEntity(); |
| 70 | + $expectedValidationGroups = ['a', 'b', 'c']; |
| 71 | + |
| 72 | + $validatorProphecy = $this->prophesize(ValidatorInterface::class); |
| 73 | + $validatorProphecy->validate($data, null, $expectedValidationGroups)->shouldBeCalled(); |
| 74 | + $validator = $validatorProphecy->reveal(); |
| 75 | + |
| 76 | + $closure = function ($data) use ($expectedValidationGroups): array { |
| 77 | + return $data instanceof DummyEntity ? $expectedValidationGroups : []; |
| 78 | + }; |
| 79 | + |
| 80 | + list($resourceMetadataFactory, $event) = $this->createEventObject($closure, $data); |
| 81 | + |
| 82 | + $validationViewListener = new ValidateListener($validator, $resourceMetadataFactory); |
| 83 | + $validationViewListener->onKernelView($event); |
| 84 | + } |
| 85 | + |
| 86 | + public function testGetGroupsFromService() |
| 87 | + { |
| 88 | + $data = new DummyEntity(); |
| 89 | + |
| 90 | + $validatorProphecy = $this->prophesize(ValidatorInterface::class); |
| 91 | + $validatorProphecy->validate($data, null, ['a', 'b', 'c'])->shouldBeCalled(); |
| 92 | + $validator = $validatorProphecy->reveal(); |
| 93 | + |
| 94 | + list($resourceMetadataFactory, $event) = $this->createEventObject('groups_builder', $data); |
| 95 | + |
| 96 | + $containerProphecy = $this->prophesize(ContainerInterface::class); |
| 97 | + $containerProphecy->has('groups_builder')->willReturn(true)->shouldBeCalled(); |
| 98 | + $containerProphecy->get('groups_builder')->willReturn(new class() { |
| 99 | + public function __invoke($data): array |
| 100 | + { |
| 101 | + return $data instanceof DummyEntity ? ['a', 'b', 'c'] : []; |
| 102 | + } |
| 103 | + } |
| 104 | + )->shouldBeCalled(); |
| 105 | + |
| 106 | + $validationViewListener = new ValidateListener($validator, $resourceMetadataFactory, $containerProphecy->reveal()); |
| 107 | + $validationViewListener->onKernelView($event); |
| 108 | + } |
| 109 | + |
66 | 110 | public function testDoNotCallWhenReceiveFlagIsFalse()
|
67 | 111 | {
|
68 | 112 | $data = new DummyEntity();
|
@@ -100,19 +144,10 @@ public function testThrowsValidationExceptionWithViolationsFound()
|
100 | 144 | $validationViewListener->onKernelView($event);
|
101 | 145 | }
|
102 | 146 |
|
103 |
| - /** |
104 |
| - * @param array $expectedValidationGroups |
105 |
| - * @param mixed $data |
106 |
| - * @param bool $receive |
107 |
| - * |
108 |
| - * @return array |
109 |
| - */ |
110 |
| - private function createEventObject($expectedValidationGroups, $data, bool $receive = true) |
| 147 | + private function createEventObject($expectedValidationGroups, $data, bool $receive = true): array |
111 | 148 | {
|
112 | 149 | $resourceMetadata = new ResourceMetadata(null, null, null, [
|
113 |
| - 'create' => [ |
114 |
| - 'validation_groups' => $expectedValidationGroups, |
115 |
| - ], |
| 150 | + 'create' => ['validation_groups' => $expectedValidationGroups], |
116 | 151 | ]);
|
117 | 152 |
|
118 | 153 | $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
|
|
0 commit comments