1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toa\Component\Validator\Tests\Constraints; |
4
|
|
|
|
5
|
|
|
use Toa\Component\Validator\Constraints\Audio; |
6
|
|
|
use Toa\Component\Validator\Constraints\AudioValidator; |
7
|
|
|
use Toa\Component\Validator\Exception\ProviderException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* AudioValidatorTest |
11
|
|
|
* |
12
|
|
|
* @author Enrico Thies <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AudioValidatorTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
protected $context; |
17
|
|
|
protected $provider; |
18
|
|
|
protected $validator; |
19
|
|
|
protected $audio; |
20
|
|
|
|
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->audio = __DIR__.'/Fixtures/silence.mp3'; |
24
|
|
|
|
25
|
|
|
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext') |
26
|
|
|
->disableOriginalConstructor() |
27
|
|
|
->getMock(); |
28
|
|
|
|
29
|
|
|
$this->provider = $this->getMock('Toa\Component\Validator\Provider\AudioProviderInterface'); |
30
|
|
|
|
31
|
|
|
$this->provider |
32
|
|
|
->expects($this->any()) |
33
|
|
|
->method('getDuration') |
34
|
|
|
->with($this->audio) |
35
|
|
|
->will($this->returnValue(30.249796)); |
36
|
|
|
|
37
|
|
|
$this->validator = new AudioValidator($this->provider); |
38
|
|
|
$this->validator->initialize($this->context); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function tearDown() |
45
|
|
|
{ |
46
|
|
|
$this->context = null; |
47
|
|
|
$this->provider = null; |
48
|
|
|
$this->validator = null; |
49
|
|
|
$this->audio = null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function testNullIsValid() |
56
|
|
|
{ |
57
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
58
|
|
|
$this->provider->expects($this->never())->method('getDuration'); |
59
|
|
|
|
60
|
|
|
$this->validator->validate(null, new Audio()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function testEmptyStringIsValid() |
67
|
|
|
{ |
68
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
69
|
|
|
$this->provider->expects($this->never())->method('getDuration'); |
70
|
|
|
|
71
|
|
|
$this->validator->validate('', new Audio()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function testValidAudio() |
78
|
|
|
{ |
79
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
80
|
|
|
$this->provider->expects($this->once())->method('getDuration'); |
81
|
|
|
|
82
|
|
|
$this->validator->validate($this->audio, new Audio()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @test |
87
|
|
|
*/ |
88
|
|
|
public function testInvalidAudio() |
89
|
|
|
{ |
90
|
|
|
$this->context->expects($this->once()) |
91
|
|
|
->method('addViolation') |
92
|
|
|
->with( |
93
|
|
|
'myMessage' |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$this->provider |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('getDuration') |
99
|
|
|
->with($this->audio) |
100
|
|
|
->will($this->throwException(new ProviderException())); |
101
|
|
|
|
102
|
|
|
$constraint = new Audio( |
103
|
|
|
array( |
104
|
|
|
'formatNotDetectedMessage' => 'myMessage', |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->validator->validate($this->audio, $constraint); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @test |
113
|
|
|
*/ |
114
|
|
|
public function testValidDuration() |
115
|
|
|
{ |
116
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
117
|
|
|
$this->provider->expects($this->once())->method('getDuration'); |
118
|
|
|
|
119
|
|
|
$constraint = new Audio( |
120
|
|
|
array( |
121
|
|
|
'minDuration' => 1, |
122
|
|
|
'maxDuration' => 32, |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$this->validator->validate($this->audio, $constraint); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @test |
131
|
|
|
*/ |
132
|
|
|
public function testDurationTooSmall() |
133
|
|
|
{ |
134
|
|
|
$this->context->expects($this->once()) |
135
|
|
|
->method('addViolation') |
136
|
|
|
->with( |
137
|
|
|
'myMessage', |
138
|
|
|
array( |
139
|
|
|
'{{ duration }}' => '30.249796', |
140
|
|
|
'{{ min_duration }}' => '33', |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$constraint = new Audio( |
145
|
|
|
array( |
146
|
|
|
'minDuration' => 33, |
147
|
|
|
'minDurationMessage' => 'myMessage', |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$this->validator->validate($this->audio, $constraint); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
*/ |
157
|
|
|
public function testDurationTooBig() |
158
|
|
|
{ |
159
|
|
|
$this->context->expects($this->once()) |
160
|
|
|
->method('addViolation') |
161
|
|
|
->with( |
162
|
|
|
'myMessage', |
163
|
|
|
array( |
164
|
|
|
'{{ duration }}' => '30.249796', |
165
|
|
|
'{{ max_duration }}' => '3', |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$constraint = new Audio( |
170
|
|
|
array( |
171
|
|
|
'maxDuration' => 3, |
172
|
|
|
'maxDurationMessage' => 'myMessage', |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$this->validator->validate($this->audio, $constraint); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
181
|
|
|
*/ |
182
|
|
|
public function testInvalidMinDuration() |
183
|
|
|
{ |
184
|
|
|
$constraint = new Audio( |
185
|
|
|
array( |
186
|
|
|
'minDuration' => '1abc', |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->validator->validate($this->audio, $constraint); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
195
|
|
|
*/ |
196
|
|
|
public function testInvalidMaxDuration() |
197
|
|
|
{ |
198
|
|
|
$constraint = new Audio( |
199
|
|
|
array( |
200
|
|
|
'maxDuration' => '1abc', |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$this->validator->validate($this->audio, $constraint); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|