wiki.techinc.nl/tests/phpunit/includes/TestingAccessWrapper.php
Adam Roses Wight c46ee213f0 Fix TestingAccessWrapper::__call
We were only passing the first parameter to the wrapped object's methods.

Change-Id: I27a69d1cc1b2d048e44514af8b4ac79d7ee1fb85
2015-03-26 09:26:01 +00:00

50 lines
1.6 KiB
PHP

<?php
/**
* 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 accomodate.
*
* 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();
*
* TODO:
* - Provide access to static methods and properties.
* - Organize other helper classes in tests/testHelpers.inc into a directory.
*/
class TestingAccessWrapper {
public $object;
/**
* Return the same object, without access restrictions.
*/
public static function newFromObject( $object ) {
$wrapper = new TestingAccessWrapper();
$wrapper->object = $object;
return $wrapper;
}
public function __call( $method, $args ) {
$classReflection = new ReflectionClass( $this->object );
$methodReflection = $classReflection->getMethod( $method );
$methodReflection->setAccessible( true );
return $methodReflection->invokeArgs( $this->object, $args );
}
public function __set( $name, $value ) {
$classReflection = new ReflectionClass( $this->object );
$propertyReflection = $classReflection->getProperty( $name );
$propertyReflection->setAccessible( true );
$propertyReflection->setValue( $this->object, $value );
}
public function __get( $name ) {
$classReflection = new ReflectionClass( $this->object );
$propertyReflection = $classReflection->getProperty( $name );
$propertyReflection->setAccessible( true );
return $propertyReflection->getValue( $this->object );
}
}