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 ( $method, $args)
 
 __set ( $name, $value)
 
 __get ( $name)
 

Static Public Member Functions

static newFromObject ( $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 ( $className)
 Allow access to non-public static methods and properties of the class.
 
static constant ( $className, $constantName)
 Allow access to non-public constants of the class.
 
static construct (string $className,... $args)
 Allow constructing a class with a non-public constructor.
 

Public Attributes

 $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

◆ __call()

Wikimedia\TestingAccessWrapper::__call ( $method,
$args )
Parameters
string$method
array$args
Returns
mixed

◆ __get()

Wikimedia\TestingAccessWrapper::__get ( $name)
Parameters
string$nameField name
Returns
mixed

◆ __set()

Wikimedia\TestingAccessWrapper::__set ( $name,
$value )
Parameters
string$name
mixed$value

◆ constant()

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

Allow access to non-public constants of the class.

Parameters
class-string$className
string$constantName
Returns
mixed

◆ construct()

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

Allow constructing a class with a non-public constructor.

Parameters
class-string<T>$className
mixed...$args
Returns
T @phan-template T

◆ newFromClass()

static Wikimedia\TestingAccessWrapper::newFromClass ( $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
Returns
self
Exceptions
InvalidArgumentException

◆ newFromObject()

static Wikimedia\TestingAccessWrapper::newFromObject ( $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).

Parameters
object$object
Returns
self
Exceptions
InvalidArgumentException

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