Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -1,6 +0,0 @@
.idea
composer.lock
composer.phar
vendor/
cache.properties
phpunit.xml

View File

@@ -1,20 +0,0 @@
language: php
php:
- 5.3.3
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm
sudo: false
before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev
script: ./vendor/bin/phpunit
notifications:
email: false

View File

@@ -0,0 +1,79 @@
# Changes in sebastian/global-state
All notable changes in `sebastian/global-state` are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
## [5.0.5] - 2022-02-14
### Fixed
* [#34](https://github.com/sebastianbergmann/global-state/pull/34): Uninitialised typed static properties are not handled correctly
## [5.0.4] - 2022-02-10
### Fixed
* The `$includeTraits` parameter of `SebastianBergmann\GlobalState\Snapshot::__construct()` is not respected
## [5.0.3] - 2021-06-11
### Changed
* `SebastianBergmann\GlobalState\CodeExporter::globalVariables()` now generates code that is compatible with PHP 8.1
## [5.0.2] - 2020-10-26
### Fixed
* `SebastianBergmann\GlobalState\Exception` now correctly extends `\Throwable`
## [5.0.1] - 2020-09-28
### Changed
* Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3`
## [5.0.0] - 2020-08-07
### Changed
* The `SebastianBergmann\GlobalState\Blacklist` class has been renamed to `SebastianBergmann\GlobalState\ExcludeList`
## [4.0.0] - 2020-02-07
### Removed
* This component is no longer supported on PHP 7.2
## [3.0.2] - 2022-02-10
### Fixed
* The `$includeTraits` parameter of `SebastianBergmann\GlobalState\Snapshot::__construct()` is not respected
## [3.0.1] - 2020-11-30
### Changed
* Changed PHP version constraint in `composer.json` from `^7.2` to `>=7.2`
## [3.0.0] - 2019-02-01
### Changed
* `Snapshot::canBeSerialized()` now recursively checks arrays and object graphs for variables that cannot be serialized
### Removed
* This component is no longer supported on PHP 7.0 and PHP 7.1
[5.0.5]: https://github.com/sebastianbergmann/global-state/compare/5.0.4...5.0.5
[5.0.4]: https://github.com/sebastianbergmann/global-state/compare/5.0.3...5.0.4
[5.0.3]: https://github.com/sebastianbergmann/global-state/compare/5.0.2...5.0.3
[5.0.2]: https://github.com/sebastianbergmann/global-state/compare/5.0.1...5.0.2
[5.0.1]: https://github.com/sebastianbergmann/global-state/compare/5.0.0...5.0.1
[5.0.0]: https://github.com/sebastianbergmann/global-state/compare/4.0.0...5.0.0
[4.0.0]: https://github.com/sebastianbergmann/global-state/compare/3.0.2...4.0.0
[3.0.2]: https://github.com/sebastianbergmann/phpunit/compare/3.0.1...3.0.2
[3.0.1]: https://github.com/sebastianbergmann/phpunit/compare/3.0.0...3.0.1
[3.0.0]: https://github.com/sebastianbergmann/phpunit/compare/2.0.0...3.0.0

View File

@@ -1,6 +1,6 @@
GlobalState
sebastian/global-state
Copyright (c) 2001-2015, Sebastian Bergmann <sebastian@phpunit.de>.
Copyright (c) 2001-2022, Sebastian Bergmann <sebastian@phpunit.de>.
All rights reserved.
Redistribution and use in source and binary forms, with or without

View File

@@ -1,15 +1,20 @@
# GlobalState
# sebastian/global-state
[![CI Status](https://github.com/sebastianbergmann/global-state/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/global-state/actions)
[![Type Coverage](https://shepherd.dev/github/sebastianbergmann/global-state/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/global-state)
Snapshotting of global state, factored out of PHPUnit into a stand-alone component.
[![Build Status](https://travis-ci.org/sebastianbergmann/global-state.svg?branch=master)](https://travis-ci.org/sebastianbergmann/global-state)
## Installation
To add this package as a local, per-project dependency to your project, simply add a dependency on `sebastian/global-state` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on GlobalState:
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
{
"require": {
"sebastian/global-state": "1.0.*"
}
}
```
composer require sebastian/global-state
```
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
```
composer require --dev sebastian/global-state
```

View File

@@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="global-state">
<target name="clean" description="Cleanup build artifacts">
<delete dir="${basedir}/vendor"/>
<delete file="${basedir}/composer.lock"/>
</target>
<target name="composer" depends="clean" description="Install dependencies with Composer">
<tstamp>
<format property="thirty.days.ago" pattern="MM/dd/yyyy hh:mm aa" offset="-30" unit="day"/>
</tstamp>
<delete>
<fileset dir="${basedir}">
<include name="composer.phar" />
<date datetime="${thirty.days.ago}" when="before"/>
</fileset>
</delete>
<get src="https://getcomposer.org/composer.phar" dest="${basedir}/composer.phar" skipexisting="true"/>
<exec executable="php">
<arg value="composer.phar"/>
<arg value="install"/>
</exec>
</target>
<target name="phpunit" description="Run unit tests with PHPUnit">
<exec executable="${basedir}/vendor/bin/phpunit" failonerror="true">
<arg value="--configuration"/>
<arg path="${basedir}/build/phpunit.xml"/>
</exec>
</target>
</project>

View File

@@ -10,11 +10,22 @@
"email": "sebastian@phpunit.de"
}
],
"prefer-stable": true,
"config": {
"platform": {
"php": "7.3.0"
},
"optimize-autoloader": true,
"sort-packages": true
},
"require": {
"php": ">=5.3.3"
"php": ">=7.3",
"sebastian/object-reflector": "^2.0",
"sebastian/recursion-context": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.2"
"ext-dom": "*",
"phpunit/phpunit": "^9.3"
},
"suggest": {
"ext-uopz": "*"
@@ -27,11 +38,14 @@
"autoload-dev": {
"classmap": [
"tests/_fixture/"
],
"files": [
"tests/_fixture/SnapshotFunctions.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "5.0-dev"
}
}
}

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
verbose="true">
<testsuite name="GlobalState">
<directory suffix="Test.php">tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<php>
<const name="GLOBALSTATE_TESTSUITE" value="true"/>
<ini name="date.timezone" value="Etc/UTC"/>
</php>
</phpunit>

View File

@@ -1,25 +1,27 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use const PHP_EOL;
use function is_array;
use function is_scalar;
use function serialize;
use function sprintf;
use function var_export;
/**
* Exports parts of a Snapshot as PHP code.
*/
class CodeExporter
final class CodeExporter
{
/**
* @param Snapshot $snapshot
* @return string
*/
public function constants(Snapshot $snapshot)
public function constants(Snapshot $snapshot): string
{
$result = '';
@@ -35,11 +37,33 @@ class CodeExporter
return $result;
}
/**
* @param Snapshot $snapshot
* @return string
*/
public function iniSettings(Snapshot $snapshot)
public function globalVariables(Snapshot $snapshot): string
{
$result = <<<'EOT'
call_user_func(
function ()
{
foreach (array_keys($GLOBALS) as $key) {
unset($GLOBALS[$key]);
}
}
);
EOT;
foreach ($snapshot->globalVariables() as $name => $value) {
$result .= sprintf(
'$GLOBALS[%s] = %s;' . PHP_EOL,
$this->exportVariable($name),
$this->exportVariable($value)
);
}
return $result;
}
public function iniSettings(Snapshot $snapshot): string
{
$result = '';
@@ -54,13 +78,9 @@ class CodeExporter
return $result;
}
/**
* @param mixed $variable
* @return string
*/
private function exportVariable($variable)
private function exportVariable($variable): string
{
if (is_scalar($variable) || is_null($variable) ||
if (is_scalar($variable) || null === $variable ||
(is_array($variable) && $this->arrayOnlyContainsScalars($variable))) {
return var_export($variable, true);
}
@@ -68,18 +88,14 @@ class CodeExporter
return 'unserialize(' . var_export(serialize($variable), true) . ')';
}
/**
* @param array $array
* @return bool
*/
private function arrayOnlyContainsScalars(array $array)
private function arrayOnlyContainsScalars(array $array): bool
{
$result = true;
foreach ($array as $element) {
if (is_array($element)) {
$result = self::arrayOnlyContainsScalars($element);
} elseif (!is_scalar($element) && !is_null($element)) {
$result = $this->arrayOnlyContainsScalars($element);
} elseif (!is_scalar($element) && null !== $element) {
$result = false;
}

View File

@@ -1,122 +1,92 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use function in_array;
use function strpos;
use ReflectionClass;
/**
* A blacklist for global state elements that should not be snapshotted.
*/
class Blacklist
final class ExcludeList
{
/**
* @var array
*/
private $globalVariables = array();
private $globalVariables = [];
/**
* @var string[]
*/
private $classes = [];
/**
* @var string[]
*/
private $classNamePrefixes = [];
/**
* @var string[]
*/
private $parentClasses = [];
/**
* @var string[]
*/
private $interfaces = [];
/**
* @var array
*/
private $classes = array();
private $staticAttributes = [];
/**
* @var array
*/
private $classNamePrefixes = array();
/**
* @var array
*/
private $parentClasses = array();
/**
* @var array
*/
private $interfaces = array();
/**
* @var array
*/
private $staticAttributes = array();
/**
* @param string $variableName
*/
public function addGlobalVariable($variableName)
public function addGlobalVariable(string $variableName): void
{
$this->globalVariables[$variableName] = true;
}
/**
* @param string $className
*/
public function addClass($className)
public function addClass(string $className): void
{
$this->classes[] = $className;
}
/**
* @param string $className
*/
public function addSubclassesOf($className)
public function addSubclassesOf(string $className): void
{
$this->parentClasses[] = $className;
}
/**
* @param string $interfaceName
*/
public function addImplementorsOf($interfaceName)
public function addImplementorsOf(string $interfaceName): void
{
$this->interfaces[] = $interfaceName;
}
/**
* @param string $classNamePrefix
*/
public function addClassNamePrefix($classNamePrefix)
public function addClassNamePrefix(string $classNamePrefix): void
{
$this->classNamePrefixes[] = $classNamePrefix;
}
/**
* @param string $className
* @param string $attributeName
*/
public function addStaticAttribute($className, $attributeName)
public function addStaticAttribute(string $className, string $attributeName): void
{
if (!isset($this->staticAttributes[$className])) {
$this->staticAttributes[$className] = array();
$this->staticAttributes[$className] = [];
}
$this->staticAttributes[$className][$attributeName] = true;
}
/**
* @param string $variableName
* @return bool
*/
public function isGlobalVariableBlacklisted($variableName)
public function isGlobalVariableExcluded(string $variableName): bool
{
return isset($this->globalVariables[$variableName]);
}
/**
* @param string $className
* @param string $attributeName
* @return bool
*/
public function isStaticAttributeBlacklisted($className, $attributeName)
public function isStaticAttributeExcluded(string $className, string $attributeName): bool
{
if (in_array($className, $this->classes)) {
if (in_array($className, $this->classes, true)) {
return true;
}

View File

@@ -1,15 +1,23 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use function array_diff;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function function_exists;
use function get_defined_functions;
use function in_array;
use function is_array;
use ReflectionClass;
use ReflectionProperty;
/**
@@ -20,11 +28,11 @@ class Restorer
/**
* Deletes function definitions that are not defined in a snapshot.
*
* @param Snapshot $snapshot
* @throws RuntimeException when the uopz_delete() function is not available
* @see https://github.com/krakjoe/uopz
*
* @see https://github.com/krakjoe/uopz
*/
public function restoreFunctions(Snapshot $snapshot)
public function restoreFunctions(Snapshot $snapshot): void
{
if (!function_exists('uopz_delete')) {
throw new RuntimeException('The uopz_delete() function is required for this operation');
@@ -39,10 +47,8 @@ class Restorer
/**
* Restores all global and super-global variables from a snapshot.
*
* @param Snapshot $snapshot
*/
public function restoreGlobalVariables(Snapshot $snapshot)
public function restoreGlobalVariables(Snapshot $snapshot): void
{
$superGlobalArrays = $snapshot->superGlobalArrays();
@@ -53,10 +59,10 @@ class Restorer
$globalVariables = $snapshot->globalVariables();
foreach (array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
!$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
if (isset($globalVariables[$key])) {
if ($key !== 'GLOBALS' &&
!in_array($key, $superGlobalArrays, true) &&
!$snapshot->excludeList()->isGlobalVariableExcluded($key)) {
if (array_key_exists($key, $globalVariables)) {
$GLOBALS[$key] = $globalVariables[$key];
} else {
unset($GLOBALS[$key]);
@@ -67,13 +73,12 @@ class Restorer
/**
* Restores all static attributes in user-defined classes from this snapshot.
*
* @param Snapshot $snapshot
*/
public function restoreStaticAttributes(Snapshot $snapshot)
public function restoreStaticAttributes(Snapshot $snapshot): void
{
$current = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
$current = new Snapshot($snapshot->excludeList(), false, false, false, false, true, false, false, false, false);
$newClasses = array_diff($current->classes(), $snapshot->classes());
unset($current);
foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
@@ -85,7 +90,7 @@ class Restorer
}
foreach ($newClasses as $className) {
$class = new \ReflectionClass($className);
$class = new ReflectionClass($className);
$defaults = $class->getDefaultProperties();
foreach ($class->getProperties() as $attribute) {
@@ -95,7 +100,7 @@ class Restorer
$name = $attribute->getName();
if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
if ($snapshot->excludeList()->isStaticAttributeExcluded($className, $name)) {
continue;
}
@@ -111,11 +116,8 @@ class Restorer
/**
* Restores a super-global variable array from this snapshot.
*
* @param Snapshot $snapshot
* @param $superGlobalArray
*/
private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
private function restoreSuperGlobalArray(Snapshot $snapshot, string $superGlobalArray): void
{
$superGlobalVariables = $snapshot->superGlobalVariables();

View File

@@ -1,17 +1,37 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use const PHP_VERSION_ID;
use function array_keys;
use function array_merge;
use function array_reverse;
use function func_get_args;
use function get_declared_classes;
use function get_declared_interfaces;
use function get_declared_traits;
use function get_defined_constants;
use function get_defined_functions;
use function get_included_files;
use function in_array;
use function ini_get_all;
use function is_array;
use function is_object;
use function is_resource;
use function is_scalar;
use function serialize;
use function unserialize;
use ReflectionClass;
use Serializable;
use SebastianBergmann\ObjectReflector\ObjectReflector;
use SebastianBergmann\RecursionContext\Context;
use Throwable;
/**
* A snapshot of global state.
@@ -19,86 +39,71 @@ use Serializable;
class Snapshot
{
/**
* @var Blacklist
* @var ExcludeList
*/
private $blacklist;
private $excludeList;
/**
* @var array
*/
private $globalVariables = array();
private $globalVariables = [];
/**
* @var array
*/
private $superGlobalArrays = array();
private $superGlobalArrays = [];
/**
* @var array
*/
private $superGlobalVariables = array();
private $superGlobalVariables = [];
/**
* @var array
*/
private $staticAttributes = array();
private $staticAttributes = [];
/**
* @var array
*/
private $iniSettings = array();
private $iniSettings = [];
/**
* @var array
*/
private $includedFiles = array();
private $includedFiles = [];
/**
* @var array
*/
private $constants = array();
private $constants = [];
/**
* @var array
*/
private $functions = array();
private $functions = [];
/**
* @var array
*/
private $interfaces = array();
private $interfaces = [];
/**
* @var array
*/
private $classes = array();
private $classes = [];
/**
* @var array
*/
private $traits = array();
private $traits = [];
/**
* Creates a snapshot of the current global state.
*
* @param Blacklist $blacklist
* @param bool $includeGlobalVariables
* @param bool $includeStaticAttributes
* @param bool $includeConstants
* @param bool $includeFunctions
* @param bool $includeClasses
* @param bool $includeInterfaces
* @param bool $includeTraits
* @param bool $includeIniSettings
* @param bool $includeIncludedFiles
*/
public function __construct(Blacklist $blacklist = null, $includeGlobalVariables = true, $includeStaticAttributes = true, $includeConstants = true, $includeFunctions = true, $includeClasses = true, $includeInterfaces = true, $includeTraits = true, $includeIniSettings = true, $includeIncludedFiles = true)
public function __construct(ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticAttributes = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true)
{
if ($blacklist === null) {
$blacklist = new Blacklist;
}
$this->blacklist = $blacklist;
$this->excludeList = $excludeList ?: new ExcludeList;
if ($includeConstants) {
$this->snapshotConstants();
@@ -133,105 +138,67 @@ class Snapshot
$this->includedFiles = get_included_files();
}
if (function_exists('get_declared_traits')) {
if ($includeTraits) {
$this->traits = get_declared_traits();
}
}
/**
* @return Blacklist
*/
public function blacklist()
public function excludeList(): ExcludeList
{
return $this->blacklist;
return $this->excludeList;
}
/**
* @return array
*/
public function globalVariables()
public function globalVariables(): array
{
return $this->globalVariables;
}
/**
* @return array
*/
public function superGlobalVariables()
public function superGlobalVariables(): array
{
return $this->superGlobalVariables;
}
/**
* Returns a list of all super-global variable arrays.
*
* @return array
*/
public function superGlobalArrays()
public function superGlobalArrays(): array
{
return $this->superGlobalArrays;
}
/**
* @return array
*/
public function staticAttributes()
public function staticAttributes(): array
{
return $this->staticAttributes;
}
/**
* @return array
*/
public function iniSettings()
public function iniSettings(): array
{
return $this->iniSettings;
}
/**
* @return array
*/
public function includedFiles()
public function includedFiles(): array
{
return $this->includedFiles;
}
/**
* @return array
*/
public function constants()
public function constants(): array
{
return $this->constants;
}
/**
* @return array
*/
public function functions()
public function functions(): array
{
return $this->functions;
}
/**
* @return array
*/
public function interfaces()
public function interfaces(): array
{
return $this->interfaces;
}
/**
* @return array
*/
public function classes()
public function classes(): array
{
return $this->classes;
}
/**
* @return array
*/
public function traits()
public function traits(): array
{
return $this->traits;
}
@@ -239,7 +206,7 @@ class Snapshot
/**
* Creates a snapshot user-defined constants.
*/
private function snapshotConstants()
private function snapshotConstants(): void
{
$constants = get_defined_constants(true);
@@ -251,7 +218,7 @@ class Snapshot
/**
* Creates a snapshot user-defined functions.
*/
private function snapshotFunctions()
private function snapshotFunctions(): void
{
$functions = get_defined_functions();
@@ -261,7 +228,7 @@ class Snapshot
/**
* Creates a snapshot user-defined classes.
*/
private function snapshotClasses()
private function snapshotClasses(): void
{
foreach (array_reverse(get_declared_classes()) as $className) {
$class = new ReflectionClass($className);
@@ -279,7 +246,7 @@ class Snapshot
/**
* Creates a snapshot user-defined interfaces.
*/
private function snapshotInterfaces()
private function snapshotInterfaces(): void
{
foreach (array_reverse(get_declared_interfaces()) as $interfaceName) {
$class = new ReflectionClass($interfaceName);
@@ -297,7 +264,7 @@ class Snapshot
/**
* Creates a snapshot of all global and super-global variables.
*/
private function snapshotGlobals()
private function snapshotGlobals(): void
{
$superGlobalArrays = $this->superGlobalArrays();
@@ -306,10 +273,11 @@ class Snapshot
}
foreach (array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
if ($key !== 'GLOBALS' &&
!in_array($key, $superGlobalArrays, true) &&
$this->canBeSerialized($GLOBALS[$key]) &&
!$this->blacklist->isGlobalVariableBlacklisted($key)) {
!$this->excludeList->isGlobalVariableExcluded($key)) {
/* @noinspection UnserializeExploitsInspection */
$this->globalVariables[$key] = unserialize(serialize($GLOBALS[$key]));
}
}
@@ -317,15 +285,14 @@ class Snapshot
/**
* Creates a snapshot a super-global variable array.
*
* @param $superGlobalArray
*/
private function snapshotSuperGlobalArray($superGlobalArray)
private function snapshotSuperGlobalArray(string $superGlobalArray): void
{
$this->superGlobalVariables[$superGlobalArray] = array();
$this->superGlobalVariables[$superGlobalArray] = [];
if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
/* @noinspection UnserializeExploitsInspection */
$this->superGlobalVariables[$superGlobalArray][$key] = unserialize(serialize($value));
}
}
@@ -334,24 +301,30 @@ class Snapshot
/**
* Creates a snapshot of all static attributes in user-defined classes.
*/
private function snapshotStaticAttributes()
private function snapshotStaticAttributes(): void
{
foreach ($this->classes as $className) {
$class = new ReflectionClass($className);
$snapshot = array();
$snapshot = [];
foreach ($class->getProperties() as $attribute) {
if ($attribute->isStatic()) {
$name = $attribute->getName();
if ($this->blacklist->isStaticAttributeBlacklisted($className, $name)) {
if ($this->excludeList->isStaticAttributeExcluded($className, $name)) {
continue;
}
$attribute->setAccessible(true);
if (PHP_VERSION_ID >= 70400 && !$attribute->isInitialized()) {
continue;
}
$value = $attribute->getValue();
if ($this->canBeSerialized($value)) {
/* @noinspection UnserializeExploitsInspection */
$snapshot[$name] = unserialize(serialize($value));
}
}
@@ -365,59 +338,106 @@ class Snapshot
/**
* Returns a list of all super-global variable arrays.
*
* @return array
*/
private function setupSuperGlobalArrays()
private function setupSuperGlobalArrays(): void
{
$this->superGlobalArrays = array(
$this->superGlobalArrays = [
'_ENV',
'_POST',
'_GET',
'_COOKIE',
'_SERVER',
'_FILES',
'_REQUEST'
);
if (ini_get('register_long_arrays') == '1') {
$this->superGlobalArrays = array_merge(
$this->superGlobalArrays,
array(
'HTTP_ENV_VARS',
'HTTP_POST_VARS',
'HTTP_GET_VARS',
'HTTP_COOKIE_VARS',
'HTTP_SERVER_VARS',
'HTTP_POST_FILES'
)
);
}
'_REQUEST',
];
}
/**
* @param mixed $variable
* @return bool
* @todo Implement this properly
*/
private function canBeSerialized($variable)
private function canBeSerialized($variable): bool
{
if (!is_object($variable)) {
return !is_resource($variable);
}
if ($variable instanceof \stdClass) {
if (is_scalar($variable) || $variable === null) {
return true;
}
$class = new ReflectionClass($variable);
if (is_resource($variable)) {
return false;
}
do {
if ($class->isInternal()) {
return $variable instanceof Serializable;
foreach ($this->enumerateObjectsAndResources($variable) as $value) {
if (is_resource($value)) {
return false;
}
} while ($class = $class->getParentClass());
if (is_object($value)) {
$class = new ReflectionClass($value);
if ($class->isAnonymous()) {
return false;
}
try {
@serialize($value);
} catch (Throwable $t) {
return false;
}
}
}
return true;
}
private function enumerateObjectsAndResources($variable): array
{
if (isset(func_get_args()[1])) {
$processed = func_get_args()[1];
} else {
$processed = new Context;
}
$result = [];
if ($processed->contains($variable)) {
return $result;
}
$array = $variable;
$processed->add($variable);
if (is_array($variable)) {
foreach ($array as $element) {
if (!is_array($element) && !is_object($element) && !is_resource($element)) {
continue;
}
if (!is_resource($element)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$result = array_merge(
$result,
$this->enumerateObjectsAndResources($element, $processed)
);
} else {
$result[] = $element;
}
}
} else {
$result[] = $variable;
foreach ((new ObjectReflector)->getAttributes($variable) as $value) {
if (!is_array($value) && !is_object($value) && !is_resource($value)) {
continue;
}
if (!is_resource($value)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$result = array_merge(
$result,
$this->enumerateObjectsAndResources($value, $processed)
);
} else {
$result[] = $value;
}
}
}
return $result;
}
}

View File

@@ -1,17 +1,16 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
/**
*/
interface Exception
use Throwable;
interface Exception extends Throwable
{
}

View File

@@ -1,17 +1,14 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
/**
*/
class RuntimeException extends \RuntimeException implements Exception
final class RuntimeException extends \RuntimeException implements Exception
{
}

View File

@@ -1,113 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use PHPUnit_Framework_TestCase;
/**
*/
class BlacklistTest extends PHPUnit_Framework_TestCase
{
/**
* @var \SebastianBergmann\GlobalState\Blacklist
*/
private $blacklist;
protected function setUp()
{
$this->blacklist = new Blacklist;
}
public function testGlobalVariableThatIsNotBlacklistedIsNotTreatedAsBlacklisted()
{
$this->assertFalse($this->blacklist->isGlobalVariableBlacklisted('variable'));
}
public function testGlobalVariableCanBeBlacklisted()
{
$this->blacklist->addGlobalVariable('variable');
$this->assertTrue($this->blacklist->isGlobalVariableBlacklisted('variable'));
}
public function testStaticAttributeThatIsNotBlacklistedIsNotTreatedAsBlacklisted()
{
$this->assertFalse(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedClass',
'attribute'
)
);
}
public function testClassCanBeBlacklisted()
{
$this->blacklist->addClass('SebastianBergmann\GlobalState\TestFixture\BlacklistedClass');
$this->assertTrue(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedClass',
'attribute'
)
);
}
public function testSubclassesCanBeBlacklisted()
{
$this->blacklist->addSubclassesOf('SebastianBergmann\GlobalState\TestFixture\BlacklistedClass');
$this->assertTrue(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedChildClass',
'attribute'
)
);
}
public function testImplementorsCanBeBlacklisted()
{
$this->blacklist->addImplementorsOf('SebastianBergmann\GlobalState\TestFixture\BlacklistedInterface');
$this->assertTrue(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedImplementor',
'attribute'
)
);
}
public function testClassNamePrefixesCanBeBlacklisted()
{
$this->blacklist->addClassNamePrefix('SebastianBergmann\GlobalState');
$this->assertTrue(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedClass',
'attribute'
)
);
}
public function testStaticAttributeCanBeBlacklisted()
{
$this->blacklist->addStaticAttribute(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedClass',
'attribute'
);
$this->assertTrue(
$this->blacklist->isStaticAttributeBlacklisted(
'SebastianBergmann\GlobalState\TestFixture\BlacklistedClass',
'attribute'
)
);
}
}

View File

@@ -1,119 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use ArrayObject;
use PHPUnit_Framework_TestCase;
use SebastianBergmann\GlobalState\TestFixture\SnapshotClass;
/**
*/
class SnapshotTest extends PHPUnit_Framework_TestCase
{
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());
}
public function testConstants()
{
$snapshot = new Snapshot($this->getBlacklist(), false, false, true, false, false, false, false, false, false);
$this->assertArrayHasKey('GLOBALSTATE_TESTSUITE', $snapshot->constants());
}
public function testFunctions()
{
require_once __DIR__.'/_fixture/SnapshotFunctions.php';
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, true, false, false, false, false, false);
$functions = $snapshot->functions();
$this->assertThat(
$functions,
$this->logicalOr(
// Zend
$this->contains('sebastianbergmann\globalstate\testfixture\snapshotfunction'),
// HHVM
$this->contains('SebastianBergmann\GlobalState\TestFixture\snapshotFunction')
)
);
$this->assertNotContains('assert', $functions);
}
public function testClasses()
{
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, true, false, false, false, false);
$classes = $snapshot->classes();
$this->assertContains('PHPUnit_Framework_TestCase', $classes);
$this->assertNotContains('Exception', $classes);
}
public function testInterfaces()
{
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, false, true, false, false, false);
$interfaces = $snapshot->interfaces();
$this->assertContains('PHPUnit_Framework_Test', $interfaces);
$this->assertNotContains('Countable', $interfaces);
}
/**
* @requires PHP 5.4
*/
public function testTraits()
{
spl_autoload_call('SebastianBergmann\GlobalState\TestFixture\SnapshotTrait');
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, false, false, true, false, false);
$this->assertContains('SebastianBergmann\GlobalState\TestFixture\SnapshotTrait', $snapshot->traits());
}
public function testIniSettings()
{
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, false, false, false, true, false);
$iniSettings = $snapshot->iniSettings();
$this->assertArrayHasKey('date.timezone', $iniSettings);
$this->assertEquals('Etc/UTC', $iniSettings['date.timezone']);
}
public function testIncludedFiles()
{
$snapshot = new Snapshot($this->getBlacklist(), false, false, false, false, false, false, false, false, true);
$this->assertContains(__FILE__, $snapshot->includedFiles());
}
/**
* @return \SebastianBergmann\GlobalState\Blacklist
*/
private function getBlacklist()
{
return $this->getMockBuilder('SebastianBergmann\GlobalState\Blacklist')
->disableOriginalConstructor()
->getMock();
}
}

View File

@@ -1,17 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
/**
*/
class BlacklistedChildClass extends BlacklistedClass
{
}

View File

@@ -1,18 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
/**
*/
class BlacklistedClass
{
private static $attribute;
}

View File

@@ -1,18 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
/**
*/
class BlacklistedImplementor implements BlacklistedInterface
{
private static $attribute;
}

View File

@@ -1,17 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
/**
*/
interface BlacklistedInterface
{
}

View File

@@ -1,37 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
use DomDocument;
use ArrayObject;
/**
*/
class SnapshotClass
{
private static $string = 'snapshot';
private static $dom;
private static $closure;
private static $arrayObject;
private static $snapshotDomDocument;
private static $resource;
private static $stdClass;
public static function init()
{
self::$dom = new DomDocument();
self::$closure = function () {};
self::$arrayObject = new ArrayObject(array(1, 2, 3));
self::$snapshotDomDocument = new SnapshotDomDocument();
self::$resource = fopen('php://memory', 'r');
self::$stdClass = new \stdClass();
}
}

View File

@@ -1,19 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
use DomDocument;
/**
*/
class SnapshotDomDocument extends DomDocument
{
}

View File

@@ -1,15 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
function snapshotFunction()
{
}

View File

@@ -1,17 +0,0 @@
<?php
/*
* This file is part of the GlobalState package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState\TestFixture;
/**
*/
trait SnapshotTrait
{
}