/**
  * 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]);
         }
     }
 }
Esempio n. 2
0
 /**
  * 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);
         }
     }
 }
 public function testStaticAttributes()
 {
     $blacklist = $this->getBlacklist();
     $blacklist->method('isStaticAttributeBlacklisted')->willReturnCallback(function ($class) {
         return $class !== 'SebastianBergmann\\GlobalState\\TestFixture\\SnapshotClass';
     });
     SnapshotClass::init();
     $snapshot = new Snapshot($blacklist, false, true, false, false, false, false, false, false, false);
     $expected = array('SebastianBergmann\\GlobalState\\TestFixture\\SnapshotClass' => array('string' => 'snapshot', 'arrayObject' => new ArrayObject(array(1, 2, 3)), 'stdClass' => new \stdClass()));
     $this->assertEquals($expected, $snapshot->staticAttributes());
 }
Esempio n. 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");
     }
 }