Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save koertho/8a0dfc17148ed2e70c214d05ba7ec022 to your computer and use it in GitHub Desktop.

Select an option

Save koertho/8a0dfc17148ed2e70c214d05ba7ec022 to your computer and use it in GitHub Desktop.
How to test private methods and set private properties with phpunit
<?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