1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Port\Steps\Step; |
4
|
|
|
|
5
|
|
|
use Port\Steps\Step; |
6
|
|
|
use Symfony\Component\Validator\Constraint; |
7
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
8
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
9
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
10
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
11
|
|
|
use PhpSpec\ObjectBehavior; |
12
|
|
|
use Prophecy\Argument; |
13
|
|
|
|
14
|
|
|
class ValidatorStepSpec extends ObjectBehavior |
15
|
|
|
{ |
16
|
|
|
function let(ValidatorInterface $validator) |
17
|
|
|
{ |
18
|
|
|
$this->beConstructedWith($validator); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
function it_is_initializable() |
22
|
|
|
{ |
23
|
|
|
$this->shouldHaveType('Port\Steps\Step\ValidatorStep'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function it_is_a_step() |
27
|
|
|
{ |
28
|
|
|
$this->shouldHaveType('Port\Steps\Step'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function it_has_a_priority() |
32
|
|
|
{ |
33
|
|
|
$this->getPriority()->shouldReturn(128); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
function it_processes_an_item(Step $step, ValidatorInterface $validator, Constraint $constraint, ConstraintViolationListInterface $list) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$next = function() {}; |
39
|
|
|
$item = ['foo' => true]; |
40
|
|
|
$step->process($item, $next)->willReturn(true); |
41
|
|
|
$list->count()->willReturn(0); |
42
|
|
|
$validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection'))->willReturn($list); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$this->add('foo', $constraint)->shouldReturn($this); |
45
|
|
|
|
46
|
|
|
$this->process( |
|
|
|
|
47
|
|
|
$item, |
48
|
|
|
function($item) use ($step, $next) { |
49
|
|
|
return $step->process($item, $next); |
50
|
|
|
} |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
function it_processes_and_validates_an_item(Step $step, ValidatorInterface $validator, Constraint $constraint, ConstraintViolationListInterface $list) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$next = function() {}; |
57
|
|
|
$item = ['foo' => true]; |
58
|
|
|
$step->process($item, $next)->shouldNotBeCalled(); |
59
|
|
|
$list->count()->willReturn(1); |
60
|
|
|
$validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection'))->willReturn($list); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->add('foo', $constraint)->shouldReturn($this); |
63
|
|
|
|
64
|
|
|
$this->process( |
|
|
|
|
65
|
|
|
$item, |
66
|
|
|
function($item) use ($step, $next) { |
67
|
|
|
return $step->process($item, $next); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->getViolations()->shouldReturn([1 => $list]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function it_throws_an_exception_when_option_is_not_supported(Step $step, ValidatorInterface $validator, Constraint $constraint, ConstraintViolation $violation) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$next = function() {}; |
77
|
|
|
$item = ['foo' => true]; |
78
|
|
|
$step->process($item, $next)->shouldNotBeCalled(); |
79
|
|
|
|
80
|
|
|
$this->add('foo', $constraint)->shouldReturn($this); |
81
|
|
|
$this->addOption('bar', 'baz')->shouldReturn($this); |
82
|
|
|
|
83
|
|
|
$this->shouldThrow('Symfony\Component\Validator\Exception\InvalidOptionsException')->duringProcess( |
84
|
|
|
$item, |
85
|
|
|
function($item) use ($step, $next) { |
86
|
|
|
return $step->process($item, $next); |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
function it_throws_an_exception_during_process_when_validation_fails( |
|
|
|
|
92
|
|
|
Step $step, |
93
|
|
|
ValidatorInterface $validator, |
94
|
|
|
Constraint $constraint, |
95
|
|
|
ConstraintViolation $violation |
96
|
|
|
) { |
97
|
|
|
$next = function() {}; |
98
|
|
|
$item = ['foo' => true]; |
99
|
|
|
$step->process($item, $next)->shouldNotBeCalled(); |
100
|
|
|
$list = new ConstraintViolationList([$violation->getWrappedObject()]); |
|
|
|
|
101
|
|
|
$validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection'))->willReturn($list); |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->add('foo', $constraint)->shouldReturn($this); |
104
|
|
|
$this->throwExceptions(); |
105
|
|
|
|
106
|
|
|
$this->shouldThrow('Port\Exception\ValidationException')->duringProcess( |
107
|
|
|
$item, |
108
|
|
|
function($item) use ($step, $next) { |
109
|
|
|
return $step->process($item, $next); |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
function it_validates_an_item_from_metadata( |
115
|
|
|
ValidatorInterface $validator, |
116
|
|
|
ConstraintViolationListInterface $list |
117
|
|
|
) { |
118
|
|
|
$next = function() {}; |
119
|
|
|
$list->count()->willReturn(1); |
120
|
|
|
$item = new \stdClass(); |
121
|
|
|
$validator->validate($item)->willReturn($list); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->process( |
|
|
|
|
124
|
|
|
$item, |
125
|
|
|
$next |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->getViolations()->shouldReturn([1 => $list]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
function it_validates_multiple_items_from_metadata( |
132
|
|
|
ValidatorInterface $validator, |
133
|
|
|
ConstraintViolationListInterface $list |
134
|
|
|
) |
135
|
|
|
{ |
136
|
|
|
$numberOfCalls = 3; |
137
|
|
|
$next = function() {}; |
138
|
|
|
$list->count()->willReturn(1); |
139
|
|
|
$item = new \stdClass(); |
140
|
|
|
$validator->validate($item)->willReturn($list); |
|
|
|
|
141
|
|
|
|
142
|
|
|
for ($i = 0; $i < $numberOfCalls; $i++) { |
143
|
|
|
$this->process($item, $next); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->getViolations()->shouldReturn(array_fill(1, $numberOfCalls, $list)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function it_tracks_lines_when_exceptions_are_thrown_during_process( |
150
|
|
|
Step $step, |
151
|
|
|
ValidatorInterface $validator, |
152
|
|
|
Constraint $constraint, |
153
|
|
|
ConstraintViolation $violation |
154
|
|
|
) |
155
|
|
|
{ |
156
|
|
|
$numberOfCalls = 3; |
157
|
|
|
$next = function() {}; |
158
|
|
|
$errorList = new ConstraintViolationList([$violation->getWrappedObject()]); |
|
|
|
|
159
|
|
|
$stepFunc = function($item) use ($step, $next) { |
160
|
|
|
return $step->process($item, $next); |
161
|
|
|
}; |
162
|
|
|
$item = ['foo' => 10]; |
163
|
|
|
|
164
|
|
|
$validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection')) |
|
|
|
|
165
|
|
|
->willReturn($errorList); |
166
|
|
|
|
167
|
|
|
$step->process($item, $next)->shouldNotBeCalled(); |
168
|
|
|
|
169
|
|
|
$this->throwExceptions(); |
170
|
|
|
$this->add('foo', $constraint)->shouldReturn($this); |
171
|
|
|
|
172
|
|
|
for ($i = 0; $i < $numberOfCalls; $i++) { |
173
|
|
|
$this->shouldThrow('Port\Exception\ValidationException')->duringProcess($item, $stepFunc); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->getViolations()->shouldReturn(array_fill(1, $numberOfCalls, $errorList)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.