@@ -491,12 +491,12 @@ public function testSetDeprecatedFailsIfInvalidDeprecationMessageType()
491
491
public function testLazyDeprecationFailsIfInvalidDeprecationMessageType ()
492
492
{
493
493
$ this ->resolver
494
- ->setDefault ('foo ' , true )
494
+ ->setDefined ('foo ' )
495
495
->setDeprecated ('foo ' , function (Options $ options , $ value ) {
496
496
return false ;
497
497
})
498
498
;
499
- $ this ->resolver ->resolve ();
499
+ $ this ->resolver ->resolve (array ( ' foo ' => null ) );
500
500
}
501
501
502
502
/**
@@ -506,16 +506,15 @@ public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
506
506
public function testFailsIfCyclicDependencyBetweenDeprecation ()
507
507
{
508
508
$ this ->resolver
509
- ->setDefault ('foo ' , null )
510
- ->setDefault ('bar ' , null )
509
+ ->setDefined (array ('foo ' , 'bar ' ))
511
510
->setDeprecated ('foo ' , function (Options $ options , $ value ) {
512
511
$ options ['bar ' ];
513
512
})
514
513
->setDeprecated ('bar ' , function (Options $ options , $ value ) {
515
514
$ options ['foo ' ];
516
515
})
517
516
;
518
- $ this ->resolver ->resolve ();
517
+ $ this ->resolver ->resolve (array ( ' foo ' => null , ' bar ' => null ) );
519
518
}
520
519
521
520
public function testIsDeprecated ()
@@ -539,10 +538,15 @@ public function testIsNotDeprecatedIfEmptyString()
539
538
/**
540
539
* @dataProvider provideDeprecationData
541
540
*/
542
- public function testDeprecationMessages (\Closure $ configureOptions , array $ options , ?array $ expectedError )
541
+ public function testDeprecationMessages (\Closure $ configureOptions , array $ options , ?array $ expectedError, int $ expectedCount )
543
542
{
543
+ $ count = 0 ;
544
544
error_clear_last ();
545
- set_error_handler (function () { return false ; });
545
+ set_error_handler (function () use (&$ count ) {
546
+ ++$ count ;
547
+
548
+ return false ;
549
+ });
546
550
$ e = error_reporting (0 );
547
551
548
552
$ configureOptions ($ this ->resolver );
@@ -555,6 +559,7 @@ public function testDeprecationMessages(\Closure $configureOptions, array $optio
555
559
unset($ lastError ['file ' ], $ lastError ['line ' ]);
556
560
557
561
$ this ->assertSame ($ expectedError , $ lastError );
562
+ $ this ->assertSame ($ expectedCount , $ count );
558
563
}
559
564
560
565
public function provideDeprecationData ()
@@ -571,6 +576,7 @@ function (OptionsResolver $resolver) {
571
576
'type ' => E_USER_DEPRECATED ,
572
577
'message ' => 'The option "foo" is deprecated. ' ,
573
578
),
579
+ 1 ,
574
580
);
575
581
576
582
yield 'It deprecates an option with custom message ' => array (
@@ -588,20 +594,27 @@ function (OptionsResolver $resolver) {
588
594
'type ' => E_USER_DEPRECATED ,
589
595
'message ' => 'The option "foo" is deprecated, use "bar" option instead. ' ,
590
596
),
597
+ 2 ,
591
598
);
592
599
593
- yield 'It deprecates a missing option with default value ' => array (
600
+ yield 'It deprecates an option evaluated in another definition ' => array (
594
601
function (OptionsResolver $ resolver ) {
602
+ // defined by superclass
595
603
$ resolver
596
- ->setDefaults ( array ( 'foo ' => null , ' bar ' => null ) )
604
+ ->setDefault ( 'foo ' , null )
597
605
->setDeprecated ('foo ' )
598
606
;
607
+ // defined by subclass
608
+ $ resolver ->setDefault ('bar ' , function (Options $ options ) {
609
+ return $ options ['foo ' ]; // It triggers a deprecation
610
+ });
599
611
},
600
- array (' bar ' => ' baz ' ),
612
+ array (),
601
613
array (
602
614
'type ' => E_USER_DEPRECATED ,
603
615
'message ' => 'The option "foo" is deprecated. ' ,
604
616
),
617
+ 1 ,
605
618
);
606
619
607
620
yield 'It deprecates allowed type and value ' => array (
@@ -623,20 +636,46 @@ function (OptionsResolver $resolver) {
623
636
'type ' => E_USER_DEPRECATED ,
624
637
'message ' => 'Passing an instance of "stdClass" to option "foo" is deprecated, pass its FQCN instead. ' ,
625
638
),
639
+ 1 ,
626
640
);
627
641
628
- yield 'It ignores deprecation for missing option without default value ' => array (
642
+ yield 'It triggers a deprecation based on the value only if option is provided by the user ' => array (
629
643
function (OptionsResolver $ resolver ) {
630
644
$ resolver
631
- ->setDefined (array ('foo ' , 'bar ' ))
632
- ->setDeprecated ('foo ' )
645
+ ->setDefined ('foo ' )
646
+ ->setAllowedTypes ('foo ' , array ('null ' , 'bool ' ))
647
+ ->setDeprecated ('foo ' , function (Options $ options , $ value ) {
648
+ if (!\is_bool ($ value )) {
649
+ return 'Passing a value different than true or false is deprecated. ' ;
650
+ }
651
+
652
+ return '' ;
653
+ })
654
+ ->setDefault ('baz ' , null )
655
+ ->setAllowedTypes ('baz ' , array ('null ' , 'int ' ))
656
+ ->setDeprecated ('baz ' , function (Options $ options , $ value ) {
657
+ if (!\is_int ($ value )) {
658
+ return 'Not passing an integer is deprecated. ' ;
659
+ }
660
+
661
+ return '' ;
662
+ })
663
+ ->setDefault ('bar ' , function (Options $ options ) {
664
+ $ options ['baz ' ]; // It does not triggers a deprecation
665
+
666
+ return $ options ['foo ' ]; // It does not triggers a deprecation
667
+ })
633
668
;
634
669
},
635
- array ('bar ' => 'baz ' ),
636
- null ,
670
+ array ('foo ' => null ), // It triggers a deprecation
671
+ array (
672
+ 'type ' => E_USER_DEPRECATED ,
673
+ 'message ' => 'Passing a value different than true or false is deprecated. ' ,
674
+ ),
675
+ 1 ,
637
676
);
638
677
639
- yield 'It ignores deprecation if closure returns an empty string ' => array (
678
+ yield 'It ignores a deprecation if closure returns an empty string ' => array (
640
679
function (OptionsResolver $ resolver ) {
641
680
$ resolver
642
681
->setDefault ('foo ' , null )
@@ -647,6 +686,7 @@ function (OptionsResolver $resolver) {
647
686
},
648
687
array ('foo ' => Bar::class),
649
688
null ,
689
+ 0 ,
650
690
);
651
691
652
692
yield 'It deprecates value depending on other option value ' => array (
@@ -668,6 +708,62 @@ function (OptionsResolver $resolver) {
668
708
'type ' => E_USER_DEPRECATED ,
669
709
'message ' => 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated. ' ,
670
710
),
711
+ 1 ,
712
+ );
713
+
714
+ yield 'It triggers a deprecation for each evaluation ' => array (
715
+ function (OptionsResolver $ resolver ) {
716
+ $ resolver
717
+ // defined by superclass
718
+ ->setDefined ('foo ' )
719
+ ->setDeprecated ('foo ' )
720
+ // defined by subclass
721
+ ->setDefault ('bar ' , function (Options $ options ) {
722
+ return $ options ['foo ' ]; // It triggers a deprecation
723
+ })
724
+ ->setNormalizer ('bar ' , function (Options $ options , $ value ) {
725
+ $ options ['foo ' ]; // It triggers a deprecation
726
+ $ options ['foo ' ]; // It triggers a deprecation
727
+
728
+ return $ value ;
729
+ })
730
+ ;
731
+ },
732
+ array ('foo ' => 'baz ' ), // It triggers a deprecation
733
+ array (
734
+ 'type ' => E_USER_DEPRECATED ,
735
+ 'message ' => 'The option "foo" is deprecated. ' ,
736
+ ),
737
+ 4 ,
738
+ );
739
+
740
+ yield 'It ignores a deprecation if no option is provided by the user ' => array (
741
+ function (OptionsResolver $ resolver ) {
742
+ $ resolver
743
+ ->setDefined ('foo ' )
744
+ ->setDefault ('bar ' , null )
745
+ ->setDeprecated ('foo ' )
746
+ ->setDeprecated ('bar ' )
747
+ ;
748
+ },
749
+ array (),
750
+ null ,
751
+ 0 ,
752
+ );
753
+
754
+ yield 'It explicitly ignores a depreciation ' => array (
755
+ function (OptionsResolver $ resolver ) {
756
+ $ resolver
757
+ ->setDefault ('foo ' , null )
758
+ ->setDeprecated ('foo ' )
759
+ ->setDefault ('bar ' , function (Options $ options ) {
760
+ return $ options ->offsetGet ('foo ' , false );
761
+ })
762
+ ;
763
+ },
764
+ array (),
765
+ null ,
766
+ 0 ,
671
767
);
672
768
}
673
769
0 commit comments