1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace spec\Knp\DictionaryBundle\Validator\Constraints; |
6
|
|
|
|
7
|
|
|
use Knp\DictionaryBundle\Dictionary; |
8
|
|
|
use Knp\DictionaryBundle\Dictionary\Collection; |
9
|
|
|
use Knp\DictionaryBundle\Validator\Constraints\Dictionary as Constraint; |
10
|
|
|
use Knp\DictionaryBundle\Validator\Constraints\DictionaryValidator; |
11
|
|
|
use PhpSpec\ObjectBehavior; |
12
|
|
|
use Prophecy\Argument; |
13
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
14
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
16
|
|
|
|
17
|
|
|
final class DictionaryValidatorSpec extends ObjectBehavior |
18
|
|
|
{ |
19
|
|
|
function let(ExecutionContextInterface $context, Dictionary $dictionary) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$dictionary->getName()->willReturn('dico'); |
22
|
|
|
$dictionary->getKeys()->willReturn(['the_key']); |
23
|
|
|
|
24
|
|
|
$dictionaries = new Collection($dictionary->getWrappedObject()); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->beConstructedWith($dictionaries); |
27
|
|
|
$this->initialize($context); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function it_is_initializable() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->shouldHaveType(DictionaryValidator::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_valids_existing_keys($context) |
36
|
|
|
{ |
37
|
|
|
$constraint = new Constraint(['name' => 'dico']); |
38
|
|
|
|
39
|
|
|
$context->addViolation(Argument::any())->shouldNotBeCalled(); |
40
|
|
|
|
41
|
|
|
$this->validate('the_key', $constraint); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function it_adds_violation_for_an_unexisting_keys($context) |
45
|
|
|
{ |
46
|
|
|
$constraint = new Constraint(['name' => 'dico']); |
47
|
|
|
|
48
|
|
|
$context |
49
|
|
|
->addViolation( |
50
|
|
|
"The key {{ key }} doesn't exist in the given dictionary. {{ keys }} available.", |
51
|
|
|
[ |
52
|
|
|
'{{ key }}' => '"the_unexisting_key"', |
53
|
|
|
'{{ keys }}' => '"the_key"', |
54
|
|
|
] |
55
|
|
|
) |
56
|
|
|
->shouldBeCalled() |
57
|
|
|
; |
58
|
|
|
|
59
|
|
|
$this->validate('the_unexisting_key', $constraint); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function it_transforms_keys_in_string_representation($dictionary, $context) |
63
|
|
|
{ |
64
|
|
|
$dictionary->getKeys()->willReturn(['the_key', true, 12, 3.14, 0.0, null]); |
65
|
|
|
|
66
|
|
|
$constraint = new Constraint(['name' => 'dico']); |
67
|
|
|
|
68
|
|
|
$context |
69
|
|
|
->addViolation( |
70
|
|
|
"The key {{ key }} doesn't exist in the given dictionary. {{ keys }} available.", |
71
|
|
|
[ |
72
|
|
|
'{{ key }}' => '"the_unexisting_key"', |
73
|
|
|
'{{ keys }}' => '"the_key", true, 12, 3.14, 0.0, null', |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
->shouldBeCalled() |
77
|
|
|
; |
78
|
|
|
|
79
|
|
|
$this->validate('the_unexisting_key', $constraint); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function it_throw_exception_form_unknown_constraints() |
83
|
|
|
{ |
84
|
|
|
$constraint = new NotNull(); |
85
|
|
|
|
86
|
|
|
$this->shouldThrow(new UnexpectedTypeException($constraint, Constraint::class))->duringValidate('the_key', $constraint); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.