blob: 63d897198b6da804127e377533bb6ec6a9749f5a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?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 );
}
/**
* ReflectionClass::getProperty() fails if the private property is defined
* in a parent class. This works more like ReflectionClass::getMethod().
*/
private function getProperty( $name ) {
$classReflection = new ReflectionClass( $this->object );
try {
return $classReflection->getProperty( $name );
} catch ( ReflectionException $ex ) {
while ( true ) {
$classReflection = $classReflection->getParentClass();
if ( !$classReflection ) {
throw $ex;
}
try {
$propertyReflection = $classReflection->getProperty( $name );
} catch ( ReflectionException $ex2 ) {
continue;
}
if ( $propertyReflection->isPrivate() ) {
return $propertyReflection;
} else {
throw $ex;
}
}
}
}
public function __set( $name, $value ) {
$propertyReflection = $this->getProperty( $name );
$propertyReflection->setAccessible( true );
$propertyReflection->setValue( $this->object, $value );
}
public function __get( $name ) {
$propertyReflection = $this->getProperty( $name );
$propertyReflection->setAccessible( true );
return $propertyReflection->getValue( $this->object );
}
}
|