@@ -37,12 +37,8 @@ final class HelpTest extends TestCase
3737 */
3838 public function testQaArgumentNamesAreWithinAcceptableBounds ()
3939 {
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 ' );
4642
4743 $ this ->assertGreaterThan (0 , count ($ allOptions ), 'No categories found ' );
4844
@@ -81,12 +77,8 @@ public function testQaArgumentNamesAreWithinAcceptableBounds()
8177 */
8278 public function testQaValidCategoryOptionDefinitions ()
8379 {
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 ' );
9082
9183 $ this ->assertGreaterThan (0 , count ($ allOptions ), 'No categories found ' );
9284
@@ -179,10 +171,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
179171 {
180172 $ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
181173
182- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
183- $ reflProperty ->setAccessible (true );
184- $ activeOptions = $ reflProperty ->getValue ($ help );
185- $ reflProperty ->setAccessible (false );
174+ $ activeOptions = $ this ->getReflectionProperty ($ help , 'activeOptions ' );
186175
187176 // Simplify the value to make it comparible.
188177 foreach ($ activeOptions as $ category => $ options ) {
@@ -318,10 +307,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
318307 {
319308 $ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
320309
321- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
322- $ reflProperty ->setAccessible (true );
323- $ activeOptions = $ reflProperty ->getValue ($ help );
324- $ reflProperty ->setAccessible (false );
310+ $ activeOptions = $ this ->getReflectionProperty ($ help , 'activeOptions ' );
325311
326312 $ this ->assertNotEmpty ($ activeOptions , 'Active options is empty, test is invalid ' );
327313
@@ -487,10 +473,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
487473 $ config = new ConfigDouble (["--report-width= $ reportWidth " , '--no-colors ' ]);
488474 $ help = new Help ($ config , $ longOptions );
489475
490- $ reflMethod = new ReflectionMethod ($ help , 'printCategories ' );
491- $ reflMethod ->setAccessible (true );
492- $ reflMethod ->invoke ($ help );
493- $ reflMethod ->setAccessible (false );
476+ $ this ->invokeReflectionMethod ($ help , 'printCategories ' );
494477
495478 $ this ->expectOutputString ($ expectedOutput );
496479
@@ -566,12 +549,8 @@ public static function dataReportWidthCalculations()
566549 */
567550 public function testColorizeVariableInput ($ input , $ expected )
568551 {
569- $ help = new Help (new ConfigDouble (), []);
570-
571- $ reflMethod = new ReflectionMethod ($ help , 'colorizeVariableInput ' );
572- $ reflMethod ->setAccessible (true );
573- $ result = $ reflMethod ->invoke ($ help , $ input );
574- $ reflMethod ->setAccessible (false );
552+ $ help = new Help (new ConfigDouble (), []);
553+ $ result = $ this ->invokeReflectionMethod ($ help , 'colorizeVariableInput ' , $ input );
575554
576555 $ this ->assertSame ($ expected , $ result );
577556
@@ -634,20 +613,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
634613 $ config = new ConfigDouble (['--no-colors ' ]);
635614 $ help = new Help ($ config , []);
636615
637- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
638- $ reflProperty ->setAccessible (true );
639- $ reflProperty ->setValue ($ help , ['cat ' => $ input ]);
640- $ reflProperty ->setAccessible (false );
641-
642- $ reflMethod = new ReflectionMethod ($ help , 'setMaxOptionNameLength ' );
643- $ reflMethod ->setAccessible (true );
644- $ reflMethod ->invoke ($ help );
645- $ reflMethod ->setAccessible (false );
646-
647- $ reflMethod = new ReflectionMethod ($ help , 'printCategoryOptions ' );
648- $ reflMethod ->setAccessible (true );
649- $ reflMethod ->invoke ($ help , $ input );
650- $ reflMethod ->setAccessible (false );
616+ $ this ->setReflectionProperty ($ help , 'activeOptions ' , ['cat ' => $ input ]);
617+ $ this ->invokeReflectionMethod ($ help , 'setMaxOptionNameLength ' );
618+ $ this ->invokeReflectionMethod ($ help , 'printCategoryOptions ' , $ input );
651619
652620 $ this ->expectOutputRegex ($ expectedRegex ['no-color ' ]);
653621
@@ -669,20 +637,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
669637 $ config = new ConfigDouble (['--colors ' ]);
670638 $ help = new Help ($ config , []);
671639
672- $ reflProperty = new ReflectionProperty ($ help , 'activeOptions ' );
673- $ reflProperty ->setAccessible (true );
674- $ reflProperty ->setValue ($ help , ['cat ' => $ input ]);
675- $ reflProperty ->setAccessible (false );
676-
677- $ reflMethod = new ReflectionMethod ($ help , 'setMaxOptionNameLength ' );
678- $ reflMethod ->setAccessible (true );
679- $ reflMethod ->invoke ($ help );
680- $ reflMethod ->setAccessible (false );
681-
682- $ reflMethod = new ReflectionMethod ($ help , 'printCategoryOptions ' );
683- $ reflMethod ->setAccessible (true );
684- $ reflMethod ->invoke ($ help , $ input );
685- $ reflMethod ->setAccessible (false );
640+ $ this ->setReflectionProperty ($ help , 'activeOptions ' , ['cat ' => $ input ]);
641+ $ this ->invokeReflectionMethod ($ help , 'setMaxOptionNameLength ' );
642+ $ this ->invokeReflectionMethod ($ help , 'printCategoryOptions ' , $ input );
686643
687644 $ this ->expectOutputRegex ($ expectedRegex ['color ' ]);
688645
@@ -760,4 +717,70 @@ public static function dataPrintCategoryOptions()
760717 }//end dataPrintCategoryOptions()
761718
762719
720+ /**
721+ * Test Helper: invoke a reflected method which is not publicly accessible.
722+ *
723+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
724+ * @param string $methodName The name of the method to invoke.
725+ * @param mixed $params Optional. Parameters to pass to the method invocation.
726+ *
727+ * @return mixed
728+ */
729+ private function invokeReflectionMethod (Help $ help , $ methodName , $ params =null )
730+ {
731+ $ reflMethod = new ReflectionMethod ($ help , $ methodName );
732+ (PHP_VERSION_ID < 80100 ) && $ reflMethod ->setAccessible (true );
733+
734+ if ($ params === null ) {
735+ $ returnValue = $ reflMethod ->invoke ($ help );
736+ } else {
737+ $ returnValue = $ reflMethod ->invoke ($ help , $ params );
738+ }
739+
740+ (PHP_VERSION_ID < 80100 ) && $ reflMethod ->setAccessible (false );
741+
742+ return $ returnValue ;
743+
744+ }//end invokeReflectionMethod()
745+
746+
747+ /**
748+ * Test Helper: retrieve the value of property which is not publicly accessible.
749+ *
750+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
751+ * @param string $properyName The name of the property to retrieve.
752+ *
753+ * @return mixed
754+ */
755+ private function getReflectionProperty (Help $ help , $ properyName )
756+ {
757+ $ reflProperty = new ReflectionProperty ($ help , $ properyName );
758+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (true );
759+ $ returnValue = $ reflProperty ->getValue ($ help );
760+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (false );
761+
762+ return $ returnValue ;
763+
764+ }//end getReflectionProperty()
765+
766+
767+ /**
768+ * Test Helper: set the value of property which is not publicly accessible.
769+ *
770+ * @param \PHP_CodeSniffer\Util\Help $help Instance of a Help object.
771+ * @param string $properyName The name of the property to set.
772+ * @param mixed $value The value to set.
773+ *
774+ * @return void
775+ */
776+ private function setReflectionProperty (Help $ help , $ properyName , $ value )
777+ {
778+ $ reflProperty = new ReflectionProperty ($ help , $ properyName );
779+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (true );
780+ $ reflProperty ->setValue ($ help , $ value );
781+ (PHP_VERSION_ID < 80100 ) && $ reflProperty ->setAccessible (false );
782+
783+ }//end setReflectionProperty()
784+
785+
763786}//end class
0 commit comments