@@ -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
@@ -185,10 +177,7 @@ public function testOptionFiltering($longOptions, $shortOptions, $expected)
185177 {
186178 $ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
187179
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 ' );
192181
193182 // Simplify the value to make it comparible.
194183 foreach ($ activeOptions as $ category => $ options ) {
@@ -324,10 +313,7 @@ public function testOptionFilteringSpacerHandling($longOptions, $shortOptions)
324313 {
325314 $ help = new Help (new ConfigDouble (), $ longOptions , $ shortOptions );
326315
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 ' );
331317
332318 $ this ->assertNotEmpty ($ activeOptions , 'Active options is empty, test is invalid ' );
333319
@@ -493,10 +479,7 @@ public function testReportWidthCalculations($reportWidth, $longOptions, $expecte
493479 $ config = new ConfigDouble (["--report-width= $ reportWidth " , '--no-colors ' ]);
494480 $ help = new Help ($ config , $ longOptions );
495481
496- $ reflMethod = new ReflectionMethod ($ help , 'printCategories ' );
497- $ reflMethod ->setAccessible (true );
498- $ reflMethod ->invoke ($ help );
499- $ reflMethod ->setAccessible (false );
482+ $ this ->invokeReflectionMethod ($ help , 'printCategories ' );
500483
501484 $ this ->expectOutputString ($ expectedOutput );
502485
@@ -572,12 +555,8 @@ public static function dataReportWidthCalculations()
572555 */
573556 public function testColorizeVariableInput ($ input , $ expected )
574557 {
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 );
581560
582561 $ this ->assertSame ($ expected , $ result );
583562
@@ -640,20 +619,9 @@ public function testPrintCategoryOptionsNoColor($input, $expectedRegex)
640619 $ config = new ConfigDouble (['--no-colors ' ]);
641620 $ help = new Help ($ config , []);
642621
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 );
657625
658626 $ this ->expectOutputRegex ($ expectedRegex ['no-color ' ]);
659627
@@ -675,20 +643,9 @@ public function testPrintCategoryOptionsColor($input, $expectedRegex)
675643 $ config = new ConfigDouble (['--colors ' ]);
676644 $ help = new Help ($ config , []);
677645
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 );
692649
693650 $ this ->expectOutputRegex ($ expectedRegex ['color ' ]);
694651
@@ -766,4 +723,70 @@ public static function dataPrintCategoryOptions()
766723 }//end dataPrintCategoryOptions()
767724
768725
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+
769792}//end class
0 commit comments