Created
March 20, 2018 08:20
-
-
Save koertho/8a0dfc17148ed2e70c214d05ba7ec022 to your computer and use it in GitHub Desktop.
How to test private methods and set private properties with phpunit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Test private methid | |
| $reflectionClass = new \ReflectionClass(TestClass::class); | |
| $testMethod = $reflectionClass->getMethod('testMethod'); | |
| $testMethod->setAccessible(true); | |
| $testingClass = new TestClass(); | |
| $result = $testMethod->invokeArgs($testingClass, ['argument 1', 'argument 2']); | |
| // Set private property | |
| $reflectionProperty = $reflectionClass->getProperty('testProperty'); | |
| $reflectionProperty->setAccessible(true); | |
| $reflectionProperty->setValue($testingClass, 'Value'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment