testing-access-wrapper
Simple unit test helper for accessing non-public properties/methods.
Loading...
Searching...
No Matches
Wikimedia\TestingAccessWrapper Class Reference

Circumvent access restrictions on object internals. More...

Public Member Functions

 __call (string $method, array $args)
 
 __set (string $name, mixed $value)
 
 __get (string $name)
 

Static Public Member Functions

static newFromObject (object $object)
 Return a proxy object which can be used the same way as the original, except that access restrictions can be ignored (protected and private methods and properties are available for any caller).
 
static newFromClass (string $className)
 Allow access to non-public static methods and properties of the class.
 
static constant (string $className, string $constantName)
 Allow access to non-public constants of the class.
 
static construct (string $className, mixed ... $args)
 Allow constructing a class with a non-public constructor.
 

Public Attributes

object string $object
 

Detailed Description

Circumvent access restrictions on object internals.

This can be helpful for writing tests that can probe object internals, without having to modify the class under test to accommodate.

Wrap an object with private methods as follows: $title = TestingAccessWrapper::newFromObject( Title::newFromDBkey( $key ) );

You can access private and protected instance methods and variables: $formatter = $title->getTitleFormatter();

You can access private and protected constants: $value = TestingAccessWrapper::constant( Foo::class, 'FOO_CONSTANT' );

Member Function Documentation

◆ constant()

static Wikimedia\TestingAccessWrapper::constant ( string $className,
string $constantName )
static

Allow access to non-public constants of the class.

Parameters
class-string$className
string$constantName

◆ construct()

static Wikimedia\TestingAccessWrapper::construct ( string $className,
mixed ... $args )
static

Allow constructing a class with a non-public constructor.

@template T of object

Parameters
class-string<T>$className
mixed...$args @phan-suppress-next-line PhanTypeMismatchDeclaredReturn
Returns
T

◆ newFromClass()

static Wikimedia\TestingAccessWrapper::newFromClass ( string $className)
static

Allow access to non-public static methods and properties of the class.

Returns an object whose methods/properties will correspond to the static methods/properties of the given class.

Parameters
class-string$className
Exceptions
InvalidArgumentException

◆ newFromObject()

static Wikimedia\TestingAccessWrapper::newFromObject ( object $object)
static

Return a proxy object which can be used the same way as the original, except that access restrictions can be ignored (protected and private methods and properties are available for any caller).

Exceptions
InvalidArgumentException

The documentation for this class was generated from the following file: