VideoValidatorTest::testInvalidVideo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Toa\Component\Validator\Tests\Constraints;
4
5
use Toa\Component\Validator\Constraints\Video;
6
use Toa\Component\Validator\Constraints\VideoValidator;
7
use Toa\Component\Validator\Exception\ProviderException;
8
9
/**
10
 * VideoValidatorTest
11
 *
12
 * @author Enrico Thies <[email protected]>
13
 */
14
class VideoValidatorTest extends \PHPUnit_Framework_TestCase
15
{
16
    protected $context;
17
    protected $provider;
18
    protected $validator;
19
    protected $video;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function setUp()
25
    {
26
        $this->video = __DIR__.'/Fixtures/white.m4v';
27
28
        $this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
29
            ->disableOriginalConstructor()
30
            ->getMock();
31
32
        $this->provider = $this->getMock('Toa\Component\Validator\Provider\VideoProviderInterface');
33
34
        $this->provider
35
            ->expects($this->any())
36
            ->method('getHeight')
37
            ->with($this->video)
38
            ->will($this->returnValue(240));
39
40
        $this->provider
41
            ->expects($this->any())
42
            ->method('getWidth')
43
            ->will($this->returnValue(480));
44
45
        $this->validator = new VideoValidator($this->provider);
46
        $this->validator->initialize($this->context);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function tearDown()
53
    {
54
        $this->context = null;
55
        $this->provider = null;
56
        $this->validator = null;
57
        $this->video = null;
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function testNullIsValid()
64
    {
65
        $this->context->expects($this->never())->method('addViolation');
66
67
        $this->provider->expects($this->never())->method('getHeight');
68
        $this->provider->expects($this->never())->method('getWidth');
69
70
        $this->validator->validate(null, new Video());
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function testEmptyStringIsValid()
77
    {
78
        $this->context->expects($this->never())->method('addViolation');
79
80
        $this->provider->expects($this->never())->method('getHeight');
81
        $this->provider->expects($this->never())->method('getWidth');
82
83
        $this->validator->validate('', new Video());
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function testValidVideo()
90
    {
91
        $this->context->expects($this->never())->method('addViolation');
92
93
        $this->provider->expects($this->once())->method('getHeight');
94
        $this->provider->expects($this->once())->method('getWidth');
95
96
        $this->validator->validate($this->video, new Video());
97
    }
98
99
    /**
100
     * @test
101
     */
102
    public function testInvalidVideo()
103
    {
104
        $this->context->expects($this->once())
105
            ->method('addViolation')
106
            ->with(
107
                'myMessage'
108
            );
109
110
        $this->provider
111
            ->expects($this->once())
112
            ->method('getHeight')
113
            ->with($this->video)
114
            ->will($this->throwException(new ProviderException()));
115
116
        $constraint = new Video(
117
            array(
118
                'formatNotDetectedMessage' => 'myMessage',
119
            )
120
        );
121
122
        $this->validator->validate($this->video, $constraint);
123
    }
124
125
    /**
126
     * @test
127
     */
128
    public function testValidSize()
129
    {
130
        $this->context->expects($this->never())->method('addViolation');
131
132
        $this->provider->expects($this->once())->method('getHeight');
133
        $this->provider->expects($this->once())->method('getWidth');
134
135
        $constraint = new Video(
136
            array(
137
                'minWidth' => 1,
138
                'maxWidth' => 480,
139
                'minHeight' => 1,
140
                'maxHeight' => 240,
141
            )
142
        );
143
144
        $this->validator->validate($this->video, $constraint);
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function testWidthTooSmall()
151
    {
152
        $this->context->expects($this->once())
153
            ->method('addViolation')
154
            ->with(
155
                'myMessage',
156
                array(
157
                    '{{ width }}' => '480',
158
                    '{{ min_width }}' => '640',
159
                )
160
            );
161
162
        $constraint = new Video(
163
            array(
164
                'minWidth' => 640,
165
                'minWidthMessage' => 'myMessage',
166
            )
167
        );
168
169
        $this->validator->validate($this->video, $constraint);
170
    }
171
172
    /**
173
     * @test
174
     */
175
    public function testWidthTooBig()
176
    {
177
        $this->context->expects($this->once())
178
            ->method('addViolation')
179
            ->with(
180
                'myMessage',
181
                array(
182
                    '{{ width }}' => '480',
183
                    '{{ max_width }}' => '1',
184
                )
185
            );
186
187
        $constraint = new Video(
188
            array(
189
                'maxWidth' => 1,
190
                'maxWidthMessage' => 'myMessage',
191
            )
192
        );
193
194
        $this->validator->validate($this->video, $constraint);
195
    }
196
197
    /**
198
     * @test
199
     */
200
    public function testHeightTooSmall()
201
    {
202
        $this->context->expects($this->once())
203
            ->method('addViolation')
204
            ->with(
205
                'myMessage',
206
                array(
207
                    '{{ height }}' => '240',
208
                    '{{ min_height }}' => '320',
209
                )
210
            );
211
212
        $constraint = new Video(
213
            array(
214
                'minHeight' => 320,
215
                'minHeightMessage' => 'myMessage',
216
            )
217
        );
218
219
        $this->validator->validate($this->video, $constraint);
220
    }
221
222
    /**
223
     * @test
224
     */
225
    public function testHeightTooBig()
226
    {
227
        $this->context->expects($this->once())
228
            ->method('addViolation')
229
            ->with(
230
                'myMessage',
231
                array(
232
                    '{{ height }}' => '240',
233
                    '{{ max_height }}' => '1',
234
                )
235
            );
236
237
        $constraint = new Video(
238
            array(
239
                'maxHeight' => 1,
240
                'maxHeightMessage' => 'myMessage',
241
            )
242
        );
243
244
        $this->validator->validate($this->video, $constraint);
245
    }
246
247
    /**
248
     * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
249
     */
250
    public function testInvalidMinWidth()
251
    {
252
        $constraint = new Video(
253
            array(
254
                'minWidth' => '1abc',
255
            )
256
        );
257
258
        $this->validator->validate($this->video, $constraint);
259
    }
260
261
    /**
262
     * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
263
     */
264
    public function testInvalidMaxWidth()
265
    {
266
        $constraint = new Video(
267
            array(
268
                'maxWidth' => '1abc',
269
            )
270
        );
271
272
        $this->validator->validate($this->video, $constraint);
273
    }
274
275
    /**
276
     * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
277
     */
278
    public function testInvalidMinHeight()
279
    {
280
        $constraint = new Video(
281
            array(
282
                'minHeight' => '1abc',
283
            )
284
        );
285
286
        $this->validator->validate($this->video, $constraint);
287
    }
288
289
    /**
290
     * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
291
     */
292
    public function testInvalidMaxHeight()
293
    {
294
        $constraint = new Video(
295
            array(
296
                'maxHeight' => '1abc',
297
            )
298
        );
299
300
        $this->validator->validate($this->video, $constraint);
301
    }
302
}
303