jenkins-bot has submitted this change and it was merged.

Change subject: In MediaWikiTestCase::stashMwGlobals(), prefer shallow copies
......................................................................


In MediaWikiTestCase::stashMwGlobals(), prefer shallow copies

MediaWikiTestCase::stashMwGlobals() performs a deep copy of globals by
serializing and then unserializing them. This is actually unwarranted in the
common case of simple scalar values and flat arrays -- and it's expensive, too.
So before attempting a deep copy, first check if a shallow copy will do.

Change-Id: Iaba1c8e1f6bae9de0a7a1fb411cac94f7e4dfb23
---
M tests/phpunit/MediaWikiTestCase.php
1 file changed, 38 insertions(+), 6 deletions(-)

Approvals:
  Krinkle: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/phpunit/MediaWikiTestCase.php 
b/tests/phpunit/MediaWikiTestCase.php
index 9f3aa11a..176ec2c 100644
--- a/tests/phpunit/MediaWikiTestCase.php
+++ b/tests/phpunit/MediaWikiTestCase.php
@@ -598,6 +598,29 @@
        }
 
        /**
+        * Check if we can back up a value by performing a shallow copy.
+        * Values which fail this test are copied recursively.
+        *
+        * @param mixed $value
+        * @return bool True if a shallow copy will do; false if a deep copy
+        *  is required.
+        */
+       private static function canShallowCopy( $value ) {
+               if ( is_scalar( $value ) || $value === null ) {
+                       return true;
+               }
+               if ( is_array( $value ) ) {
+                       foreach ( $value as $subValue ) {
+                               if ( !is_scalar( $subValue ) && $subValue !== 
null ) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+               return false;
+       }
+
+       /**
         * Stashes the global, will be restored in tearDown()
         *
         * Individual test functions may override globals through the 
setMwGlobals() function
@@ -632,13 +655,22 @@
                                // NOTE: we serialize then unserialize the 
value in case it is an object
                                // this stops any objects being passed by 
reference. We could use clone
                                // and if is_object but this does account for 
objects within objects!
-                               try {
-                                       $this->mwGlobals[$globalKey] = 
unserialize( serialize( $GLOBALS[$globalKey] ) );
-                               }
-                                       // NOTE; some things such as Closures 
are not serializable
-                                       // in this case just set the value!
-                               catch ( Exception $e ) {
+                               if ( self::canShallowCopy( $GLOBALS[$globalKey] 
) ) {
                                        $this->mwGlobals[$globalKey] = 
$GLOBALS[$globalKey];
+                               } elseif (
+                                       // Many MediaWiki types are safe to 
clone. These are the
+                                       // ones that are most commonly stashed.
+                                       $GLOBALS[$globalKey] instanceof 
Language ||
+                                       $GLOBALS[$globalKey] instanceof User ||
+                                       $GLOBALS[$globalKey] instanceof 
FauxRequest
+                               ) {
+                                       $this->mwGlobals[$globalKey] = clone 
$GLOBALS[$globalKey];
+                               } else {
+                                       try {
+                                               $this->mwGlobals[$globalKey] = 
unserialize( serialize( $GLOBALS[$globalKey] ) );
+                                       } catch ( Exception $e ) {
+                                               $this->mwGlobals[$globalKey] = 
$GLOBALS[$globalKey];
+                                       }
                                }
                        }
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/289365
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iaba1c8e1f6bae9de0a7a1fb411cac94f7e4dfb23
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to