|
4 | 4 |
|
5 | 5 | namespace Yokai\Batch\Tests\Bridge\Symfony\Validator;
|
6 | 6 |
|
| 7 | +use Composer\InstalledVersions; |
| 8 | +use Doctrine\Common\Annotations\AnnotationReader; |
7 | 9 | use PHPUnit\Framework\TestCase;
|
8 |
| -use Prophecy\PhpUnit\ProphecyTrait; |
9 |
| -use Prophecy\Prophecy\ObjectProphecy; |
10 |
| -use Symfony\Component\Validator\ConstraintViolationInterface; |
11 |
| -use Symfony\Component\Validator\ConstraintViolationList; |
| 10 | +use Symfony\Component\Validator\Constraints\Blank; |
| 11 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 12 | +use Symfony\Component\Validator\Validation; |
12 | 13 | use Symfony\Component\Validator\Validator\ValidatorInterface;
|
13 | 14 | use Yokai\Batch\Bridge\Symfony\Validator\SkipInvalidItemProcessor;
|
14 | 15 | use Yokai\Batch\Job\Item\InvalidItemException;
|
| 16 | +use Yokai\Batch\Tests\Bridge\Symfony\Validator\Fixtures\ObjectWithAnnotationValidation; |
15 | 17 |
|
16 | 18 | class SkipInvalidItemProcessorTest extends TestCase
|
17 | 19 | {
|
18 |
| - use ProphecyTrait; |
| 20 | + private static ValidatorInterface $validator; |
| 21 | + |
| 22 | + public static function setUpBeforeClass(): void |
| 23 | + { |
| 24 | + if (\version_compare(InstalledVersions::getVersion('symfony/validator'), '5.0.0') >= 0) { |
| 25 | + self::$validator = Validation::createValidatorBuilder() |
| 26 | + ->enableAnnotationMapping(true) |
| 27 | + ->addDefaultDoctrineAnnotationReader() |
| 28 | + ->getValidator(); |
| 29 | + } else { |
| 30 | + // @codeCoverageIgnoreStart |
| 31 | + // Symfony 4.x compatibility |
| 32 | + self::$validator = Validation::createValidatorBuilder() |
| 33 | + ->enableAnnotationMapping(new AnnotationReader()) |
| 34 | + ->getValidator(); |
| 35 | + // @codeCoverageIgnoreEnd |
| 36 | + } |
| 37 | + } |
19 | 38 |
|
20 | 39 | /**
|
21 | 40 | * @dataProvider groups
|
22 | 41 | */
|
23 |
| - public function testProcessValid(array $groups = null): void |
| 42 | + public function testProcessValid(?array $groups): void |
24 | 43 | {
|
25 |
| - /** @var ObjectProphecy|ValidatorInterface $validator */ |
26 |
| - $validator = $this->prophesize(ValidatorInterface::class); |
27 |
| - $validator->validate('item to validate', null, $groups) |
28 |
| - ->shouldBeCalledTimes(1) |
29 |
| - ->willReturn(new ConstraintViolationList([])); |
30 |
| - |
31 |
| - $processor = new SkipInvalidItemProcessor($validator->reveal(), $groups); |
32 |
| - self::assertSame('item to validate', $processor->process('item to validate')); |
| 44 | + $processor = new SkipInvalidItemProcessor(self::$validator, [new NotBlank(['groups' => $groups])], $groups); |
| 45 | + self::assertSame('valid item not blank', $processor->process('valid item not blank')); |
33 | 46 | }
|
34 | 47 |
|
35 | 48 | /**
|
36 | 49 | * @dataProvider groups
|
37 | 50 | */
|
38 |
| - public function testProcessInvalid(array $groups = null): void |
| 51 | + public function testProcessInvalid(?array $groups): void |
39 | 52 | {
|
40 | 53 | $this->expectException(InvalidItemException::class);
|
41 | 54 |
|
42 |
| - $violations = new ConstraintViolationList([]); |
43 |
| - /** @var ObjectProphecy|ConstraintViolationInterface $stringViolation */ |
44 |
| - $stringViolation = $this->prophesize(ConstraintViolationInterface::class); |
45 |
| - $stringViolation->getPropertyPath()->willReturn('stringProperty'); |
46 |
| - $stringViolation->getInvalidValue()->willReturn('invalid string'); |
47 |
| - $stringViolation->getMessage()->willReturn('"invalid string" is invalid'); |
48 |
| - |
49 |
| - /** @var ObjectProphecy|ConstraintViolationInterface $dateViolation */ |
50 |
| - $dateViolation = $this->prophesize(ConstraintViolationInterface::class); |
51 |
| - $dateViolation->getPropertyPath()->willReturn('dateProperty'); |
52 |
| - $dateViolation->getInvalidValue()->willReturn(new \DateTime()); |
53 |
| - $dateViolation->getMessage()->willReturn('"invalid date" is invalid'); |
54 |
| - |
55 |
| - /** @var ObjectProphecy|ConstraintViolationInterface $objectToStringViolation */ |
56 |
| - $objectToStringViolation = $this->prophesize(ConstraintViolationInterface::class); |
57 |
| - $objectToStringViolation->getPropertyPath()->willReturn('objectToStringProperty'); |
58 |
| - $objectToStringViolation->getInvalidValue()->willReturn( |
59 |
| - new class { |
60 |
| - public function __toString(): string |
61 |
| - { |
62 |
| - return 'invalid object'; |
63 |
| - } |
64 |
| - } |
| 55 | + $processor = new SkipInvalidItemProcessor( |
| 56 | + self::$validator, |
| 57 | + [new Blank(['groups' => ['Default', 'Full']])], |
| 58 | + $groups |
65 | 59 | );
|
66 |
| - $objectToStringViolation->getMessage()->willReturn('"object with __toString" is invalid'); |
67 |
| - |
68 |
| - /** @var ObjectProphecy|ConstraintViolationInterface $dateViolation */ |
69 |
| - $objectViolation = $this->prophesize(ConstraintViolationInterface::class); |
70 |
| - $objectViolation->getPropertyPath()->willReturn('objectProperty'); |
71 |
| - $objectViolation->getInvalidValue()->willReturn(new \stdClass()); |
72 |
| - $objectViolation->getMessage()->willReturn('"object" is invalid'); |
| 60 | + $processor->process('invalid item not blank'); |
| 61 | + } |
73 | 62 |
|
74 |
| - /** @var ObjectProphecy|ConstraintViolationInterface $arrayViolation */ |
75 |
| - $arrayViolation = $this->prophesize(ConstraintViolationInterface::class); |
76 |
| - $arrayViolation->getPropertyPath()->willReturn('arrayProperty'); |
77 |
| - $arrayViolation->getInvalidValue()->willReturn( |
78 |
| - [null, new \stdClass(), [1, new \DateTime(), new \ArrayIterator(['string', 2.3])]] |
| 63 | + /** |
| 64 | + * @dataProvider groups |
| 65 | + */ |
| 66 | + public function testProcessNormalization(?array $groups): void |
| 67 | + { |
| 68 | + $this->expectException(InvalidItemException::class); |
| 69 | + $this->expectExceptionMessageMatches( |
| 70 | + <<<REGEXP |
| 71 | +#^emptyString: This value should be null\.: "" |
| 72 | +null: This value should not be null\.: NULL |
| 73 | +string: This value should be null\.: string |
| 74 | +int: This value should be null\.: 1 |
| 75 | +date: This value should be null\.: 2021-09-23T12:09:32\+0200 |
| 76 | +array: This collection should contain exactly 0 elements\.: 1, 2 |
| 77 | +objectStringable: This value should be null\.: /.+/tests/Fixtures/ObjectWithAnnotationValidation\.php |
| 78 | +objectNotStringable: This value should be null\.: class@anonymous.+ |
| 79 | +valueWithoutInterpretation: This value should be null\.: resource$# |
| 80 | +REGEXP |
79 | 81 | );
|
80 |
| - $arrayViolation->getMessage()->willReturn('"array" is invalid'); |
81 |
| - |
82 |
| - $violations->add($stringViolation->reveal()); |
83 |
| - $violations->add($dateViolation->reveal()); |
84 |
| - $violations->add($objectToStringViolation->reveal()); |
85 |
| - $violations->add($objectViolation->reveal()); |
86 |
| - $violations->add($arrayViolation->reveal()); |
87 |
| - |
88 |
| - /** @var ObjectProphecy|ValidatorInterface $validator */ |
89 |
| - $validator = $this->prophesize(ValidatorInterface::class); |
90 |
| - $validator->validate('item to validate', null, $groups) |
91 |
| - ->shouldBeCalledTimes(1) |
92 |
| - ->willReturn($violations); |
93 | 82 |
|
94 |
| - $processor = new SkipInvalidItemProcessor($validator->reveal(), $groups); |
95 |
| - $processor->process('item to validate'); |
| 83 | + $processor = new SkipInvalidItemProcessor(self::$validator, null, $groups); |
| 84 | + $processor->process(new ObjectWithAnnotationValidation()); |
96 | 85 | }
|
97 | 86 |
|
98 | 87 | public function groups()
|
99 | 88 | {
|
100 |
| - yield [null]; |
101 |
| - yield [[]]; |
102 |
| - yield [['Full']]; |
| 89 | + yield 'No groups specified' => [null]; |
| 90 | + yield 'Group "Full" only' => [['Full']]; |
103 | 91 | }
|
104 | 92 | }
|
0 commit comments