@@ -37,12 +37,8 @@ final class HelpTest extends TestCase
37
37
*/
38
38
public function testQaArgumentNamesAreWithinAcceptableBounds ()
39
39
{
40
- $ help = new Help (new ConfigDouble (), []);
41
-
42
- $ reflMethod = new ReflectionMethod ($ help , 'getAllOptions ' );
43
- $ reflMethod ->setAccessible (true );
44
- $ allOptions = $ reflMethod ->invoke ($ help );
45
- $ reflMethod ->setAccessible (false );
40
+ $ help = new Help (new ConfigDouble (), []);
41
+ $ allOptions = $ this ->invokeReflectionMethod ($ help , 'getAllOptions ' );
46
42
47
43
$ this ->assertGreaterThan (0 , count ($ allOptions ), 'No categories found ' );
48
44
@@ -81,12 +77,8 @@ public function testQaArgumentNamesAreWithinAcceptableBounds()
81
77
*/
82
78
public function testQaValidCategoryOptionDefinitions ()
83
79
{
84
- $ help = new Help (new ConfigDouble (), []);
85
-
86
- $ reflMethod = new ReflectionMethod ($ help , 'getAllOptions ' );
87
- $ reflMethod ->setAccessible (true );
88
- $ allOptions = $ reflMethod ->invoke ($ help );
89
- $ reflMethod ->setAccessible (false );
80
+ $ help = new Help (new ConfigDouble (), []);
81
+ $ allOptions = $ this ->invokeReflectionMethod ($ help , 'getAllOptions ' );
90
82
91
83
$ this ->assertGreaterThan (0 , count ($ allOptions ), 'No categories found ' );
92
84
@@ -185,10 +177,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
185
177
{
186
178
$ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
187
179
188
- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
189
- $ reflProperty ->setAccessible (true );
190
- $ activeOptions = $ reflProperty ->getValue ($ help );
191
- $ reflProperty ->setAccessible (false );
180
+ $ activeOptions = $ this ->getReflectionProperty ($ help , 'activeOptions ' );
192
181
193
182
// Simplify the value to make it comparible.
194
183
foreach ($ activeOptions as $ category => $ options ) {
@@ -324,10 +313,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
324
313
{
325
314
$ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
326
315
327
- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
328
- $ reflProperty ->setAccessible (true );
329
- $ activeOptions = $ reflProperty ->getValue ($ help );
330
- $ reflProperty ->setAccessible (false );
316
+ $ activeOptions = $ this ->getReflectionProperty ($ help , 'activeOptions ' );
331
317
332
318
$ this ->assertNotEmpty ($ activeOptions , 'Active options is empty, test is invalid ' );
333
319
@@ -493,10 +479,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
493
479
$ config = new ConfigDouble (["--report-width= $ reportWidth " , '--no-colors ' ]);
494
480
$ help = new Help ($ config , $ longOptions );
495
481
496
- $ reflMethod = new ReflectionMethod ($ help , 'printCategories ' );
497
- $ reflMethod ->setAccessible (true );
498
- $ reflMethod ->invoke ($ help );
499
- $ reflMethod ->setAccessible (false );
482
+ $ this ->invokeReflectionMethod ($ help , 'printCategories ' );
500
483
501
484
$ this ->expectOutputString ($ expectedOutput );
502
485
@@ -572,12 +555,8 @@ public static function dataReportWidthCalculations()
572
555
*/
573
556
public function testColorizeVariableInput ($ input , $ expected )
574
557
{
575
- $ help = new Help (new ConfigDouble (), []);
576
-
577
- $ reflMethod = new ReflectionMethod ($ help , 'colorizeVariableInput ' );
578
- $ reflMethod ->setAccessible (true );
579
- $ result = $ reflMethod ->invoke ($ help , $ input );
580
- $ reflMethod ->setAccessible (false );
558
+ $ help = new Help (new ConfigDouble (), []);
559
+ $ result = $ this ->invokeReflectionMethod ($ help , 'colorizeVariableInput ' , $ input );
581
560
582
561
$ this ->assertSame ($ expected , $ result );
583
562
@@ -640,20 +619,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
640
619
$ config = new ConfigDouble (['--no-colors ' ]);
641
620
$ help = new Help ($ config , []);
642
621
643
- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
644
- $ reflProperty ->setAccessible (true );
645
- $ reflProperty ->setValue ($ help , ['cat ' => $ input ]);
646
- $ reflProperty ->setAccessible (false );
647
-
648
- $ reflMethod = new ReflectionMethod ($ help , 'setMaxOptionNameLength ' );
649
- $ reflMethod ->setAccessible (true );
650
- $ reflMethod ->invoke ($ help );
651
- $ reflMethod ->setAccessible (false );
652
-
653
- $ reflMethod = new ReflectionMethod ($ help , 'printCategoryOptions ' );
654
- $ reflMethod ->setAccessible (true );
655
- $ reflMethod ->invoke ($ help , $ input );
656
- $ reflMethod ->setAccessible (false );
622
+ $ this ->setReflectionProperty ($ help , 'activeOptions ' , ['cat ' => $ input ]);
623
+ $ this ->invokeReflectionMethod ($ help , 'setMaxOptionNameLength ' );
624
+ $ this ->invokeReflectionMethod ($ help , 'printCategoryOptions ' , $ input );
657
625
658
626
$ this ->expectOutputRegex ($ expectedRegex ['no-color ' ]);
659
627
@@ -675,20 +643,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
675
643
$ config = new ConfigDouble (['--colors ' ]);
676
644
$ help = new Help ($ config , []);
677
645
678
- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
679
- $ reflProperty ->setAccessible (true );
680
- $ reflProperty ->setValue ($ help , ['cat ' => $ input ]);
681
- $ reflProperty ->setAccessible (false );
682
-
683
- $ reflMethod = new ReflectionMethod ($ help , 'setMaxOptionNameLength ' );
684
- $ reflMethod ->setAccessible (true );
685
- $ reflMethod ->invoke ($ help );
686
- $ reflMethod ->setAccessible (false );
687
-
688
- $ reflMethod = new ReflectionMethod ($ help , 'printCategoryOptions ' );
689
- $ reflMethod ->setAccessible (true );
690
- $ reflMethod ->invoke ($ help , $ input );
691
- $ reflMethod ->setAccessible (false );
646
+ $ this ->setReflectionProperty ($ help , 'activeOptions ' , ['cat ' => $ input ]);
647
+ $ this ->invokeReflectionMethod ($ help , 'setMaxOptionNameLength ' );
648
+ $ this ->invokeReflectionMethod ($ help , 'printCategoryOptions ' , $ input );
692
649
693
650
$ this ->expectOutputRegex ($ expectedRegex ['color ' ]);
694
651
@@ -766,4 +723,70 @@ public static function dataPrintCategoryOptions()
766
723
}//end dataPrintCategoryOptions()
767
724
768
725
726
+ /**
727
+ * Test Helper: invoke a reflected method which is not publicly accessible.
728
+ *
729
+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
730
+ * @param string $methodName The name of the method to invoke.
731
+ * @param mixed $params Optional. Parameters to pass to the method invocation.
732
+ *
733
+ * @return mixed
734
+ */
735
+ private function invokeReflectionMethod (Help $ help , $ methodName , $ params =null )
736
+ {
737
+ $ reflMethod = new ReflectionMethod ($ help , $ methodName );
738
+ (PHP_VERSION_ID < 80100 ) && $ reflMethod ->setAccessible (true );
739
+
740
+ if ($ params === null ) {
741
+ $ returnValue = $ reflMethod ->invoke ($ help );
742
+ } else {
743
+ $ returnValue = $ reflMethod ->invoke ($ help , $ params );
744
+ }
745
+
746
+ (PHP_VERSION_ID < 80100 ) && $ reflMethod ->setAccessible (false );
747
+
748
+ return $ returnValue ;
749
+
750
+ }//end invokeReflectionMethod()
751
+
752
+
753
+ /**
754
+ * Test Helper: retrieve the value of property which is not publicly accessible.
755
+ *
756
+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
757
+ * @param string $properyName The name of the property to retrieve.
758
+ *
759
+ * @return mixed
760
+ */
761
+ private function getReflectionProperty (Help $ help , $ properyName )
762
+ {
763
+ $ reflProperty = new ReflectionProperty ($ help , $ properyName );
764
+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (true );
765
+ $ returnValue = $ reflProperty ->getValue ($ help );
766
+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (false );
767
+
768
+ return $ returnValue ;
769
+
770
+ }//end getReflectionProperty()
771
+
772
+
773
+ /**
774
+ * Test Helper: set the value of property which is not publicly accessible.
775
+ *
776
+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
777
+ * @param string $properyName The name of the property to set.
778
+ * @param mixed $value The value to set.
779
+ *
780
+ * @return void
781
+ */
782
+ private function setReflectionProperty (Help $ help , $ properyName , $ value )
783
+ {
784
+ $ reflProperty = new ReflectionProperty ($ help , $ properyName );
785
+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (true );
786
+ $ reflProperty ->setValue ($ help , $ value );
787
+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (false );
788
+
789
+ }//end setReflectionProperty()
790
+
791
+
769
792
}//end class
0 commit comments