@@ -501,7 +501,7 @@ public function testLazyDeprecationFailsIfInvalidDeprecationMessageType()
501
501
502
502
/**
503
503
* @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
504
- * @expectedExceptionMessage The options "bar ", "foo " have a cyclic dependency.
504
+ * @expectedExceptionMessage The options "foo ", "bar " have a cyclic dependency.
505
505
*/
506
506
public function testFailsIfCyclicDependencyBetweenDeprecation ()
507
507
{
@@ -538,10 +538,15 @@ public function testIsNotDeprecatedIfEmptyString()
538
538
/**
539
539
* @dataProvider provideDeprecationData
540
540
*/
541
- public function testDeprecationMessages (\Closure $ configureOptions , array $ options , ?array $ expectedError )
541
+ public function testDeprecationMessages (\Closure $ configureOptions , array $ options , ?array $ expectedError, int $ expectedCount )
542
542
{
543
+ $ count = 0 ;
543
544
error_clear_last ();
544
- set_error_handler (function () { return false ; });
545
+ set_error_handler (function () use (&$ count ) {
546
+ ++$ count ;
547
+
548
+ return false ;
549
+ });
545
550
$ e = error_reporting (0 );
546
551
547
552
$ configureOptions ($ this ->resolver );
@@ -554,6 +559,7 @@ public function testDeprecationMessages(\Closure $configureOptions, array $optio
554
559
unset($ lastError ['file ' ], $ lastError ['line ' ]);
555
560
556
561
$ this ->assertSame ($ expectedError , $ lastError );
562
+ $ this ->assertSame ($ expectedCount , $ count );
557
563
}
558
564
559
565
public function provideDeprecationData ()
@@ -570,6 +576,7 @@ function (OptionsResolver $resolver) {
570
576
'type ' => E_USER_DEPRECATED ,
571
577
'message ' => 'The option "foo" is deprecated. ' ,
572
578
),
579
+ 1 ,
573
580
);
574
581
575
582
yield 'It deprecates an option with custom message ' => array (
@@ -587,17 +594,7 @@ function (OptionsResolver $resolver) {
587
594
'type ' => E_USER_DEPRECATED ,
588
595
'message ' => 'The option "foo" is deprecated, use "bar" option instead. ' ,
589
596
),
590
- );
591
-
592
- yield 'It does not deprecates a missing option with default value ' => array (
593
- function (OptionsResolver $ resolver ) {
594
- $ resolver
595
- ->setDefault ('foo ' , null )
596
- ->setDeprecated ('foo ' )
597
- ;
598
- },
599
- array (),
600
- null ,
597
+ 2 ,
601
598
);
602
599
603
600
yield 'It deprecates an option evaluated in another definition ' => array (
@@ -609,14 +606,15 @@ function (OptionsResolver $resolver) {
609
606
;
610
607
// defined by subclass
611
608
$ resolver ->setDefault ('bar ' , function (Options $ options ) {
612
- return $ options ['foo ' ];
609
+ return $ options ['foo ' ]; // It triggers a deprecation
613
610
});
614
611
},
615
612
array (),
616
613
array (
617
614
'type ' => E_USER_DEPRECATED ,
618
615
'message ' => 'The option "foo" is deprecated. ' ,
619
616
),
617
+ 1 ,
620
618
);
621
619
622
620
yield 'It deprecates allowed type and value ' => array (
@@ -638,20 +636,46 @@ function (OptionsResolver $resolver) {
638
636
'type ' => E_USER_DEPRECATED ,
639
637
'message ' => 'Passing an instance of "stdClass" to option "foo" is deprecated, pass its FQCN instead. ' ,
640
638
),
639
+ 1 ,
641
640
);
642
641
643
- 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 (
644
643
function (OptionsResolver $ resolver ) {
645
644
$ resolver
646
- ->setDefined (array ('foo ' , 'bar ' ))
647
- ->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
+ })
648
668
;
649
669
},
650
- array ('bar ' => 'baz ' ),
651
- 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 ,
652
676
);
653
677
654
- yield 'It ignores deprecation if closure returns an empty string ' => array (
678
+ yield 'It ignores a deprecation if closure returns an empty string ' => array (
655
679
function (OptionsResolver $ resolver ) {
656
680
$ resolver
657
681
->setDefault ('foo ' , null )
@@ -662,6 +686,7 @@ function (OptionsResolver $resolver) {
662
686
},
663
687
array ('foo ' => Bar::class),
664
688
null ,
689
+ 0 ,
665
690
);
666
691
667
692
yield 'It deprecates value depending on other option value ' => array (
@@ -683,6 +708,62 @@ function (OptionsResolver $resolver) {
683
708
'type ' => E_USER_DEPRECATED ,
684
709
'message ' => 'Using the "date_format" option when the "widget" option is set to "single_text" is deprecated. ' ,
685
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 ,
686
767
);
687
768
}
688
769
0 commit comments