Skip to content
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Here is a list of all components that are primarily developed and maintained by
* [sebastian/recursion-context](https://github.com/sebastianbergmann/recursion-context)
* [sebastian/type](https://github.com/sebastianbergmann/type)
* [sebastian/version](https://github.com/sebastianbergmann/version)
* [sebastian/version-requirement](https://github.com/sebastianbergmann/version-requirement)

A very special thanks to everyone who has contributed to the [PHPUnit Manual](https://github.com/sebastianbergmann/phpunit-documentation-english).

Expand Down
7 changes: 7 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@
</fileset>
</copy>

<copy file="${basedir}/vendor/sebastian/version-requirement/LICENSE" tofile="${basedir}/build/tmp/phar/sebastian-version-requirement/LICENSE"/>
<copy todir="${basedir}/build/tmp/phar/sebastian-version-requirement">
<fileset dir="${basedir}/vendor/sebastian/version-requirement/src">
<include name="**/*.php" />
</fileset>
</copy>

<copy file="${basedir}/vendor/staabm/side-effects-detector/LICENSE" tofile="${basedir}/build/tmp/phar/staabm-side-effects-detector/LICENSE"/>
<copy todir="${basedir}/build/tmp/phar/staabm-side-effects-detector">
<fileset dir="${basedir}/vendor/staabm/side-effects-detector/lib">
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"security": "https://github.com/sebastianbergmann/phpunit/security/policy"
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=8.4.1",
"ext-dom": "*",
Expand Down Expand Up @@ -55,6 +56,7 @@
"sebastian/recursion-context": "^8.0.0",
"sebastian/type": "^7.0.1",
"sebastian/version": "^7.0.0",
"sebastian/version-requirement": "^1.0",
"staabm/side-effects-detector": "^1.0.5"
},
"config": {
Expand Down
75 changes: 73 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions src/Metadata/Api/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PHPUnit\Metadata\Version\Requirement;
use PHPUnit\Runner\Version;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use SebastianBergmann\VersionRequirement\Requirement as RequirementImplementation;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
Expand All @@ -68,7 +69,7 @@ public function requirementsNotSatisfiedFor(string $className, string $methodNam

$this->warnAboutIncompleteVersion($metadata->versionRequirement(), $className, $methodName);

if (!$versionRequirement->isSatisfiedBy(PHP_VERSION)) {
if (!$this->isSatisfiedBy($versionRequirement, PHP_VERSION)) {
$notSatisfied[] = sprintf(
'PHP %s is required.',
$versionRequirement->asString(),
Expand All @@ -91,7 +92,7 @@ public function requirementsNotSatisfiedFor(string $className, string $methodNam

if (!extension_loaded($metadata->extension()) ||
($metadata->hasVersionRequirement() &&
!$metadata->versionRequirement()->isSatisfiedBy($extensionVersion))) {
!$this->isSatisfiedBy($metadata->versionRequirement(), $extensionVersion))) {
$notSatisfied[] = sprintf(
'PHP extension %s%s is required.',
$metadata->extension(),
Expand All @@ -107,7 +108,7 @@ public function requirementsNotSatisfiedFor(string $className, string $methodNam

$this->warnAboutIncompleteVersion($metadata->versionRequirement(), $className, $methodName);

if (!$versionRequirement->isSatisfiedBy(Version::id())) {
if (!$this->isSatisfiedBy($versionRequirement, Version::id())) {
$notSatisfied[] = sprintf(
'PHPUnit %s is required.',
$versionRequirement->asString(),
Expand Down Expand Up @@ -269,6 +270,17 @@ public function requiresXdebug(string $className, string $methodName): bool
return false;
}

private function isSatisfiedBy(Requirement $versionRequirement, string $version): bool
{
$requirement = $versionRequirement->asString();

if ($versionRequirement instanceof InvalidVersionRequirement || $requirement === '') {
return false;
}

return RequirementImplementation::from($requirement)->isSatisfiedBy($version);
}

/**
* @param class-string $className
* @param non-empty-string $methodName
Expand Down
6 changes: 0 additions & 6 deletions src/Metadata/Version/ComparisonRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace PHPUnit\Metadata\Version;

use function version_compare;
use PHPUnit\Util\VersionComparisonOperator;

/**
Expand All @@ -28,11 +27,6 @@ public function __construct(string $version, VersionComparisonOperator $operator
$this->operator = $operator;
}

public function isSatisfiedBy(string $version): bool
{
return version_compare($version, $this->version, $this->operator->asString());
}

public function asString(): string
{
return $this->operator->asString() . ' ' . $this->version;
Expand Down
31 changes: 3 additions & 28 deletions src/Metadata/Version/ConstraintRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,22 @@
*/
namespace PHPUnit\Metadata\Version;

use function assert;
use function preg_replace;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraint;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final readonly class ConstraintRequirement extends Requirement
{
private VersionConstraint $constraint;
private string $constraint;

public function __construct(VersionConstraint $constraint)
public function __construct(string $constraint)
{
$this->constraint = $constraint;
}

public function isSatisfiedBy(string $version): bool
{
return $this->constraint->complies(
new Version($this->sanitize($version)),
);
}

public function asString(): string
{
return $this->constraint->asString();
}

private function sanitize(string $version): string
{
$sanitized = preg_replace(
'/^(\d+\.\d+(?:.\d+)?).*$/',
'$1',
$version,
);

assert($sanitized !== null);

return $sanitized;
return $this->constraint;
}
}
5 changes: 0 additions & 5 deletions src/Metadata/Version/InvalidVersionRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function __construct(string $message)
$this->message = $message;
}

public function isSatisfiedBy(string $version): bool
{
return false;
}

/**
* @return non-empty-string
*/
Expand Down
43 changes: 21 additions & 22 deletions src/Metadata/Version/Requirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
*/
namespace PHPUnit\Metadata\Version;

use function preg_match;
use PharIo\Version\UnsupportedVersionConstraintException;
use PharIo\Version\VersionConstraintParser;
use function assert;
use function explode;
use PHPUnit\Metadata\InvalidVersionRequirementException;
use PHPUnit\Util\InvalidVersionOperatorException;
use PHPUnit\Util\VersionComparisonOperator;
use SebastianBergmann\VersionRequirement\ComparisonRequirement as ComparisonRequirementImplementation;
use SebastianBergmann\VersionRequirement\ConstraintRequirement as ConstraintRequirementImplementation;
use SebastianBergmann\VersionRequirement\Exception;
use SebastianBergmann\VersionRequirement\Requirement as RequirementImplementation;

/**
* @immutable
Expand All @@ -23,35 +25,32 @@
*/
abstract readonly class Requirement
{
private const string VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m";

/**
* @throws InvalidVersionOperatorException
* @param non-empty-string $versionRequirement
*
* @throws InvalidVersionRequirementException
*/
public static function from(string $versionRequirement): self
{
try {
return new ConstraintRequirement(
(new VersionConstraintParser)->parse(
$versionRequirement,
$requirement = RequirementImplementation::from($versionRequirement);
} catch (Exception) {
throw new InvalidVersionRequirementException;
}

if ($requirement instanceof ComparisonRequirementImplementation) {
return new ComparisonRequirement(
$requirement->version(),
new VersionComparisonOperator(
explode(' ', $requirement->asString(), 2)[0],
),
);
} catch (UnsupportedVersionConstraintException) {
if (preg_match(self::VERSION_COMPARISON, $versionRequirement, $matches) > 0) {
return new ComparisonRequirement(
$matches['version'],
new VersionComparisonOperator(
$matches['operator'] !== '' ? $matches['operator'] : '>=',
),
);
}
}

throw new InvalidVersionRequirementException;
}
assert($requirement instanceof ConstraintRequirementImplementation);

abstract public function isSatisfiedBy(string $version): bool;
return new ConstraintRequirement($requirement->asString());
}

abstract public function asString(): string;
}
25 changes: 7 additions & 18 deletions src/Util/VersionComparisonOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
*/
namespace PHPUnit\Util;

use function in_array;
use SebastianBergmann\VersionRequirement\InvalidVersionOperatorException as InvalidVersionOperatorExceptionImplementation;
use SebastianBergmann\VersionRequirement\VersionComparisonOperator as VersionComparisonOperatorImplementation;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
Expand All @@ -24,15 +25,15 @@
private string $operator;

/**
* @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
*
* @throws InvalidVersionOperatorException
*/
public function __construct(string $operator)
{
$this->ensureOperatorIsValid($operator);

$this->operator = $operator;
try {
$this->operator = new VersionComparisonOperatorImplementation($operator)->asString();
} catch (InvalidVersionOperatorExceptionImplementation) {
throw new InvalidVersionOperatorException($operator);
}
}

/**
Expand All @@ -42,16 +43,4 @@ public function asString(): string
{
return $this->operator;
}

/**
* @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
*
* @throws InvalidVersionOperatorException
*/
private function ensureOperatorIsValid(string $operator): void
{
if (!in_array($operator, ['<', 'lt', '<=', 'le', '>', 'gt', '>=', 'ge', '==', '=', 'eq', '!=', '<>', 'ne'], true)) {
throw new InvalidVersionOperatorException($operator);
}
}
}
Loading