Testing Access Wrapper is a simple helper for writing unit tests which provides convenient shortcuts for using reflection to access non-public properties/methods.
The code was originally part of MediaWiki. See composer.json for a list of authors.
use Wikimedia\TestingAccessWrapper;
class NonPublic {
protected $prop;
protected const CONSTANT = 4;
protected function func() {}
protected static function staticFunc() {}
}
class NonPublicCtor {
protected function __construct() {}
}
$object = new NonPublic();
// or:
// $object = TestingAccessWrapper::construct( NonPublicCtor::class );
$wrapper = TestingAccessWrapper::newFromObject( $object );
$classWrapper = TestingAccessWrapper::newFromClass( NonPublic::class );
$wrapper->prop = 'foo';
$wrapper->func();
$classWrapper->staticFunc();
$value = TestingAccessWrapper::constant( NonPublic::class, 'CONSTANT' );
composer install
composer test