Browse Source

Correctly treat dev versions for other types of comparisons and add tests

Nils Adermann 13 years ago
parent
commit
560c3254d4

+ 11 - 7
src/Composer/Package/LinkConstraint/VersionConstraint.php

@@ -44,6 +44,15 @@ class VersionConstraint extends SpecificConstraint
         $this->version = $version;
         $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
      * @param VersionConstraint $provider
@@ -58,16 +67,11 @@ class VersionConstraint extends SpecificConstraint
         $isProviderEqualOp = '==' === $provider->operator;
         $isProviderEqualOp = '==' === $provider->operator;
         $isProviderNonEqualOp = '!=' === $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
         // '!=' operator is match when other operator is not '==' operator or version is not match
         // these kinds of comparisons always have a solution
         // these kinds of comparisons always have a solution
         if ($isNonEqualOp || $isProviderNonEqualOp) {
         if ($isNonEqualOp || $isProviderNonEqualOp) {
             return !$isEqualOp && !$isProviderEqualOp
             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
         // an example for the condition is <= 2.0 & < 1.0
@@ -76,7 +80,7 @@ class VersionConstraint extends SpecificConstraint
             return true;
             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
             // special case, e.g. require >= 1.0 and provide < 1.0
             // 1.0 >= 1.0 but 1.0 is outside of the provided interval
             // 1.0 >= 1.0 but 1.0 is outside of the provided interval
             if ($provider->version == $this->version && $provider->operator == $providerNoEqualOp && $this->operator != $noEqualOp) {
             if ($provider->version == $this->version && $provider->operator == $providerNoEqualOp && $this->operator != $noEqualOp) {

+ 9 - 0
tests/Composer/Test/Package/LinkConstraint/VersionConstraintTest.php

@@ -34,6 +34,12 @@ class VersionConstraintTest extends \PHPUnit_Framework_TestCase
             array('!=', '1', '>',  '1'),
             array('!=', '1', '>',  '1'),
             array('!=', '1', '>=', '1'),
             array('!=', '1', '>=', '1'),
             array('==', 'dev-foo-bar', '==', 'dev-foo-bar'),
             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('==', '1', '!=', '1'),
             array('==', '1', '!=', '1'),
             array('==', 'dev-foo-dist', '==', 'dev-foo-zist'),
             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'),
         );
         );
     }
     }