Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Remove PHPUnit 5.7 compatability hacks #10444

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"php": "^8.1",
"bramus/monolog-colored-line-formatter": "^2.0.3",
"composer/installers": "^2.1.1",
"composer/semver": "^3.3.2",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.4.0",
"embed/embed": "^4.4.4",
Expand Down Expand Up @@ -60,9 +59,7 @@
"squizlabs/php_codesniffer": "^3.7"
},
"conflict": {
"egulias/email-validator": "^2",
"phpunit/phpunit": "^6 || ^7 || ^8",
"cwp/cwp-core": "<2.11.0"
"egulias/email-validator": "^2"
},
"provide": {
"psr/container-implementation": "1.0.0"
Expand Down
30 changes: 3 additions & 27 deletions src/Core/BaseKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,10 @@ protected function bootPHP()
protected function bootManifests($flush)
{
// Setup autoloader
$this->getClassLoader()->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoredCIConfigs()
);
$this->getClassLoader()->init($this->getIncludeTests(), $flush);

// Find modules
$this->getModuleLoader()->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoredCIConfigs()
);
$this->getModuleLoader()->init($this->getIncludeTests(), $flush);

// Flush config
if ($flush) {
Expand All @@ -209,11 +201,7 @@ protected function bootManifests($flush)
$defaultSet->setProject(
ModuleManifest::config()->get('project')
);
$defaultSet->init(
$this->getIncludeTests(),
$flush,
$this->getIgnoredCIConfigs()
);
$defaultSet->init($this->getIncludeTests(), $flush);
}
}

Expand Down Expand Up @@ -372,18 +360,6 @@ protected function buildManifestCacheFactory()
]);
}

/**
* When manifests are discovering files, tests files in modules using the following CI library type will be ignored.
*
* The purpose of this method is to avoid loading PHPUnit test files with incompatible definitions.
*
* @return string[] List of CI types to ignore as defined by `Module`.
*/
protected function getIgnoredCIConfigs(): array
{
return [];
}

/**
* @return bool
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Core/Manifest/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ public function getItemPath($class)
*
* @param bool $includeTests
* @param bool $forceRegen
* @param string[] $ignoredCIConfigs
*/
public function init($includeTests = false, $forceRegen = false, array $ignoredCIConfigs = [])
public function init($includeTests = false, $forceRegen = false)
{
foreach ($this->manifests as $manifest) {
/** @var ClassManifest $instance */
$instance = $manifest['instance'];
$instance->init($includeTests, $forceRegen, $ignoredCIConfigs);
$instance->init($includeTests, $forceRegen);
}

$this->registerAutoloader();
Expand Down
9 changes: 3 additions & 6 deletions src/Core/Manifest/ClassManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,8 @@ public function isFlushed()
*
* @param bool $includeTests
* @param bool $forceRegen
* @param string[] $ignoredCIConfigs
*/
public function init($includeTests = false, $forceRegen = false, array $ignoredCIConfigs = [])
public function init($includeTests = false, $forceRegen = false)
{
$this->cache = $this->buildCache($includeTests);

Expand All @@ -292,7 +291,7 @@ public function init($includeTests = false, $forceRegen = false, array $ignoredC
}

// Build
$this->regenerate($includeTests, $ignoredCIConfigs);
$this->regenerate($includeTests);
}

/**
Expand Down Expand Up @@ -539,9 +538,8 @@ public function getOwnerModule($class)
* Completely regenerates the manifest file.
*
* @param bool $includeTests
* @param string[] $ignoredCIConfigs
*/
public function regenerate($includeTests, array $ignoredCIConfigs = [])
public function regenerate($includeTests)
{
// Reset the manifest so stale info doesn't cause errors.
$this->loadState([]);
Expand All @@ -553,7 +551,6 @@ public function regenerate($includeTests, array $ignoredCIConfigs = [])
'name_regex' => '/^[^_].*\\.php$/',
'ignore_files' => ['index.php', 'cli-script.php'],
'ignore_tests' => !$includeTests,
'ignored_ci_configs' => $ignoredCIConfigs,
'file_callback' => function ($basename, $pathname, $depth) use ($includeTests, $finder) {
$this->handleFile($basename, $pathname, $includeTests);
},
Expand Down
51 changes: 2 additions & 49 deletions src/Core/Manifest/ManifestFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SilverStripe\Core\Manifest;

use RuntimeException;
use SilverStripe\Assets\FileFinder;

/**
Expand All @@ -11,9 +10,8 @@
* - Only modules with _config.php files are scanned.
* - If a _manifest_exclude file is present inside a directory it is ignored.
* - Assets and module language directories are ignored.
* - Module tests directories are skipped if either of these conditions is meant:
* - the `ignore_tests` option is not set to false.
* - the module PHP CI configuration matches one of the `ignored_ci_configs`
* - Module tests directories are skipped if the `ignore_tests` option is not
* set to false.
*/
class ManifestFileFinder extends FileFinder
{
Expand All @@ -30,7 +28,6 @@ class ManifestFileFinder extends FileFinder
'ignore_tests' => true,
'min_depth' => 1,
'ignore_dirs' => ['node_modules'],
'ignored_ci_configs' => []
];

public function acceptDir($basename, $pathname, $depth)
Expand Down Expand Up @@ -71,14 +68,6 @@ public function acceptDir($basename, $pathname, $depth)
return false;
}

// Skip if test dir inside vendor module with unexpected CI Configuration
if ($depth > 3 && $basename === self::TESTS_DIR && $ignoredCIConfig = $this->getOption('ignored_ci_configs')) {
$ciLib = $this->findModuleCIPhpConfiguration($basename, $pathname, $depth);
if (in_array($ciLib, $ignoredCIConfig ?? [])) {
return false;
}
}

return parent::acceptDir($basename, $pathname, $depth);
}

Expand Down Expand Up @@ -257,40 +246,4 @@ public function isDirectoryIgnored($basename, $pathname, $depth)

return false;
}

/**
* Find out the root of the current module and read the PHP CI configuration from tho composer file
*
* @param string $basename Name of the current folder
* @param string $pathname Full path the parent folder
* @param string $depth Depth of the current folder
*/
private function findModuleCIPhpConfiguration(string $basename, string $pathname, int $depth): string
{
if ($depth < 1) {
// We went all the way back to the root of the project
return Module::CI_UNKNOWN;
}

// We pop the current folder and use the next entry the pathname
$newBasename = basename($pathname ?? '');
$newPathname = dirname($pathname ?? '');
$newDepth = $depth - 1;

if ($this->isDirectoryModule($newBasename, $newPathname, $newDepth)) {
// We've reached the root of the module folder, we can read the PHP CI config now
$module = new Module($newPathname, $this->upLevels($newPathname, $newDepth));
$config = $module->getCIConfig();

if (empty($config['PHP'])) {
// This should never happen
throw new RuntimeException('Module::getCIConfig() did not return a PHP CI value');
}

return $config['PHP'];
}

// We haven't reach our module root yet ... let's look up one more level
return $this->findModuleCIPhpConfiguration($newBasename, $newPathname, $newDepth);
}
}
133 changes: 0 additions & 133 deletions src/Core/Manifest/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace SilverStripe\Core\Manifest;

use Composer\Semver\Semver;
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Serializable;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Deprecation;
Expand All @@ -21,23 +19,6 @@ class Module implements Serializable
*/
const TRIM_CHARS = ' /\\';

/**
* Return value of getCIConfig() when module uses PHPUNit 9
*/
const CI_PHPUNIT_NINE = 'CI_PHPUNIT_NINE';

/**
* Return value of getCIConfig() when module uses PHPUNit 5
*/
const CI_PHPUNIT_FIVE = 'CI_PHPUNIT_FIVE';

/**
* Return value of getCIConfig() when module does not use any CI
*/
const CI_UNKNOWN = 'CI_UNKNOWN';



/**
* Full directory path to this module with no trailing slash
*
Expand Down Expand Up @@ -325,120 +306,6 @@ public function hasResource($path)
->getResource($path)
->exists();
}

/**
* Determine what configurations the module is using to run various aspects of its CI. THe only aspect
* that is observed is `PHP`
* @return array List of configuration aspects e.g.: `['PHP' => 'CI_PHPUNIT_NINE']`
* @internal
*/
public function getCIConfig(): array
{
return [
'PHP' => $this->getPhpCiConfig()
];
}

/**
* Determine what CI Configuration the module uses to test its PHP code.
*/
private function getPhpCiConfig(): string
{
// We don't have any composer data at all
if (empty($this->composerData)) {
return self::CI_UNKNOWN;
}

// We don't have any dev dependencies
if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) {
return self::CI_UNKNOWN;
}

// We are assuming a typical setup where the CI lib is defined in require-dev rather than require
$requireDev = $this->composerData['require-dev'];

// Try to pick which CI we are using based on phpunit constraint
$phpUnitConstraint = $this->requireDevConstraint(['sminnee/phpunit', 'phpunit/phpunit']);
if ($phpUnitConstraint) {
if ($this->constraintSatisfies(
$phpUnitConstraint,
['5.7.0', '5.0.0', '5.x-dev', '5.7.x-dev'],
5
)) {
return self::CI_PHPUNIT_FIVE;
}
if ($this->constraintSatisfies(
$phpUnitConstraint,
['9.0.0', '9.5.0', '9.x-dev', '9.5.x-dev'],
9
)) {
return self::CI_PHPUNIT_NINE;
}
}

// Try to pick which CI we are using based on recipe-testing constraint
$recipeTestingConstraint = $this->requireDevConstraint(['silverstripe/recipe-testing']);
if ($recipeTestingConstraint) {
if ($this->constraintSatisfies(
$recipeTestingConstraint,
['1.0.0', '1.1.0', '1.2.0', '1.1.x-dev', '1.2.x-dev', '1.x-dev'],
1
)) {
return self::CI_PHPUNIT_FIVE;
}
if ($this->constraintSatisfies(
$recipeTestingConstraint,
['2.0.0', '2.0.x-dev', '2.x-dev'],
2
)) {
return self::CI_PHPUNIT_NINE;
}
}

return self::CI_UNKNOWN;
}

/**
* Retrieve the constraint for the first module that is found in the require-dev section
* @param string[] $modules
* @return false|string
*/
private function requireDevConstraint(array $modules)
{
if (empty($this->composerData['require-dev']) || !is_array($this->composerData['require-dev'])) {
return false;
}

$requireDev = $this->composerData['require-dev'];
foreach ($modules as $module) {
if (isset($requireDev[$module])) {
return $requireDev[$module];
}
}

return false;
}

/**
* Determines if the provided constraint allows at least one of the version provided
*/
private function constraintSatisfies(
string $constraint,
array $possibleVersions,
int $majorVersionFallback
): bool {
// Let's see of any of our possible versions is allowed by the constraint
if (!empty(Semver::satisfiedBy($possibleVersions, $constraint))) {
return true;
}

// Let's see if we are using an exact version constraint. e.g. ~1.2.3 or 1.2.3 or ~1.2 or 1.2.*
if (preg_match("/^~?$majorVersionFallback(\.(\d+)|\*){0,2}/", $constraint ?? '')) {
return true;
}

return false;
}
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Core/Manifest/ModuleLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ public function countManifests()
*
* @param bool $includeTests
* @param bool $forceRegen
* @param string[] $ignoredCIConfigs
*/
public function init($includeTests = false, $forceRegen = false, array $ignoredCIConfigs = [])
public function init($includeTests = false, $forceRegen = false)
{
foreach ($this->manifests as $manifest) {
$manifest->init($includeTests, $forceRegen, $ignoredCIConfigs);
$manifest->init($includeTests, $forceRegen);
}
}
}
Loading