Completed
Push — master ( 0dcc6d...a8e4c5 )
by Markus
01:46
created

it_validates_multiple_items_from_metadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        $this->add('foo', $constraint)->shouldReturn($this);
45
46
        $this->process(
0 ignored issues
show
Bug introduced by
The method process() does not exist on spec\Port\Steps\Step\ValidatorStepSpec. Did you maybe mean it_processes_an_item()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        $this->add('foo', $constraint)->shouldReturn($this);
63
64
        $this->process(
0 ignored issues
show
Bug introduced by
The method process() does not exist on spec\Port\Steps\Step\ValidatorStepSpec. Did you maybe mean it_processes_an_item()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $violation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()]);
0 ignored issues
show
Bug introduced by
The method getWrappedObject() does not seem to exist on object<Symfony\Component...or\ConstraintViolation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        $validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection'))->willReturn($list);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
        $this->process(
0 ignored issues
show
Bug introduced by
The method process() does not exist on spec\Port\Steps\Step\ValidatorStepSpec. Did you maybe mean it_processes_an_item()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
142
        for ($i = 0; $i < $numberOfCalls; $i++) {
143
            $this->process($item, $next);
0 ignored issues
show
Bug introduced by
The method process() does not exist on spec\Port\Steps\Step\ValidatorStepSpec. Did you maybe mean it_processes_an_item()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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()]);
0 ignored issues
show
Bug introduced by
The method getWrappedObject() does not seem to exist on object<Symfony\Component...or\ConstraintViolation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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'))
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ViolationListInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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