This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathInputTest.php
882 lines (747 loc) · 31.9 KB
/
InputTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\InputFilter;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\Filter\FilterChain;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputInterface;
use Zend\Validator\NotEmpty as NotEmptyValidator;
use Zend\Validator\ValidatorChain;
use Zend\Validator\ValidatorInterface;
/**
* @covers Zend\InputFilter\Input
*/
class InputTest extends TestCase
{
/**
* @var Input
*/
protected $input;
public function setUp()
{
$this->input = new Input('foo');
}
public function assertRequiredValidationErrorMessage(Input $input, $message = '')
{
$message = $message ?: 'Expected failure message for required input';
$message .= ';';
$messages = $input->getMessages();
$this->assertInternalType('array', $messages, $message . ' non-array messages array');
$notEmpty = new NotEmptyValidator();
$messageTemplates = $notEmpty->getOption('messageTemplates');
$this->assertSame([
NotEmptyValidator::IS_EMPTY => $messageTemplates[NotEmptyValidator::IS_EMPTY],
], $messages, $message . ' missing NotEmpty::IS_EMPTY key and/or contains additional messages');
}
public function testConstructorRequiresAName()
{
$this->assertEquals('foo', $this->input->getName());
}
public function testInputHasEmptyFilterChainByDefault()
{
$filters = $this->input->getFilterChain();
$this->assertInstanceOf(FilterChain::class, $filters);
$this->assertEquals(0, count($filters));
}
public function testInputHasEmptyValidatorChainByDefault()
{
$validators = $this->input->getValidatorChain();
$this->assertInstanceOf(ValidatorChain::class, $validators);
$this->assertEquals(0, count($validators));
}
public function testCanInjectFilterChain()
{
$chain = $this->createFilterChainMock();
$this->input->setFilterChain($chain);
$this->assertSame($chain, $this->input->getFilterChain());
}
public function testCanInjectValidatorChain()
{
$chain = $this->createValidatorChainMock();
$this->input->setValidatorChain($chain);
$this->assertSame($chain, $this->input->getValidatorChain());
}
public function testInputIsMarkedAsRequiredByDefault()
{
$this->assertTrue($this->input->isRequired());
}
public function testRequiredFlagIsMutable()
{
$this->input->setRequired(false);
$this->assertFalse($this->input->isRequired());
}
public function testInputDoesNotAllowEmptyValuesByDefault()
{
$this->assertFalse($this->input->allowEmpty());
}
public function testAllowEmptyFlagIsMutable()
{
$this->input->setAllowEmpty(true);
$this->assertTrue($this->input->allowEmpty());
}
public function testContinueIfEmptyFlagIsFalseByDefault()
{
$input = $this->input;
$this->assertFalse($input->continueIfEmpty());
}
public function testContinueIfEmptyFlagIsMutable()
{
$input = $this->input;
$input->setContinueIfEmpty(true);
$this->assertTrue($input->continueIfEmpty());
}
/**
* @dataProvider setValueProvider
*/
public function testSetFallbackValue($fallbackValue)
{
$input = $this->input;
$return = $input->setFallbackValue($fallbackValue);
$this->assertSame($input, $return, 'setFallbackValue() must return it self');
$this->assertEquals($fallbackValue, $input->getFallbackValue(), 'getFallbackValue() value not match');
$this->assertEquals(true, $input->hasFallback(), 'hasFallback() value not match');
}
/**
* @dataProvider fallbackValueVsIsValidProvider
*/
public function testFallbackValueVsIsValidRules($required, $fallbackValue, $originalValue, $isValid, $expectedValue)
{
$input = $this->input;
$input->setContinueIfEmpty(true);
$input->setRequired($required);
$input->setValidatorChain($this->createValidatorChainMock($isValid, $originalValue));
$input->setFallbackValue($fallbackValue);
$input->setValue($originalValue);
$this->assertTrue(
$input->isValid(),
'isValid() should be return always true when fallback value is set. Detail: ' .
json_encode($input->getMessages())
);
$this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid');
$this->assertSame($expectedValue, $input->getRawValue(), 'getRawValue() value not match');
$this->assertSame($expectedValue, $input->getValue(), 'getValue() value not match');
}
/**
* @dataProvider fallbackValueVsIsValidProvider
*/
public function testFallbackValueVsIsValidRulesWhenValueNotSet($required, $fallbackValue)
{
$expectedValue = $fallbackValue; // Should always return the fallback value
$input = $this->input;
$input->setContinueIfEmpty(true);
$input->setRequired($required);
$input->setValidatorChain($this->createValidatorChainMock(null));
$input->setFallbackValue($fallbackValue);
$this->assertTrue(
$input->isValid(),
'isValid() should be return always true when fallback value is set. Detail: ' .
json_encode($input->getMessages())
);
$this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid');
$this->assertSame($expectedValue, $input->getRawValue(), 'getRawValue() value not match');
$this->assertSame($expectedValue, $input->getValue(), 'getValue() value not match');
}
public function testRequiredWithoutFallbackAndValueNotSetThenFail()
{
$input = $this->input;
$input->setRequired(true);
$this->assertFalse(
$input->isValid(),
'isValid() should be return always false when no fallback value, is required, and not data is set.'
);
$this->assertRequiredValidationErrorMessage($input);
}
public function testRequiredWithoutFallbackAndValueNotSetThenFailReturnsCustomErrorMessageWhenSet()
{
$input = $this->input;
$input->setRequired(true);
$input->setErrorMessage('FAILED TO VALIDATE');
$this->assertFalse(
$input->isValid(),
'isValid() should be return always false when no fallback value, is required, and not data is set.'
);
$this->assertSame(['FAILED TO VALIDATE'], $input->getMessages());
}
/**
* @group 28
* @group 60
*/
public function testRequiredWithoutFallbackAndValueNotSetProvidesNotEmptyValidatorIsEmptyErrorMessage()
{
$input = $this->input;
$input->setRequired(true);
$this->assertFalse(
$input->isValid(),
'isValid() should always return false when no fallback value is present, '
. 'the input is required, and no data is set.'
);
$this->assertRequiredValidationErrorMessage($input);
}
/**
* @group 28
* @group 60
*/
public function testRequiredWithoutFallbackAndValueNotSetProvidesCustomErrorMessageWhenSet()
{
$input = $this->input;
$input->setRequired(true);
$input->setErrorMessage('FAILED TO VALIDATE');
$this->assertFalse(
$input->isValid(),
'isValid() should always return false when no fallback value is present, '
. 'the input is required, and no data is set.'
);
$this->assertSame(['FAILED TO VALIDATE'], $input->getMessages());
}
public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()
{
$input = $this->input;
$input->setRequired(false);
$input->setAllowEmpty(false);
$input->setContinueIfEmpty(true);
// Validator should not to be called
$input->getValidatorChain()
->attach($this->createValidatorMock(null, null))
;
$this->assertTrue(
$input->isValid(),
'isValid() should be return always true when is not required, and no data is set. Detail: ' .
json_encode($input->getMessages())
);
$this->assertEquals([], $input->getMessages(), 'getMessages() should be empty because the input is valid');
}
/**
* @dataProvider emptyValueProvider
*/
public function testNotEmptyValidatorNotInjectedIfContinueIfEmptyIsTrue($value)
{
$input = $this->input;
$input->setContinueIfEmpty(true);
$input->setValue($value);
$input->isValid();
$validators = $input->getValidatorChain()
->getValidators();
$this->assertEmpty($validators);
}
public function testDefaultGetValue()
{
$this->assertNull($this->input->getValue());
}
public function testValueMayBeInjected()
{
$valueRaw = $this->getDummyValue();
$this->input->setValue($valueRaw);
$this->assertEquals($valueRaw, $this->input->getValue());
}
public function testRetrievingValueFiltersTheValue()
{
$valueRaw = $this->getDummyValue();
$valueFiltered = $this->getDummyValue(false);
$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
$this->input->setFilterChain($filterChain);
$this->input->setValue($valueRaw);
$this->assertSame($valueFiltered, $this->input->getValue());
}
public function testCanRetrieveRawValue()
{
$valueRaw = $this->getDummyValue();
$filterChain = $this->createFilterChainMock();
$this->input->setFilterChain($filterChain);
$this->input->setValue($valueRaw);
$this->assertEquals($valueRaw, $this->input->getRawValue());
}
public function testValidationOperatesOnFilteredValue()
{
$valueRaw = $this->getDummyValue();
$valueFiltered = $this->getDummyValue(false);
$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
$validatorChain = $this->createValidatorChainMock(true, $valueFiltered);
$this->input->setAllowEmpty(true);
$this->input->setFilterChain($filterChain);
$this->input->setValidatorChain($validatorChain);
$this->input->setValue($valueRaw);
$this->assertTrue(
$this->input->isValid(),
'isValid() value not match. Detail . ' . json_encode($this->input->getMessages())
);
}
public function testBreakOnFailureFlagIsOffByDefault()
{
$this->assertFalse($this->input->breakOnFailure());
}
public function testBreakOnFailureFlagIsMutable()
{
$this->input->setBreakOnFailure(true);
$this->assertTrue($this->input->breakOnFailure());
}
/**
* @dataProvider emptyValueProvider
*/
public function testNotEmptyValidatorAddedWhenIsValidIsCalled($value)
{
$this->assertTrue($this->input->isRequired());
$this->input->setValue($value);
$validatorChain = $this->input->getValidatorChain();
$this->assertEquals(0, count($validatorChain->getValidators()));
$this->assertFalse($this->input->isValid());
$messages = $this->input->getMessages();
$this->assertArrayHasKey('isEmpty', $messages);
$this->assertEquals(1, count($validatorChain->getValidators()));
// Assert that NotEmpty validator wasn't added again
$this->assertFalse($this->input->isValid());
$this->assertEquals(1, count($validatorChain->getValidators()));
}
/**
* @dataProvider emptyValueProvider
*/
public function testRequiredNotEmptyValidatorNotAddedWhenOneExists($value)
{
$this->input->setRequired(true);
$this->input->setValue($value);
$notEmptyMock = $this->createNonEmptyValidatorMock(false, $value);
$validatorChain = $this->input->getValidatorChain();
$validatorChain->prependValidator($notEmptyMock);
$this->assertFalse($this->input->isValid());
$validators = $validatorChain->getValidators();
$this->assertEquals(1, count($validators));
$this->assertEquals($notEmptyMock, $validators[0]['instance']);
}
/**
* @dataProvider emptyValueProvider
*/
public function testDoNotInjectNotEmptyValidatorIfAnywhereInChain($valueRaw, $valueFiltered)
{
$filterChain = $this->createFilterChainMock($valueRaw, $valueFiltered);
$validatorChain = $this->input->getValidatorChain();
$this->input->setRequired(true);
$this->input->setFilterChain($filterChain);
$this->input->setValue($valueRaw);
$notEmptyMock = $this->createNonEmptyValidatorMock(false, $valueFiltered);
$validatorChain->attach($this->createValidatorMock(true));
$validatorChain->attach($notEmptyMock);
$this->assertFalse($this->input->isValid());
$validators = $validatorChain->getValidators();
$this->assertEquals(2, count($validators));
$this->assertEquals($notEmptyMock, $validators[1]['instance']);
}
/**
* @group 7448
* @dataProvider isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider
*/
public function testIsRequiredVsAllowEmptyVsContinueIfEmptyVsIsValid(
$required,
$allowEmpty,
$continueIfEmpty,
$validator,
$value,
$expectedIsValid,
$expectedMessages
) {
$this->input->setRequired($required);
$this->input->setAllowEmpty($allowEmpty);
$this->input->setContinueIfEmpty($continueIfEmpty);
$this->input->getValidatorChain()
->attach($validator)
;
$this->input->setValue($value);
$this->assertEquals(
$expectedIsValid,
$this->input->isValid(),
'isValid() value not match. Detail: ' . json_encode($this->input->getMessages())
);
$this->assertEquals($expectedMessages, $this->input->getMessages(), 'getMessages() value not match');
$this->assertEquals($value, $this->input->getRawValue(), 'getRawValue() must return the value always');
$this->assertEquals($value, $this->input->getValue(), 'getValue() must return the filtered value always');
}
/**
* @dataProvider setValueProvider
*/
public function testSetValuePutInputInTheDesiredState($value)
{
$input = $this->input;
$this->assertFalse($input->hasValue(), 'Input should not have value by default');
$input->setValue($value);
$this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set");
}
/**
* @dataProvider setValueProvider
*/
public function testResetValueReturnsInputValueToDefaultValue($value)
{
$input = $this->input;
$originalInput = clone $input;
$this->assertFalse($input->hasValue(), 'Input should not have value by default');
$input->setValue($value);
$this->assertTrue($input->hasValue(), "hasValue() didn't return true when value was set");
$return = $input->resetValue();
$this->assertSame($input, $return, 'resetValue() must return itself');
$this->assertEquals($originalInput, $input, 'Input was not reset to the default value state');
}
public function testMerge()
{
$sourceRawValue = $this->getDummyValue();
$source = $this->createInputInterfaceMock();
$source->method('getName')->willReturn('bazInput');
$source->method('getErrorMessage')->willReturn('bazErrorMessage');
$source->method('breakOnFailure')->willReturn(true);
$source->method('isRequired')->willReturn(true);
$source->method('getRawValue')->willReturn($sourceRawValue);
$source->method('getFilterChain')->willReturn($this->createFilterChainMock());
$source->method('getValidatorChain')->willReturn($this->createValidatorChainMock());
$targetFilterChain = $this->createFilterChainMock();
$targetFilterChain->expects(TestCase::once())
->method('merge')
->with($source->getFilterChain())
;
$targetValidatorChain = $this->createValidatorChainMock();
$targetValidatorChain->expects(TestCase::once())
->method('merge')
->with($source->getValidatorChain())
;
$target = $this->input;
$target->setName('fooInput');
$target->setErrorMessage('fooErrorMessage');
$target->setBreakOnFailure(false);
$target->setRequired(false);
$target->setFilterChain($targetFilterChain);
$target->setValidatorChain($targetValidatorChain);
$return = $target->merge($source);
$this->assertSame($target, $return, 'merge() must return it self');
$this->assertEquals('bazInput', $target->getName(), 'getName() value not match');
$this->assertEquals('bazErrorMessage', $target->getErrorMessage(), 'getErrorMessage() value not match');
$this->assertEquals(true, $target->breakOnFailure(), 'breakOnFailure() value not match');
$this->assertEquals(true, $target->isRequired(), 'isRequired() value not match');
$this->assertEquals($sourceRawValue, $target->getRawValue(), 'getRawValue() value not match');
$this->assertTrue($target->hasValue(), 'hasValue() value not match');
}
/**
* Specific Input::merge extras
*/
public function testInputMergeWithoutValues()
{
$source = new Input();
$source->setContinueIfEmpty(true);
$this->assertFalse($source->hasValue(), 'Source should not have a value');
$target = $this->input;
$target->setContinueIfEmpty(false);
$this->assertFalse($target->hasValue(), 'Target should not have a value');
$return = $target->merge($source);
$this->assertSame($target, $return, 'merge() must return it self');
$this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match');
$this->assertFalse($target->hasValue(), 'hasValue() value not match');
}
/**
* Specific Input::merge extras
*/
public function testInputMergeWithSourceValue()
{
$source = new Input();
$source->setContinueIfEmpty(true);
$source->setValue(['foo']);
$target = $this->input;
$target->setContinueIfEmpty(false);
$this->assertFalse($target->hasValue(), 'Target should not have a value');
$return = $target->merge($source);
$this->assertSame($target, $return, 'merge() must return it self');
$this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match');
$this->assertEquals(['foo'], $target->getRawValue(), 'getRawValue() value not match');
$this->assertTrue($target->hasValue(), 'hasValue() value not match');
}
/**
* Specific Input::merge extras
*/
public function testInputMergeWithTargetValue()
{
$source = new Input();
$source->setContinueIfEmpty(true);
$this->assertFalse($source->hasValue(), 'Source should not have a value');
$target = $this->input;
$target->setContinueIfEmpty(false);
$target->setValue(['foo']);
$return = $target->merge($source);
$this->assertSame($target, $return, 'merge() must return it self');
$this->assertEquals(true, $target->continueIfEmpty(), 'continueIfEmpty() value not match');
$this->assertEquals(['foo'], $target->getRawValue(), 'getRawValue() value not match');
$this->assertTrue($target->hasValue(), 'hasValue() value not match');
}
public function fallbackValueVsIsValidProvider()
{
$required = true;
$isValid = true;
$originalValue = 'fooValue';
$fallbackValue = 'fooFallbackValue';
// @codingStandardsIgnoreStart
return [
// Description => [$inputIsRequired, $fallbackValue, $originalValue, $isValid, $expectedValue]
'Required: T, Input: Invalid. getValue: fallback' => [ $required, $fallbackValue, $originalValue, !$isValid, $fallbackValue],
'Required: T, Input: Valid. getValue: original' => [ $required, $fallbackValue, $originalValue, $isValid, $originalValue],
'Required: F, Input: Invalid. getValue: fallback' => [!$required, $fallbackValue, $originalValue, !$isValid, $fallbackValue],
'Required: F, Input: Valid. getValue: original' => [!$required, $fallbackValue, $originalValue, $isValid, $originalValue],
];
// @codingStandardsIgnoreEnd
}
public function setValueProvider()
{
$emptyValues = $this->emptyValueProvider();
$mixedValues = $this->mixedValueProvider();
$values = array_merge($emptyValues, $mixedValues);
return $values;
}
public function isRequiredVsAllowEmptyVsContinueIfEmptyVsIsValidProvider()
{
$allValues = $this->setValueProvider();
$emptyValues = $this->emptyValueProvider();
$nonEmptyValues = array_diff_key($allValues, $emptyValues);
$isRequired = true;
$aEmpty = true;
$cIEmpty = true;
$isValid = true;
$validatorMsg = ['FooValidator' => 'Invalid Value'];
$notEmptyMsg = ['isEmpty' => "Value is required and can't be empty"];
$validatorNotCall = function ($value, $context = null) {
return $this->createValidatorMock(null, $value, $context);
};
$validatorInvalid = function ($value, $context = null) use ($validatorMsg) {
return $this->createValidatorMock(false, $value, $context, $validatorMsg);
};
$validatorValid = function ($value, $context = null) {
return $this->createValidatorMock(true, $value, $context);
};
// @codingStandardsIgnoreStart
$dataTemplates=[
// Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validator, [$values], $expectedIsValid, $expectedMessages]
'Required: T; AEmpty: T; CIEmpty: T; Validator: T' => [ $isRequired, $aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []],
'Required: T; AEmpty: T; CIEmpty: T; Validator: F' => [ $isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg],
'Required: T; AEmpty: T; CIEmpty: F; Validator: X, Value: Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []],
'Required: T; AEmpty: T; CIEmpty: F; Validator: T, Value: Not Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []],
'Required: T; AEmpty: T; CIEmpty: F; Validator: F, Value: Not Empty' => [ $isRequired, $aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg],
'Required: T; AEmpty: F; CIEmpty: T; Validator: T' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []],
'Required: T; AEmpty: F; CIEmpty: T; Validator: F' => [ $isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg],
'Required: T; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , !$isValid, $notEmptyMsg],
'Required: T; AEmpty: F; CIEmpty: F; Validator: T, Value: Not Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []],
'Required: T; AEmpty: F; CIEmpty: F; Validator: F, Value: Not Empty' => [ $isRequired, !$aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg],
'Required: F; AEmpty: T; CIEmpty: T; Validator: T' => [!$isRequired, $aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []],
'Required: F; AEmpty: T; CIEmpty: T; Validator: F' => [!$isRequired, $aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg],
'Required: F; AEmpty: T; CIEmpty: F; Validator: X, Value: Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []],
'Required: F; AEmpty: T; CIEmpty: F; Validator: T, Value: Not Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []],
'Required: F; AEmpty: T; CIEmpty: F; Validator: F, Value: Not Empty' => [!$isRequired, $aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg],
'Required: F; AEmpty: F; CIEmpty: T; Validator: T' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorValid , $allValues , $isValid, []],
'Required: F; AEmpty: F; CIEmpty: T; Validator: F' => [!$isRequired, !$aEmpty, $cIEmpty, $validatorInvalid, $allValues , !$isValid, $validatorMsg],
'Required: F; AEmpty: F; CIEmpty: F; Validator: X, Value: Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorNotCall, $emptyValues , $isValid, []],
'Required: F; AEmpty: F; CIEmpty: F; Validator: T, Value: Not Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorValid , $nonEmptyValues, $isValid, []],
'Required: F; AEmpty: F; CIEmpty: F; Validator: F, Value: Not Empty' => [!$isRequired, !$aEmpty, !$cIEmpty, $validatorInvalid, $nonEmptyValues, !$isValid, $validatorMsg],
];
// @codingStandardsIgnoreEnd
// Expand data template matrix for each possible input value.
// Description => [$isRequired, $allowEmpty, $continueIfEmpty, $validator, $value, $expectedIsValid]
$dataSets = [];
foreach ($dataTemplates as $dataTemplateDescription => $dataTemplate) {
foreach ($dataTemplate[4] as $valueDescription => $value) {
$tmpTemplate = $dataTemplate;
$tmpTemplate[3] = $dataTemplate[3]($value['filtered']); // Get validator mock for each data set
$tmpTemplate[4] = $value['raw']; // expand value
$dataSets[$dataTemplateDescription . ' / ' . $valueDescription] = $tmpTemplate;
}
}
return $dataSets;
}
public function emptyValueProvider()
{
return [
// Description => [$value]
'null' => [
'raw' => null,
'filtered' => null,
],
'""' => [
'raw' => '',
'filtered' => '',
],
// '"0"' => ['0'],
// '0' => [0],
// '0.0' => [0.0],
// 'false' => [false],
'[]' => [
'raw' => [],
'filtered' => [],
],
];
}
public function mixedValueProvider()
{
return [
// Description => [$value]
'"0"' => [
'raw' => '0',
'filtered' => '0',
],
'0' => [
'raw' => 0,
'filtered' => 0,
],
'0.0' => [
'raw' => 0.0,
'filtered' => 0.0,
],
// TODO enable me
// 'false' => [
// 'raw' => false,
// 'filtered' => false,
// ],
'php' => [
'raw' => 'php',
'filtered' => 'php',
],
// TODO enable me
// 'whitespace' => [
// 'raw' => ' ',
// 'filtered' => ' ',
// ],
'1' => [
'raw' => 1,
'filtered' => 1,
],
'1.0' => [
'raw' => 1.0,
'filtered' => 1.0,
],
'true' => [
'raw' => true,
'filtered' => true,
],
'["php"]' => [
'raw' => ['php'],
'filtered' => ['php'],
],
'object' => [
'raw' => new stdClass(),
'filtered' => new stdClass(),
],
// @codingStandardsIgnoreStart
// TODO Skip HHVM failure enable me
// 'callable' => [
// 'raw' => function () {},
// 'filtered' => function () {},
// ],
// @codingStandardsIgnoreEnd
];
}
/**
* @return InputInterface|MockObject
*/
protected function createInputInterfaceMock()
{
/** @var InputInterface|MockObject $source */
$source = $this->getMock(InputInterface::class);
return $source;
}
/**
* @param mixed $valueRaw
* @param mixed $valueFiltered
*
* @return FilterChain|MockObject
*/
protected function createFilterChainMock($valueRaw = null, $valueFiltered = null)
{
/** @var FilterChain|MockObject $filterChain */
$filterChain = $this->getMock(FilterChain::class);
$filterChain->method('filter')
->with($valueRaw)
->willReturn($valueFiltered)
;
return $filterChain;
}
/**
* @param null|bool $isValid If set stub isValid method for return the argument value.
* @param mixed $value
* @param mixed $context
* @param string[] $messages
*
* @return ValidatorChain|MockObject
*/
protected function createValidatorChainMock($isValid = null, $value = null, $context = null, $messages = [])
{
/** @var ValidatorChain|MockObject $validatorChain */
$validatorChain = $this->getMock(ValidatorChain::class);
if (($isValid === false) || ($isValid === true)) {
$validatorChain->expects($this->once())
->method('isValid')
->with($value, $context)
->willReturn($isValid)
;
} else {
$validatorChain->expects($this->never())
->method('isValid')
->with($value, $context)
;
}
$validatorChain->method('getMessages')
->willReturn($messages)
;
return $validatorChain;
}
/**
* @param null|bool $isValid
* @param mixed $value
* @param mixed $context
* @param string[] $messages
*
* @return ValidatorInterface|MockObject
*/
protected function createValidatorMock($isValid, $value = 'not-set', $context = null, $messages = [])
{
/** @var ValidatorInterface|MockObject $validator */
$validator = $this->getMock(ValidatorInterface::class);
if (($isValid === false) || ($isValid === true)) {
$isValidMethod = $validator->expects($this->once())
->method('isValid')
->willReturn($isValid)
;
} else {
$isValidMethod = $validator->expects($this->never())
->method('isValid')
;
}
if ($value !== 'not-set') {
$isValidMethod->with($value, $context);
}
$validator->method('getMessages')
->willReturn($messages)
;
return $validator;
}
/**
* @param bool $isValid
* @param mixed $value
* @param mixed $context
*
* @return NotEmptyValidator|MockObject
*/
protected function createNonEmptyValidatorMock($isValid, $value, $context = null)
{
/** @var NotEmptyValidator|MockObject $notEmptyMock */
$notEmptyMock = $this->getMock(NotEmptyValidator::class, ['isValid']);
$notEmptyMock->expects($this->once())
->method('isValid')
->with($value, $context)
->willReturn($isValid)
;
return $notEmptyMock;
}
protected function getDummyValue($raw = true)
{
if ($raw) {
return 'foo';
} else {
return 'filtered';
}
}
}