1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toa\Component\Validator\Tests\Constraints; |
4
|
|
|
|
5
|
|
|
use Toa\Component\Validator\Constraints\Csv; |
6
|
|
|
use Toa\Component\Validator\Constraints\CsvValidator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* CsvValidatorTest |
10
|
|
|
* |
11
|
|
|
* @author Enrico Thies <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CsvValidatorTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
protected $context; |
16
|
|
|
protected $provider; |
17
|
|
|
protected $validator; |
18
|
|
|
protected $csv; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext') |
26
|
|
|
->disableOriginalConstructor() |
27
|
|
|
->getMock(); |
28
|
|
|
|
29
|
|
|
$this->provider = $this->getMock('Toa\Component\Validator\Provider\CsvProviderInterface'); |
30
|
|
|
|
31
|
|
|
$this->provider |
32
|
|
|
->expects($this->any()) |
33
|
|
|
->method('countRows') |
34
|
|
|
->will($this->returnValue(4)); |
35
|
|
|
|
36
|
|
|
$this->provider |
37
|
|
|
->expects($this->any()) |
38
|
|
|
->method('collectColumnSizes') |
39
|
|
|
->will( |
40
|
|
|
$this->returnValue( |
41
|
|
|
array( |
42
|
|
|
3 => array(1, 2, 4), |
43
|
|
|
0 => array(3), |
44
|
|
|
) |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->validator = new CsvValidator($this->provider); |
49
|
|
|
$this->validator->initialize($this->context); |
50
|
|
|
|
51
|
|
|
$this->csv = __DIR__.'/Fixtures/test.csv'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function tearDown() |
58
|
|
|
{ |
59
|
|
|
$this->context = null; |
60
|
|
|
$this->provider = null; |
61
|
|
|
$this->validator = null; |
62
|
|
|
$this->video = null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function testNullIsValid() |
69
|
|
|
{ |
70
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
71
|
|
|
|
72
|
|
|
$this->provider->expects($this->never())->method('countRows'); |
73
|
|
|
$this->provider->expects($this->never())->method('collectColumnSizes'); |
74
|
|
|
|
75
|
|
|
$this->validator->validate(null, new Csv()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function testEmptyStringIsValid() |
82
|
|
|
{ |
83
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
84
|
|
|
|
85
|
|
|
$this->provider->expects($this->never())->method('countRows'); |
86
|
|
|
$this->provider->expects($this->never())->method('collectColumnSizes'); |
87
|
|
|
|
88
|
|
|
$this->validator->validate('', new Csv()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function testValidCsv() |
95
|
|
|
{ |
96
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
97
|
|
|
|
98
|
|
|
$this->provider->expects($this->once())->method('countRows'); |
99
|
|
|
$this->provider->expects($this->once())->method('collectColumnSizes'); |
100
|
|
|
|
101
|
|
|
$this->validator->validate($this->csv, new Csv()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @test |
106
|
|
|
*/ |
107
|
|
|
public function testValidSize() |
108
|
|
|
{ |
109
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
110
|
|
|
|
111
|
|
|
$constraint = new Csv( |
112
|
|
|
array( |
113
|
|
|
'minColumns' => 1, |
114
|
|
|
'maxColumns' => 3, |
115
|
|
|
'minRows' => 1, |
116
|
|
|
'maxRows' => 4, |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->validator->validate($this->csv, $constraint); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @test |
125
|
|
|
*/ |
126
|
|
|
public function testRowsTooSmall() |
127
|
|
|
{ |
128
|
|
|
$constraint = new Csv( |
129
|
|
|
array( |
130
|
|
|
'minRows' => 5, |
131
|
|
|
'minRowsMessage' => 'myMessage', |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->context->expects($this->once()) |
136
|
|
|
->method('addViolation') |
137
|
|
|
->with( |
138
|
|
|
'myMessage', |
139
|
|
|
array( |
140
|
|
|
'{{ min_rows }}' => '5', |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->validator->validate($this->csv, $constraint); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @test |
149
|
|
|
*/ |
150
|
|
|
public function testRowsTooBig() |
151
|
|
|
{ |
152
|
|
|
$constraint = new Csv( |
153
|
|
|
array( |
154
|
|
|
'maxRows' => 1, |
155
|
|
|
'maxRowsMessage' => 'myMessage', |
156
|
|
|
) |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$this->context->expects($this->once()) |
160
|
|
|
->method('addViolation') |
161
|
|
|
->with( |
162
|
|
|
'myMessage', |
163
|
|
|
array( |
164
|
|
|
'{{ max_rows }}' => '1', |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->validator->validate($this->csv, $constraint); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
|
public function testColumnsTooSmall() |
175
|
|
|
{ |
176
|
|
|
$constraint = new Csv( |
177
|
|
|
array( |
178
|
|
|
'minColumns' => 5, |
179
|
|
|
'minColumnsMessage' => 'myMessage', |
180
|
|
|
) |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$this->context->expects($this->once()) |
184
|
|
|
->method('addViolation') |
185
|
|
|
->with( |
186
|
|
|
'myMessage', |
187
|
|
|
array( |
188
|
|
|
'{{ min_columns }}' => '5', |
189
|
|
|
'{{ occurrences }}' => '1,2,4', |
190
|
|
|
) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$this->validator->validate($this->csv, $constraint); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @test |
198
|
|
|
*/ |
199
|
|
|
public function testColumnsTooBig() |
200
|
|
|
{ |
201
|
|
|
$constraint = new Csv( |
202
|
|
|
array( |
203
|
|
|
'maxColumns' => 1, |
204
|
|
|
'maxColumnsMessage' => 'myMessage', |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$this->context->expects($this->once()) |
209
|
|
|
->method('addViolation') |
210
|
|
|
->with( |
211
|
|
|
'myMessage', |
212
|
|
|
array( |
213
|
|
|
'{{ max_columns }}' => '1', |
214
|
|
|
'{{ occurrences }}' => '1,2,4', |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$this->validator->validate($this->csv, $constraint); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @test |
223
|
|
|
*/ |
224
|
|
|
public function testEmptyColumns() |
225
|
|
|
{ |
226
|
|
|
$constraint = new Csv( |
227
|
|
|
array( |
228
|
|
|
'ignoreEmptyColumns' => false, |
229
|
|
|
'emptyColumnsMessage' => 'myMessage', |
230
|
|
|
) |
231
|
|
|
); |
232
|
|
|
|
233
|
|
|
$this->context->expects($this->once()) |
234
|
|
|
->method('addViolation') |
235
|
|
|
->with( |
236
|
|
|
'myMessage', |
237
|
|
|
array( |
238
|
|
|
'{{ occurrences }}' => '3', |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$this->validator->validate($this->csv, $constraint); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
247
|
|
|
*/ |
248
|
|
|
public function testInvalidMinRows() |
249
|
|
|
{ |
250
|
|
|
$constraint = new Csv( |
251
|
|
|
array( |
252
|
|
|
'minRows' => '1abc', |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$this->validator->validate($this->csv, $constraint); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
261
|
|
|
*/ |
262
|
|
|
public function testInvalidMaxRows() |
263
|
|
|
{ |
264
|
|
|
$constraint = new Csv( |
265
|
|
|
array( |
266
|
|
|
'maxRows' => '1abc', |
267
|
|
|
) |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$this->validator->validate($this->csv, $constraint); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
275
|
|
|
*/ |
276
|
|
|
public function testInvalidMinColumns() |
277
|
|
|
{ |
278
|
|
|
$constraint = new Csv( |
279
|
|
|
array( |
280
|
|
|
'minColumns' => '1abc', |
281
|
|
|
) |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
$this->validator->validate($this->csv, $constraint); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
289
|
|
|
*/ |
290
|
|
|
public function testInvalidMaxColumns() |
291
|
|
|
{ |
292
|
|
|
$constraint = new Csv( |
293
|
|
|
array( |
294
|
|
|
'maxColumns' => '1abc', |
295
|
|
|
) |
296
|
|
|
); |
297
|
|
|
|
298
|
|
|
$this->validator->validate($this->csv, $constraint); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
303
|
|
|
*/ |
304
|
|
|
public function testInvalidDelimiter() |
305
|
|
|
{ |
306
|
|
|
$constraint = new Csv( |
307
|
|
|
array( |
308
|
|
|
'delimiter' => '1abc', |
309
|
|
|
) |
310
|
|
|
); |
311
|
|
|
|
312
|
|
|
$this->validator->validate($this->csv, $constraint); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
317
|
|
|
*/ |
318
|
|
|
public function testInvalidEnclosure() |
319
|
|
|
{ |
320
|
|
|
$constraint = new Csv( |
321
|
|
|
array( |
322
|
|
|
'enclosure' => '1abc', |
323
|
|
|
) |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$this->validator->validate($this->csv, $constraint); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
331
|
|
|
*/ |
332
|
|
|
public function testInvalidEscape() |
333
|
|
|
{ |
334
|
|
|
$constraint = new Csv( |
335
|
|
|
array( |
336
|
|
|
'escape' => '1abc', |
337
|
|
|
) |
338
|
|
|
); |
339
|
|
|
|
340
|
|
|
$this->validator->validate($this->csv, $constraint); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|