BookingInputFilterTest::optionsProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 52
rs 9.4929
cc 1
eloc 32
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace JhFlexiTimeTest\InputFilter;
4
5
use JhFlexiTime\InputFilter\BookingInputFilter;
6
7
/**
8
 * Class BookingFilterTest
9
 * @package JhFlexiTimeTest\InputFilter
10
 * @author Aydin Hassan <[email protected]>
11
 */
12
class BookingInputFilterTest extends \PHPUnit_Framework_TestCase
13
{
14
15
    /**
16
     * @param array $input
17
     * @param array $output
18
     * @param array $messages
19
     * @dataProvider formDataProvider
20
     */
21
    public function testInputFilter(array $input, array $output = null, array $messages = null)
22
    {
23
24
        $mockValidator  = $this->getMockValidator(true);
25
        $mockOptions    = $this->getMock('JhFlexiTime\Options\BookingOptionsInterface');
26
        $filter         = new BookingInputFilter($mockValidator, $mockValidator, $mockOptions);
27
        $filter->setData($input);
28
29
30
        if ($output === null) {
31
            $this->assertFalse($filter->isValid(), 'Input must not be valid');
32
            $this->assertEquals($messages, $filter->getMessages());
33
        } else {
34
            $this->assertTrue($filter->isValid(), 'Input must be valid');
35
            $this->assertEquals($output, $filter->getValues());
36
        }
37
    }
38
39
    /**
40
     * @param bool $validates
41
     * @return \Zend\Validator\ValidatorInterface
42
     */
43
    protected function getMockValidator($validates)
44
    {
45
        $validator = $this->getMock('Zend\Validator\ValidatorInterface');
46
        $validator->expects($this->any())
47
            ->method('isValid')
48
            ->will($this->returnValue($validates));
49
50
        if (!$validates) {
51
            $validator->expects($this->any())
52
                ->method('getMessages')
53
                ->will($this->returnValue([]));
54
        }
55
56
        return $validator;
57
    }
58
59
    /**
60
     * @param array $input
61
     * @param array $messages
62
     * @param string $method
63
     * @param string $value
64
     * @dataProvider optionsProvider
65
     */
66
    public function testInputFilterFailsIfMinMaxTimeOptionsArePresent(
67
        array $input,
68
        array $messages,
69
        $method,
70
        $value
71
    ) {
72
        $mockValidator  = $this->getMockValidator(true);
73
        $mockOptions    = $this->getMock('JhFlexiTime\Options\BookingOptionsInterface');
74
        $mockOptions
75
            ->expects($this->exactly(2))
76
            ->method($method)
77
            ->will($this->returnValue($value));
78
79
        $filter = new BookingInputFilter($mockValidator, $mockValidator, $mockOptions);
80
        $filter->setData($input);
81
82
        $this->assertFalse($filter->isValid(), 'Input must not be valid');
83
        $this->assertEquals($messages, $filter->getMessages());
84
85
    }
86
87
    public function optionsProvider()
88
    {
89
        $input = [
90
            'user'      => 2,
91
            'date'      => '12-04-1988',
92
            'startTime' => '08:00',
93
            'endTime'   => '17:00',
94
            'notes'     => 'Some notes',
95
        ];
96
        return [
97
            'less-than-min-start-time' => [
98
                $input,
99
                [
100
                    'startTime' => [
101
                        'notGreaterThanInclusive' => 'The input is not greater or equal than \'10:00\'',
102
                    ],
103
                ],
104
                'getMinStartTime',
105
                '10:00',
106
            ],
107
            'greater-than-max-start-time' =>[
108
                $input,
109
                [
110
                    'startTime' => [
111
                        'notLessThanInclusive' => 'The input is not less or equal than \'07:00\'',
112
                    ],
113
                ],
114
                'getMaxStartTime',
115
                '07:00',
116
            ],
117
            'less-than-min-end-time' => [
118
                $input,
119
                [
120
                    'endTime' => [
121
                        'notGreaterThanInclusive' => 'The input is not greater or equal than \'17:15\'',
122
                    ],
123
                ],
124
                'getMinEndTime',
125
                '17:15',
126
            ],
127
            'greater-than-max-end-time' =>[
128
                $input,
129
                [
130
                    'endTime' => [
131
                        'notLessThanInclusive' => 'The input is not less or equal than \'16:45\'',
132
                    ],
133
                ],
134
                'getMaxEndTime',
135
                '16:45',
136
            ],
137
        ];
138
    }
139
140
    /**
141
     * @param array $input
142
     * @param string $method
143
     * @param string $value
144
     * @dataProvider optionsValidProvider
145
     */
146
    public function testInputFilterValidatesIfMinMaxTimeOptionsArePresent(array $input, $method, $value)
147
    {
148
        $mockValidator  = $this->getMockValidator(true);
149
        $mockOptions    = $this->getMock('JhFlexiTime\Options\BookingOptionsInterface');
150
        $mockOptions
151
            ->expects($this->exactly(2))
152
            ->method($method)
153
            ->will($this->returnValue($value));
154
155
        $filter = new BookingInputFilter($mockValidator, $mockValidator, $mockOptions);
156
        $filter->setData($input);
157
158
        $this->assertTrue($filter->isValid(), 'Input must not be valid');
159
160
    }
161
162
    public function optionsValidProvider()
163
    {
164
        $input = [
165
            'user'      => 2,
166
            'date'      => '12-04-1988',
167
            'startTime' => '08:00',
168
            'endTime'   => '17:00',
169
            'notes'     => 'Some notes',
170
        ];
171
        return [
172
            'less-than-min-start-time' => [
173
                $input,
174
                'getMinStartTime',
175
                '08:00',
176
            ],
177
            'greater-than-max-start-time' =>[
178
                $input,
179
                'getMaxStartTime',
180
                '08:00',
181
            ],
182
            'less-than-min-end-time' => [
183
                $input,
184
                'getMinEndTime',
185
                '17:00',
186
            ],
187
            'greater-than-max-end-time' =>[
188
                $input,
189
                'getMaxEndTime',
190
                '17:00',
191
            ],
192
        ];
193
    }
194
195
    /**
196
     * Valid & Invalid Datasets
197
     *
198
     * @return array
199
     */
200
    public function formDataProvider()
201
    {
202
        return [
203
            'completely-valid-input' => [
204
                [
205
                    'user'      => 2,
206
                    'date'      => '12-04-1988',
207
                    'startTime' => '08:00',
208
                    'endTime'   => '17:00',
209
                    'notes'     => 'Some notes',
210
                ],
211
                [
212
                    'user'      => 2,
213
                    'date'      => new \DateTime('12-04-1988'),
214
                    'startTime' => '08:00',
215
                    'endTime'   => '17:00',
216
                    'notes'     => 'Some notes',
217
                ],
218
                null,
219
            ],
220
            'valid-time-low-boundary' => [
221
                [
222
                    'user'      => 2,
223
                    'date'      => '12-04-1988',
224
                    'startTime' => '07:00',
225
                    'endTime'   => '16:00',
226
                    'notes'     => 'Some notes',
227
                ],
228
                [
229
                    'user'      => 2,
230
                    'date'      => new \DateTime('12-04-1988'),
231
                    'startTime' => '07:00',
232
                    'endTime'   => '16:00',
233
                    'notes'     => 'Some notes',
234
                ],
235
                null,
236
            ],
237
            'valid-time-high-boundary' => [
238
                [
239
                    'user'      => 2,
240
                    'date'      => '12-04-1988',
241
                    'startTime' => '10:00',
242
                    'endTime'   => '19:00',
243
                    'notes'     => 'Some notes',
244
                ],
245
                [
246
                    'user'      => 2,
247
                    'date'      => new \DateTime('12-04-1988'),
248
                    'startTime' => '10:00',
249
                    'endTime'   => '19:00',
250
                    'notes'     => 'Some notes',
251
                ],
252
                null,
253
            ],
254
            'space-padded-valid-input' => [
255
                [
256
                    'user'      => 2,
257
                    'date'      => '12-04-1988',
258
                    'startTime' => '07:00',
259
                    'endTime'   => '17:00',
260
                    'notes'     => '    Some notes   ',
261
                ],
262
                [
263
                    'user'      => 2,
264
                    'date'      => new \DateTime('12-04-1988'),
265
                    'startTime' => '07:00',
266
                    'endTime'   => '17:00',
267
                    'notes'     => 'Some notes',
268
                ],
269
                null,
270
            ],
271
            'invalid-date-format' => [
272
                [
273
                    'user'      => 2,
274
                    'date'      => '12-not-a-month-2014',
275
                    'startTime' => '07:00',
276
                    'endTime'   => '17:00',
277
                ],
278
                null,
279
                [
280
                    'date' => [
281
                        'dateInvalidDate' => 'The input does not appear to be a valid date',
282
                        'dateFalseFormat' => 'The input does not fit the date format \'d-m-Y\'',
283
                    ],
284
                ],
285
            ],
286
            'invalid-date' => [
287
                [
288
                    'user'      => 2,
289
                    'date'      => 'not-a-date',
290
                    'startTime' => '07:00',
291
                    'endTime'   => '17:00',
292
                ],
293
                null,
294
                [
295
                    'date' => [
296
                        'dateInvalidDate' => 'The input does not appear to be a valid date',
297
                    ],
298
                ],
299
            ],
300
            'greater-than-time' => [
301
                [
302
                    'user'      => 2,
303
                    'date'      => '12-04-1988',
304
                    'startTime' => '10:01',
305
                    'endTime'   => '19:01',
306
                ],
307
                null,
308
                [
309
                    'startTime' => [
310
                        //'notLessThanInclusive' => 'The input is not less or equal than \'10:00\'',
311
                        'dateStepNotStep' => 'The input is not a valid step',
312
                    ],
313
                    'endTime' => [
314
                        //'notLessThanInclusive' => 'The input is not less or equal than \'19:00\'',
315
                        'dateStepNotStep' => 'The input is not a valid step',
316
                    ],
317
                ],
318
            ],
319
            'invalid-time' => [
320
                [
321
                    'user'      => 2,
322
                    'date'      => '12-04-1988',
323
                    'startTime' => 'not-a-time',
324
                    'endTime'   => 'not-a-time',
325
                ],
326
                null,
327
                [
328
                    'startTime' => [
329
                        'dateInvalidDate' => 'The input does not appear to be a valid date',
330
                        //'notLessThanInclusive' => 'The input is not less or equal than \'10:00\'',
331
                    ],
332
                    'endTime' => [
333
                        'dateInvalidDate' => 'The input does not appear to be a valid date',
334
                        //'notLessThanInclusive' => 'The input is not less or equal than \'19:00\'',
335
                    ],
336
                ],
337
            ],
338
            'message-to-long' => [
339
                [
340
                    'user'      => 2,
341
                    'date'      => '12-04-1988',
342
                    'startTime' => '09:00',
343
                    'endTime'   => '17:00',
344
                    'notes'     => $this->getLongString()
345
                ],
346
                null,
347
                [
348
                    'notes' => [
349
                        'stringLengthTooLong' => 'The input is more than 512 characters long',
350
                    ],
351
                ],
352
            ],
353
            'invalid-time-step' => [
354
                [
355
                    'user'      => 2,
356
                    'date'      => '12-04-1988',
357
                    'startTime' => '09:04',
358
                    'endTime'   => '17:07',
359
                ],
360
                null,
361
                [
362
                    'startTime' => [
363
                        'dateStepNotStep' => 'The input is not a valid step',
364
                    ],
365
                    'endTime' => [
366
                        'dateStepNotStep' => 'The input is not a valid step',
367
                    ],
368
                ],
369
            ],
370
            'required-fields' => [
371
                [
372
                ],
373
                null,
374
                [
375
                    'user' => [
376
                        'isEmpty' => 'Value is required and can\'t be empty',
377
                    ],
378
                    'date' => [
379
                        'isEmpty' => 'Value is required and can\'t be empty',
380
                    ],
381
                    'startTime' => [
382
                        'isEmpty' => 'Value is required and can\'t be empty',
383
                    ],
384
                    'endTime' => [
385
                        'isEmpty' => 'Value is required and can\'t be empty',
386
                    ],
387
                ],
388
            ],
389
        ];
390
    }
391
392
    public function testStartTimeEndTimeSameTime()
393
    {
394
        $mockValidator  = $this->getMockValidator(true);
395
        $mockOptions    = $this->getMock('JhFlexiTime\Options\BookingOptionsInterface');
396
397
398
        $filter = new BookingInputFilter($mockValidator, $mockValidator, $mockOptions);
399
        $filter->setData([
400
            'user'      => 2,
401
            'date'      => '12-04-1988',
402
            'startTime' => '00:00',
403
            'endTime'   => '00:00',
404
            'notes'     => '',
405
        ]);
406
407
        $this->assertTrue($filter->isValid(), 'Input must not be valid');
408
    }
409
410
    /**
411
     * @return string
412
     */
413
    protected function getLongString()
414
    {
415
        $text = <<<EOT
416
  Long string which should fail the string length validator,
417
  this string should be longer than 512 characters to fail the validation
418
  of this filter component.
419
420
  Long string which should fail the string length validator,
421
  this string should be longer than 512 characters to fail the validation
422
  of this filter component.
423
424
  Long string which should fail the string length validator,
425
  this string should be longer than 512 characters to fail the validation
426
  of this filter component.
427
428
  Long string which should fail the string length validator,
429
  this string should be longer than 512 characters to fail the validation
430
  of this filter component.
431
EOT;
432
        return $text;
433
    }
434
}
435