[MediaWiki-commits] [Gerrit] New testing wrapper to circumvent object access - change (mediawiki/core)
Ejegg has submitted this change and it was merged. Change subject: New testing wrapper to circumvent object access .. 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 --- M tests/TestsAutoLoader.php A tests/phpunit/data/helpers/WellProtectedClass.php A tests/phpunit/includes/TestingAccessWrapper.php A tests/phpunit/includes/TestingAccessWrapperTest.php 4 files changed, 98 insertions(+), 0 deletions(-) Approvals: Ejegg: Verified; Looks good to me, approved diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 29c3269..6587be8 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -49,6 +49,7 @@ # tests/phpunit/includes 'BlockTest' = $testDir/phpunit/includes/BlockTest.php, 'RevisionStorageTest' = $testDir/phpunit/includes/RevisionStorageTest.php, + 'TestingAccessWrapper' = $testDir/phpunit/includes/TestingAccessWrapper.php, 'WikiPageTest' = $testDir/phpunit/includes/WikiPageTest.php, # tests/phpunit/includes/api diff --git a/tests/phpunit/data/helpers/WellProtectedClass.php b/tests/phpunit/data/helpers/WellProtectedClass.php new file mode 100644 index 000..7114cc9 --- /dev/null +++ b/tests/phpunit/data/helpers/WellProtectedClass.php @@ -0,0 +1,17 @@ +?php + +class WellProtectedClass { + protected $property; + + public function __construct() { + $this-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 000..d4ad363 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapper.php @@ -0,0 +1,50 @@ +?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 ); + } +} diff --git a/tests/phpunit/includes/TestingAccessWrapperTest.php b/tests/phpunit/includes/TestingAccessWrapperTest.php new file mode 100644 index 000..8da8e42 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapperTest.php @@ -0,0 +1,30 @@ +?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 ); + } + +
[MediaWiki-commits] [Gerrit] New testing wrapper to circumvent object access - change (mediawiki/core)
Awight has uploaded a new change for review. https://gerrit.wikimedia.org/r/201133 Change subject: New testing wrapper to circumvent object access .. 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 --- M tests/TestsAutoLoader.php A tests/phpunit/data/helpers/WellProtectedClass.php A tests/phpunit/includes/TestingAccessWrapper.php A tests/phpunit/includes/TestingAccessWrapperTest.php 4 files changed, 98 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/33/201133/1 diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 29c3269..6587be8 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -49,6 +49,7 @@ # tests/phpunit/includes 'BlockTest' = $testDir/phpunit/includes/BlockTest.php, 'RevisionStorageTest' = $testDir/phpunit/includes/RevisionStorageTest.php, + 'TestingAccessWrapper' = $testDir/phpunit/includes/TestingAccessWrapper.php, 'WikiPageTest' = $testDir/phpunit/includes/WikiPageTest.php, # tests/phpunit/includes/api diff --git a/tests/phpunit/data/helpers/WellProtectedClass.php b/tests/phpunit/data/helpers/WellProtectedClass.php new file mode 100644 index 000..7114cc9 --- /dev/null +++ b/tests/phpunit/data/helpers/WellProtectedClass.php @@ -0,0 +1,17 @@ +?php + +class WellProtectedClass { + protected $property; + + public function __construct() { + $this-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 000..d4ad363 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapper.php @@ -0,0 +1,50 @@ +?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 ); + } +} diff --git a/tests/phpunit/includes/TestingAccessWrapperTest.php b/tests/phpunit/includes/TestingAccessWrapperTest.php new file mode 100644 index 000..8da8e42 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapperTest.php @@ -0,0 +1,30 @@ +?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() { +
[MediaWiki-commits] [Gerrit] New testing wrapper to circumvent object access - change (mediawiki/core)
jenkins-bot has submitted this change and it was merged. Change subject: New testing wrapper to circumvent object access .. 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 --- M tests/TestsAutoLoader.php A tests/phpunit/data/helpers/WellProtectedClass.php A tests/phpunit/includes/TestingAccessWrapper.php A tests/phpunit/includes/TestingAccessWrapperTest.php 4 files changed, 100 insertions(+), 0 deletions(-) Approvals: Anomie: Looks good to me, approved jenkins-bot: Verified diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index b410898..b0f29d5 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -47,6 +47,9 @@ '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 000..7114cc9 --- /dev/null +++ b/tests/phpunit/data/helpers/WellProtectedClass.php @@ -0,0 +1,17 @@ +?php + +class WellProtectedClass { + protected $property; + + public function __construct() { + $this-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 000..d4ad363 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapper.php @@ -0,0 +1,50 @@ +?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 ); + } +} diff --git a/tests/phpunit/includes/TestingAccessWrapperTest.php b/tests/phpunit/includes/TestingAccessWrapperTest.php new file mode 100644 index 000..8da8e42 --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapperTest.php @@ -0,0 +1,30 @@ +?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 ); +
[MediaWiki-commits] [Gerrit] New testing wrapper to circumvent object access - change (mediawiki/core)
Awight has uploaded a new change for review. https://gerrit.wikimedia.org/r/193388 Change subject: New testing wrapper to circumvent object access .. 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. Change-Id: I958d55df18c74e9d2b25d98cd0316989a0fbbe6f --- M tests/TestsAutoLoader.php A tests/phpunit/includes/TestingAccessWrapper.php 2 files changed, 53 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/88/193388/1 diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 4ed28a8..684965e 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -48,6 +48,9 @@ '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/includes/TestingAccessWrapper.php b/tests/phpunit/includes/TestingAccessWrapper.php new file mode 100644 index 000..2da6f6f --- /dev/null +++ b/tests/phpunit/includes/TestingAccessWrapper.php @@ -0,0 +1,50 @@ +?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( ReflectionMethod::IS_PUBLIC ); + return $methodReflection-invoke( $this-object, $args ); + } + + public function __set( $name, $value ) { + $classReflection = new ReflectionClass( $this-object ); + $propertyReflection = $classReflection-getProperty( $name ); + $propertyReflection-setAccessible( ReflectionProperty::IS_PUBLIC ); + $propertyReflection-setValue( $this-object, $value ); + } + + public function __get( $name ) { + $classReflection = new ReflectionClass( $this-object ); + $propertyReflection = $classReflection-getProperty( $name ); + $propertyReflection-setAccessible( ReflectionProperty::IS_PUBLIC ); + return $propertyReflection-getValue( $this-object ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/193388 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I958d55df18c74e9d2b25d98cd0316989a0fbbe6f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Awight awi...@wikimedia.org ___ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits