Giter Site home page Giter Site logo

global-state's Introduction

Hi!

I am Sebastian Bergmann. If you develop software using the PHP programming language then you may have come across my name. I am the creator and maintainer of PHPUnit as well as many other libraries and tools that are commonly used.


๐Ÿ‘ท Check out what I'm currently working on


๐Ÿ”ญ Latest releases I've contributed to


๐Ÿ“ซ How to reach me

global-state's People

Contributors

anthonyvancauwenberghe avatar grahamcampbell avatar henriquemoody avatar kfrohwein avatar localheinz avatar lolli42 avatar mbeccati avatar mrclay avatar remicollet avatar seb35 avatar sebastianbergmann avatar whatthejeff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

global-state's Issues

v2 compatibility with PHP 7.4

Is this supported ?

Everything seems OK, with v3 (used by PHPUnit 8), but not with v2 (used by PHPUnit 7)

Running PHPUnit 7 test suite

1) PHPUnit\Framework\TestCaseTest::testStaticAttributesBackupPre
Exception: Serialization of 'ReflectionClass' is not allowed

/usr/share/php/SebastianBergmann/GlobalState2/Snapshot.php:304
/usr/share/php/SebastianBergmann/GlobalState2/Snapshot.php:116
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestCase.php:1937
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestCase.php:1858
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestCase.php:815
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestResult.php:693
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestCase.php:796
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestSuite.php:746
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestSuite.php:746
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/Framework/TestSuite.php:746
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/TextUI/TestRunner.php:652
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/TextUI/Command.php:206
/dev/shm/BUILDROOT/phpunit7-7.5.12-2.fc29.remi.x86_64/usr/share/php/PHPUnit7/TextUI/Command.php:162

php7 Compatibility problem

vendor/sebastian/global-state/src/Snapshot.php

FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE

383 | WARNING | INI directive 'register_long_arrays' is deprecated since PHP 5.3 and removed since PHP 5.4

1.1.0 destroys globals after the first test method in PHPUnit

Our bootstrap sets a couple global stdClass vars and we use the default options in our config. After a recent composer update I noticed these globals get removed after the first test method in the suite, leading to Creating default object from empty value warnings when we write to them.

Forcing composer to use the "1.0" global-state fixes this problem.

Closure can't be serialized

In my current project, I've get the error "Closure can't be serialized", without any informations (lines, or files) .

So, with debugging, I've found a solution :

In src/Snapshot.php, in the function snapshotGlobals(), you try to serialize, and unserialize, this crash if it's a closure .

So, I've updated the function canBeSerialized(), with (on the begining of the function) :

if($variable instanceof \Closure)
            return false;
    
        if(is_array( $variable) || $variable instanceof \Iterator){
            foreach ( $variable as $item ) {
                if($item instanceof \Closure)
                    return false;
            }
        }

So, I check if $variable is a Closure, or if it's an array containing a closure .

In my case, the var $GLOBALS["functions"] contain two function, the phpunit autoloader, and the autoloader I write in my custom bootstrap file .

( In fact, the project I test doesn't respect the specs about spl_autoload_register, so I need to unregister this, and register a new function, respecting the specs )

I use phpunit 5.7.26

with

PHP 5.6.24 (cli) (built: Jul 20 2016 21:19:29)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

On windows

[Exception] Serialization of 'Closure' is not allowed

As described at this post on Stack Overflow, there's an error caused by line 313 of Snapshot.php.

Looks like it's a simple fix, just remove the serialization / unserialization.

Here's the output I get when trying to run my own unit test (using Codeception on top of PHPUnit):

Test  tests/unit/TemplateHeirarchyTest.php:testContentTypeIsHTML
                                                         
  [Exception] Serialization of 'Closure' is not allowed  
                                                     
#1  /Users/nabhacosley/Sites/ananda-designs/plugins/ananda-x/vendor/sebastian/global-state/src/Snapshot.php:313
#2  /Users/nabhacosley/Sites/ananda-designs/plugins/ananda-x/vendor/sebastian/global-state/src/Snapshot.php:121

ERRORS!
Tests: 3, Assertions: 1, Errors: 1.
>> Exited with code: 1.
>> Error executing child process: Error: Command failed: /bin/sh -c php `vendor/codeception/codeception/codecept run

Modernize build automation

  • Use GitHub Actions instead of Travis CI
  • Have Composer in tools/composer and managed through composer self-update (see update-tools target in build.xml
  • Install Psalm using Phive as tools/psalm (phive install --copy psalm)
  • Install PHP-CS-Fixer using Phive as tools/php-cs-fixer (phive install --copy php-cs-fixer)
  • Create Psalm configuration
  • Add Psalm build step to GitHub Actions-based CI workflow
  • Add PHP-CS-Fixer build step to GitHub Actions-based CI workflow

Attempted to serialize unserializable builtin class PDO

This error occurs when a global variable is present that has an array containing a PDO instance assigned to it.

The issue appears to stem from lack of support for this particular scenario in Snapshot->canBeSerialized().

I encountered this behavior while running PHPUnit tests in an environment where PHP was configured to use xhprof, which unfortunately uses a global for its configuration and contains a PDO instance in said configuration.

It's possible this issue may be resolved with #3. Until it is, it appears there's no way to run PHPUnit with @backupGlobals and xhprof enabled without modifying xhprof core files.

Restorer::restoreStaticAttributes() fails to restore static attributes for auto-loaded classes

A fix for this was originally made for issue #4

The fix doesn't work as explained in a comment on the commit:
23af31f

Example:

spl_autoload_register(function($className) {
    if ($className === 'AutoloadedClass') {
        class AutoloadedClass
        {
            public static $staticProperty = '';
        }
    }
});

// Create snapshot
$snap = new SebastianBergmann\GlobalState\Snapshot();

// Update static property (also auto-loads class)
AutoloadedClass::$staticProperty = 'new property value';

// Restore
$restorer = new SebastianBergmann\GlobalState\Restorer();
$restorer->restoreStaticAttributes($snap);

// Show that the static property wasn't restored
var_dump(AutoloadedClass::$staticProperty);

Null variables are not restored

When a global variable has the value null it is backuped, but the restore operation unset the variable instead of restoring its value null. It is misleading when a variable has to exist, particularly when global variables are accessed via $GLOBALS['myVariable'].

Tested with PHPUnit 6.1.1 (9fbc261d90) and global-state 1.1.1 (bc37d50). See minimal, self-contained, reproducing test case below.


This issue has origins in recent versions of MediaWiki when backupGlobals is enabled -- it is not the case for default CI but it is the case in a MediaWiki extension I am developing.

The context is: MediaWiki has most of its configuration in global variables (it is slowly changing) and some have the value null. The new MediaWiki configuration system checks if a configuration parameter is defined in the corresponding global variable, else it throws an exception. Because of this issue, some configuration parameters have the value null in the first test, but the variable is deleted before the second test, and consequently an exception is thrown if the second test need this specific configuration parameter.


Minimal, self-contained, reproducing test case:

<?php

/**
 * Example showing that null values in global variables are not restored.
 *
 * @backupGlobals enabled
 */
class NoNullRestoreTest extends PHPUnit\Framework\TestCase {

	static function setUpBeforeClass() {
		$GLOBALS['myVarBool'] = false;
		$GLOBALS['myVarNull'] = null;
	}

	function testNoNullRestoreWorking() {
		$this->assertEquals( false, $GLOBALS['myVarBool'] );
		$this->assertArrayHasKey( 'myVarNull', $GLOBALS );
		$this->assertEquals( null, $GLOBALS['myVarNull'] );
	}

	function testNoNullRestoreFailing() {
		$this->assertEquals( false, $GLOBALS['myVarBool'] );
		$this->assertArrayHasKey( 'myVarNull', $GLOBALS );
		$this->assertEquals( null, $GLOBALS['myVarNull'] );
	}
}
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 46 ms, Memory: 4.00MB

There was 1 failure:

1) NoNullRestoreTest::testNoNullRestoreFailing
Failed asserting that an array has the key 'myVarNull'.

/tmp/test-phpunit/NoNullRestoreTest.php:23

FAILURES!
Tests: 2, Assertions: 5, Failures: 1.

The tests pass when backupGlobals is disabled.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.