3 * Circumvent access restrictions on object internals
5 * This can be helpful for writing tests that can probe object internals,
6 * without having to modify the class under test to accomodate.
8 * Wrap an object with private methods as follows:
9 * $title = TestingAccessWrapper::newFromObject( Title::newFromDBkey( $key ) );
11 * You can access private and protected instance methods and variables:
12 * $formatter = $title->getTitleFormatter();
15 * - Organize other helper classes in tests/testHelpers.inc into a directory.
17 class TestingAccessWrapper
{
18 /** @var mixed The object, or the class name for static-only access */
22 * Return the same object, without access restrictions.
24 public static function newFromObject( $object ) {
25 if ( !is_object( $object ) ) {
26 throw new InvalidArgumentException( __METHOD__
. ' must be called with an object' );
28 $wrapper = new TestingAccessWrapper();
29 $wrapper->object = $object;
34 * Allow access to non-public static methods and properties of the class.
35 * Use non-static access,
37 public static function newFromClass( $className ) {
38 if ( !is_string( $className ) ) {
39 throw new InvalidArgumentException( __METHOD__
. ' must be called with a class name' );
41 $wrapper = new TestingAccessWrapper();
42 $wrapper->object = $className;
46 public function __call( $method, $args ) {
47 $methodReflection = $this->getMethod( $method );
49 if ( $this->isStatic() && !$methodReflection->isStatic() ) {
50 throw new DomainException( __METHOD__
. ': Cannot call non-static when wrapping static class' );
53 return $methodReflection->invokeArgs( $methodReflection->isStatic() ?
null : $this->object,
57 public function __set( $name, $value ) {
58 $propertyReflection = $this->getProperty( $name );
60 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
61 throw new DomainException( __METHOD__
. ': Cannot set property when wrapping static class' );
64 $propertyReflection->setValue( $this->object, $value );
67 public function __get( $name ) {
68 $propertyReflection = $this->getProperty( $name );
70 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
71 throw new DomainException( __METHOD__
. ': Cannot get property when wrapping static class' );
74 return $propertyReflection->getValue( $this->object );
77 private function isStatic() {
78 return is_string( $this->object );
82 * Return a property and make it accessible.
84 * @return ReflectionMethod
86 private function getMethod( $name ) {
87 $classReflection = new ReflectionClass( $this->object );
88 $methodReflection = $classReflection->getMethod( $name );
89 $methodReflection->setAccessible( true );
90 return $methodReflection;
94 * Return a property and make it accessible.
96 * ReflectionClass::getProperty() fails if the private property is defined
97 * in a parent class. This works more like ReflectionClass::getMethod().
100 * @return ReflectionProperty
101 * @throws ReflectionException
103 private function getProperty( $name ) {
104 $classReflection = new ReflectionClass( $this->object );
106 $propertyReflection = $classReflection->getProperty( $name );
107 } catch ( ReflectionException
$ex ) {
109 $classReflection = $classReflection->getParentClass();
110 if ( !$classReflection ) {
114 $propertyReflection = $classReflection->getProperty( $name );
115 } catch ( ReflectionException
$ex2 ) {
118 if ( $propertyReflection->isPrivate() ) {
125 $propertyReflection->setAccessible( true );
126 return $propertyReflection;