From 9cfbbbe9467adb16b81db0b1ba93f218022a9461 Mon Sep 17 00:00:00 2001 From: Adam Roses Wight Date: Fri, 27 Feb 2015 09:22:16 -0800 Subject: [PATCH] New testing wrapper to circumvent object access The new TestingAccessWrapper class provides a convenient way to make all of an object's methods and properties public. TODO: We should organize test helpers into a source directory. Note that the helper and its test are in the same directory. Change-Id: I958d55df18c74e9d2b25d98cd0316989a0fbbe6f --- tests/TestsAutoLoader.php | 3 ++ .../data/helpers/WellProtectedClass.php | 17 +++++++ .../phpunit/includes/TestingAccessWrapper.php | 50 +++++++++++++++++++ .../includes/TestingAccessWrapperTest.php | 30 +++++++++++ 4 files changed, 100 insertions(+) create mode 100644 tests/phpunit/data/helpers/WellProtectedClass.php create mode 100644 tests/phpunit/includes/TestingAccessWrapper.php create mode 100644 tests/phpunit/includes/TestingAccessWrapperTest.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index b410898557..b0f29d5d7d 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -47,6 +47,9 @@ $wgAutoloadClasses += array( '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", diff --git a/tests/phpunit/data/helpers/WellProtectedClass.php b/tests/phpunit/data/helpers/WellProtectedClass.php new file mode 100644 index 0000000000..7114cc9524 --- /dev/null +++ b/tests/phpunit/data/helpers/WellProtectedClass.php @@ -0,0 +1,17 @@ +property = 1; + } + + protected function incrementPropertyValue() { + $this->property++; + } + + public function getProperty() { + return $this->property; + } +} diff --git a/tests/phpunit/includes/TestingAccessWrapper.php b/tests/phpunit/includes/TestingAccessWrapper.php new file mode 100644 index 0000000000..d4ad363f69 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapper.php @@ -0,0 +1,50 @@ +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 ); + } +} diff --git a/tests/phpunit/includes/TestingAccessWrapperTest.php b/tests/phpunit/includes/TestingAccessWrapperTest.php new file mode 100644 index 0000000000..8da8e420d6 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapperTest.php @@ -0,0 +1,30 @@ +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() ); + } +} -- 2.20.1