From 560c3254d47945e40b0d4dfbefdebd5e1ce67429 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 11 Jul 2012 20:48:57 +0200 Subject: [PATCH] Correctly treat dev versions for other types of comparisons and add tests --- .../LinkConstraint/VersionConstraint.php | 18 +++++++++++------- .../LinkConstraint/VersionConstraintTest.php | 9 +++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Composer/Package/LinkConstraint/VersionConstraint.php b/src/Composer/Package/LinkConstraint/VersionConstraint.php index 47d2e69cf..8eb69dfd3 100644 --- a/src/Composer/Package/LinkConstraint/VersionConstraint.php +++ b/src/Composer/Package/LinkConstraint/VersionConstraint.php @@ -44,6 +44,15 @@ class VersionConstraint extends SpecificConstraint $this->version = $version; } + public function versionCompare($a, $b, $operator) + { + if ('dev-' === substr($a, 0, 4) && 'dev-' === substr($b, 0, 4)) { + return $operator == '==' && $a === $b; + } + + return version_compare($a, $b, $operator); + } + /** * * @param VersionConstraint $provider @@ -58,16 +67,11 @@ class VersionConstraint extends SpecificConstraint $isProviderEqualOp = '==' === $provider->operator; $isProviderNonEqualOp = '!=' === $provider->operator; - // dev- versions can not be compared with version_compare - if ('dev-' === substr($provider->version, 0, 4) && 'dev-' === substr($this->version, 0, 4)) { - return $isEqualOp && $isProviderEqualOp && $provider->version === $this->version; - } - // '!=' operator is match when other operator is not '==' operator or version is not match // these kinds of comparisons always have a solution if ($isNonEqualOp || $isProviderNonEqualOp) { return !$isEqualOp && !$isProviderEqualOp - || version_compare($provider->version, $this->version, '!='); + || $this->versionCompare($provider->version, $this->version, '!='); } // an example for the condition is <= 2.0 & < 1.0 @@ -76,7 +80,7 @@ class VersionConstraint extends SpecificConstraint return true; } - if (version_compare($provider->version, $this->version, $this->operator)) { + if ($this->versionCompare($provider->version, $this->version, $this->operator)) { // special case, e.g. require >= 1.0 and provide < 1.0 // 1.0 >= 1.0 but 1.0 is outside of the provided interval if ($provider->version == $this->version && $provider->operator == $providerNoEqualOp && $this->operator != $noEqualOp) { diff --git a/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php b/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php index 55bceda2d..eb6663822 100644 --- a/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php +++ b/tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php @@ -34,6 +34,12 @@ class VersionConstraintTest extends \PHPUnit_Framework_TestCase array('!=', '1', '>', '1'), array('!=', '1', '>=', '1'), array('==', 'dev-foo-bar', '==', 'dev-foo-bar'), + array('==', 'dev-foo-xyz', '==', 'dev-foo-xyz'), + array('>=', 'dev-foo-bar', '>=', 'dev-foo-xyz'), + array('<=', 'dev-foo-bar', '<', 'dev-foo-xyz'), + array('!=', 'dev-foo-bar', '<', 'dev-foo-xyz'), + array('>=', 'dev-foo-bar', '!=', 'dev-foo-bar'), + array('!=', 'dev-foo-bar', '!=', 'dev-foo-xyz'), ); } @@ -63,6 +69,9 @@ class VersionConstraintTest extends \PHPUnit_Framework_TestCase array('!=', '1', '==', '1'), array('==', '1', '!=', '1'), array('==', 'dev-foo-dist', '==', 'dev-foo-zist'), + array('==', 'dev-foo-bist', '==', 'dev-foo-aist'), + array('<=', 'dev-foo-bist', '>=', 'dev-foo-aist'), + array('>=', 'dev-foo-bist', '<', 'dev-foo-aist'), ); }