'TestUser' => "$testDir/phpunit/includes/TestUser.php",
'LessFileCompilationTest' => "$testDir/phpunit/LessFileCompilationTest.php",
+ # tests/phpunit/includes
+ 'TestingAccessWrapper' => "$testDir/phpunit/includes/TestingAccessWrapper.php",
+
# tests/phpunit/includes/api
'ApiFormatTestBase' => "$testDir/phpunit/includes/api/format/ApiFormatTestBase.php",
'ApiQueryTestBase' => "$testDir/phpunit/includes/api/query/ApiQueryTestBase.php",
--- /dev/null
+<?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->invoke( $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 );
+ }
+}
--- /dev/null
+<?php
+
+class TestingAccessWrapperTest extends MediaWikiTestCase {
+ protected $raw;
+ protected $wrapped;
+
+ function setUp() {
+ parent::setUp();
+
+ require_once __DIR__ . '/../data/helpers/WellProtectedClass.php';
+ $this->raw = new WellProtectedClass();
+ $this->wrapped = TestingAccessWrapper::newFromObject( $this->raw );
+ }
+
+ function testGetProperty() {
+ $this->assertSame( 1, $this->wrapped->property );
+ }
+
+ function testSetProperty() {
+ $this->wrapped->property = 10;
+ $this->assertSame( 10, $this->wrapped->property );
+ $this->assertSame( 10, $this->raw->getProperty() );
+ }
+
+ function testCallMethod() {
+ $this->wrapped->incrementPropertyValue();
+ $this->assertSame( 2, $this->wrapped->property );
+ $this->assertSame( 2, $this->raw->getProperty() );
+ }
+}