|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\Validation\Constraints\FieldConstraints; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\Constraints\FieldConstraints\JsonData; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\Constraints\FieldConstraints\JsonDataValidator; |
|
7
|
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
|
8
|
|
|
|
|
9
|
|
|
class JsonValidatorTest extends ConstraintValidatorTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public const VALID = [ |
|
12
|
|
|
'{"testArray": [1,2,3]}' , |
|
13
|
|
|
'{"testObject":{ "firstVar": "a string", "secondVar": 123}}', |
|
14
|
|
|
'false', |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
public const INVALID = [ |
|
18
|
|
|
'{"malformedObject"', |
|
19
|
|
|
'{"missingQuotes": in a string}', |
|
20
|
|
|
'{"missingCommas": "between" "different":"values"}', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
public function provideValid(): \Generator |
|
24
|
|
|
{ |
|
25
|
|
|
foreach (self::VALID as $value) { |
|
26
|
|
|
yield $value => [$value]; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
* @small |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $value |
|
35
|
|
|
* |
|
36
|
|
|
* @dataProvider provideValid |
|
37
|
|
|
*/ |
|
38
|
|
|
public function noViolationsForValidValues(string $value): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->validator->validate($value, new JsonData()); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertNoViolation(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function provideInvalid(): \Generator |
|
46
|
|
|
{ |
|
47
|
|
|
foreach (self::INVALID as $value) { |
|
48
|
|
|
yield $value => [$value]; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
* @small |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $value |
|
57
|
|
|
* |
|
58
|
|
|
* @dataProvider provideInvalid |
|
59
|
|
|
*/ |
|
60
|
|
|
public function violationsForInvalidValues(string $value): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->validator->validate($value, new JsonData()); |
|
63
|
|
|
|
|
64
|
|
|
$this->buildViolation(JsonData::MESSAGE) |
|
65
|
|
|
->setParameter('{{ string }}', $value) |
|
66
|
|
|
->setParameter('{{ error }}', 'Syntax error') |
|
67
|
|
|
->assertRaised(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function createValidator() |
|
71
|
|
|
{ |
|
72
|
|
|
return new JsonDataValidator(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|