Merge "Rm unused 'remembermypassword' message, doc another"
[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 * - Organize other helper classes in tests/testHelpers.inc into a directory.
16 */
17 class TestingAccessWrapper {
18 /** @var mixed The object, or the class name for static-only access */
19 public $object;
20
21 /**
22 * Return the same object, without access restrictions.
23 */
24 public static function newFromObject( $object ) {
25 if ( !is_object( $object ) ) {
26 throw new InvalidArgumentException( __METHOD__ . ' must be called with an object' );
27 }
28 $wrapper = new TestingAccessWrapper();
29 $wrapper->object = $object;
30 return $wrapper;
31 }
32
33 /**
34 * Allow access to non-public static methods and properties of the class.
35 * Use non-static access,
36 */
37 public static function newFromClass( $className ) {
38 if ( !is_string( $className ) ) {
39 throw new InvalidArgumentException( __METHOD__ . ' must be called with a class name' );
40 }
41 $wrapper = new TestingAccessWrapper();
42 $wrapper->object = $className;
43 return $wrapper;
44 }
45
46 public function __call( $method, $args ) {
47 $methodReflection = $this->getMethod( $method );
48
49 if ( $this->isStatic() && !$methodReflection->isStatic() ) {
50 throw new DomainException( __METHOD__ . ': Cannot call non-static when wrapping static class' );
51 }
52
53 return $methodReflection->invokeArgs( $methodReflection->isStatic() ? null : $this->object,
54 $args );
55 }
56
57 public function __set( $name, $value ) {
58 $propertyReflection = $this->getProperty( $name );
59
60 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
61 throw new DomainException( __METHOD__ . ': Cannot set property when wrapping static class' );
62 }
63
64 $propertyReflection->setValue( $this->object, $value );
65 }
66
67 public function __get( $name ) {
68 $propertyReflection = $this->getProperty( $name );
69
70 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
71 throw new DomainException( __METHOD__ . ': Cannot get property when wrapping static class' );
72 }
73
74 return $propertyReflection->getValue( $this->object );
75 }
76
77 private function isStatic() {
78 return is_string( $this->object );
79 }
80
81 /**
82 * Return a property and make it accessible.
83 * @param string $name
84 * @return ReflectionMethod
85 */
86 private function getMethod( $name ) {
87 $classReflection = new ReflectionClass( $this->object );
88 $methodReflection = $classReflection->getMethod( $name );
89 $methodReflection->setAccessible( true );
90 return $methodReflection;
91 }
92
93 /**
94 * Return a property and make it accessible.
95 *
96 * ReflectionClass::getProperty() fails if the private property is defined
97 * in a parent class. This works more like ReflectionClass::getMethod().
98 *
99 * @param string $name
100 * @return ReflectionProperty
101 * @throws ReflectionException
102 */
103 private function getProperty( $name ) {
104 $classReflection = new ReflectionClass( $this->object );
105 try {
106 $propertyReflection = $classReflection->getProperty( $name );
107 } catch ( ReflectionException $ex ) {
108 while ( true ) {
109 $classReflection = $classReflection->getParentClass();
110 if ( !$classReflection ) {
111 throw $ex;
112 }
113 try {
114 $propertyReflection = $classReflection->getProperty( $name );
115 } catch ( ReflectionException $ex2 ) {
116 continue;
117 }
118 if ( $propertyReflection->isPrivate() ) {
119 break;
120 } else {
121 throw $ex;
122 }
123 }
124 }
125 $propertyReflection->setAccessible( true );
126 return $propertyReflection;
127 }
128 }