Merge "PostgreSQL: Fix text search on moved pages"
[lhc/web/wiklou.git] / tests / phpunit / includes / TestingAccessWrapper.php
1 <?php
2 /**
3 * Circumvent access restrictions on object internals
4 *
5 * This can be helpful for writing tests that can probe object internals,
6 * without having to modify the class under test to accomodate.
7 *
8 * Wrap an object with private methods as follows:
9 * $title = TestingAccessWrapper::newFromObject( Title::newFromDBkey( $key ) );
10 *
11 * You can access private and protected instance methods and variables:
12 * $formatter = $title->getTitleFormatter();
13 *
14 * TODO:
15 * - Provide access to static methods and properties.
16 * - Organize other helper classes in tests/testHelpers.inc into a directory.
17 */
18 class TestingAccessWrapper {
19 public $object;
20
21 /**
22 * Return the same object, without access restrictions.
23 */
24 public static function newFromObject( $object ) {
25 $wrapper = new TestingAccessWrapper();
26 $wrapper->object = $object;
27 return $wrapper;
28 }
29
30 public function __call( $method, $args ) {
31 $classReflection = new ReflectionClass( $this->object );
32 $methodReflection = $classReflection->getMethod( $method );
33 $methodReflection->setAccessible( true );
34 return $methodReflection->invokeArgs( $this->object, $args );
35 }
36
37 public function __set( $name, $value ) {
38 $classReflection = new ReflectionClass( $this->object );
39 $propertyReflection = $classReflection->getProperty( $name );
40 $propertyReflection->setAccessible( true );
41 $propertyReflection->setValue( $this->object, $value );
42 }
43
44 public function __get( $name ) {
45 $classReflection = new ReflectionClass( $this->object );
46 $propertyReflection = $classReflection->getProperty( $name );
47 $propertyReflection->setAccessible( true );
48 return $propertyReflection->getValue( $this->object );
49 }
50 }