Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / tests / phpunit / includes / TestingAccessWrapper.php
blob7332e15e8d95cb74954e6e8fe30cdc3c09271c94
1 <?php
2 /**
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();
14 * TODO:
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 */
19 public $object;
21 /**
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;
30 return $wrapper;
33 /**
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;
43 return $wrapper;
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,
54 $args );
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 );
81 /**
82 * Return a property and make it accessible.
83 * @param string $name
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;
93 /**
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().
99 * @param string $name
100 * @return ReflectionProperty
101 * @throws ReflectionException
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;
113 try {
114 $propertyReflection = $classReflection->getProperty( $name );
115 } catch ( ReflectionException $ex2 ) {
116 continue;
118 if ( $propertyReflection->isPrivate() ) {
119 break;
120 } else {
121 throw $ex;
125 $propertyReflection->setAccessible( true );
126 return $propertyReflection;