Example #1
0
 /**
  * Restores all static attributes in user-defined classes from this snapshot.
  *
  * @param Snapshot $snapshot
  */
 public function restoreStaticAttributes(Snapshot $snapshot)
 {
     $current = new Snapshot($snapshot->blacklist(), FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE);
     $newClasses = array_diff($current->classes(), $snapshot->classes());
     unset($current);
     foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
         foreach ($staticAttributes as $name => $value) {
             $reflector = new ReflectionProperty($className, $name);
             $reflector->setAccessible(TRUE);
             $reflector->setValue($value);
         }
     }
     foreach ($newClasses as $className) {
         $class = new \ReflectionClass($className);
         $defaults = $class->getDefaultProperties();
         foreach ($class->getProperties() as $attribute) {
             if (!$attribute->isStatic()) {
                 continue;
             }
             $name = $attribute->getName();
             if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
                 continue;
             }
             if (!isset($defaults[$name])) {
                 continue;
             }
             $attribute->setAccessible(TRUE);
             $attribute->setValue($defaults[$name]);
         }
     }
 }
Example #2
0
 /**
  * Restores a super-global variable array from this snapshot.
  *
  * @param Snapshot $snapshot
  * @param $superGlobalArray
  */
 private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
 {
     $superGlobalVariables = $snapshot->superGlobalVariables();
     if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray]) && isset($superGlobalVariables[$superGlobalArray])) {
         $keys = array_keys(array_merge($GLOBALS[$superGlobalArray], $superGlobalVariables[$superGlobalArray]));
         foreach ($keys as $key) {
             if (isset($superGlobalVariables[$superGlobalArray][$key])) {
                 $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
             } else {
                 unset($GLOBALS[$superGlobalArray][$key]);
             }
         }
     }
 }
Example #3
0
 private function restoreAllGlobals()
 {
     $superGlobalArrays = $this->globalStateSnapshot->superGlobalArrays();
     $globalVariables = $this->globalStateSnapshot->globalVariables();
     foreach (array_keys($globalVariables) as $key) {
         if ($key != 'GLOBALS' && !in_array($key, $superGlobalArrays) && !$this->globalStateSnapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
             if (!isset($GLOBALS[$key])) {
                 $GLOBALS[$key] = $globalVariables[$key];
             }
         }
     }
 }
Example #4
0
 /**
  * @param  Snapshot $before
  * @param  Snapshot $after
  * @throws PHPUnit_Framework_RiskyTestError
  */
 private function compareGlobalStateSnapshots(Snapshot $before, Snapshot $after)
 {
     $backupGlobals = $this->backupGlobals === null || $this->backupGlobals === true;
     if ($backupGlobals) {
         $this->compareGlobalStateSnapshotPart($before->globalVariables(), $after->globalVariables(), "--- Global variables before the test\n+++ Global variables after the test\n");
         $this->compareGlobalStateSnapshotPart($before->superGlobalVariables(), $after->superGlobalVariables(), "--- Super-global variables before the test\n+++ Super-global variables after the test\n");
     }
     if ($this->backupStaticAttributes) {
         $this->compareGlobalStateSnapshotPart($before->staticAttributes(), $after->staticAttributes(), "--- Static attributes before the test\n+++ Static attributes after the test\n");
     }
 }
 public function testIncludedFiles()
 {
     $snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, false, false, false, false, true);
     $this->assertContains(__FILE__, $snapshot->includedFiles());
 }
 /**
  * Restores all static attributes in user-defined classes from this snapshot.
  *
  * @param Snapshot $snapshot
  */
 public function restoreStaticAttributes(Snapshot $snapshot)
 {
     foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
         foreach ($staticAttributes as $name => $value) {
             $reflector = new ReflectionProperty($className, $name);
             $reflector->setAccessible(true);
             $reflector->setValue($value);
         }
     }
 }