Browse Source

Add test for Pool handling and refactor a couple things

Jordi Boggiano 13 years ago
parent
commit
453b9a616b

+ 8 - 2
src/Composer/DependencyResolver/Pool.php

@@ -33,10 +33,16 @@ class Pool
     protected $acceptableStabilities;
     protected $stabilityFlags;
 
+    // TODO BC change to stable end of june?
     public function __construct($minimumStability = 'dev', array $stabilityFlags = array())
     {
         $stabilities = BasePackage::$stabilities;
-        $this->acceptableStabilities = array_flip(array_splice($stabilities, 0, array_search($minimumStability, $stabilities) + 1));
+        $this->acceptableStabilities = array();
+        foreach (BasePackage::$stabilities as $stability => $value) {
+            if ($value <= BasePackage::$stabilities[$minimumStability]) {
+                $this->acceptableStabilities[$stability] = $value;
+            }
+        }
         $this->stabilityFlags = $stabilityFlags;
     }
 
@@ -69,7 +75,7 @@ class Pool
                         && isset($this->acceptableStabilities[$stability]))
                     // allow if package matches the package-specific stability flag
                     || (isset($this->stabilityFlags[$name])
-                        && array_search($stability, BasePackage::$stabilities) <= $this->stabilityFlags[$name]
+                        && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$name]
                     )
                 ) {
                     $package->setId($id++);

+ 11 - 5
src/Composer/Package/BasePackage.php

@@ -32,12 +32,18 @@ abstract class BasePackage implements PackageInterface
         'require-dev' => array('description' => 'requires (for development)', 'method' => 'devRequires'),
     );
 
+    const STABILITY_STABLE  = 0;
+    const STABILITY_RC      = 5;
+    const STABILITY_BETA    = 10;
+    const STABILITY_ALPHA   = 15;
+    const STABILITY_DEV     = 20;
+
     public static $stabilities = array(
-        'stable',
-        'RC',
-        'beta',
-        'alpha',
-        'dev',
+        'stable' => self::STABILITY_STABLE,
+        'RC'     => self::STABILITY_RC,
+        'beta'   => self::STABILITY_BETA,
+        'alpha'  => self::STABILITY_ALPHA,
+        'dev'    => self::STABILITY_DEV,
     );
 
     protected $name;

+ 3 - 3
src/Composer/Package/Loader/RootPackageLoader.php

@@ -120,9 +120,9 @@ class RootPackageLoader extends ArrayLoader
         $stabilities = BasePackage::$stabilities;
         foreach ($requires as $reqName => $reqVersion) {
             // parse explicit stability flags
-            if (preg_match('{^[^,\s]*?@('.implode('|', $stabilities).')$}i', $reqVersion, $match)) {
+            if (preg_match('{^[^,\s]*?@('.implode('|', array_keys($stabilities)).')$}i', $reqVersion, $match)) {
                 $name = strtolower($reqName);
-                $stability = array_search(VersionParser::normalizeStability($match[1]), $stabilities);
+                $stability = $stabilities[VersionParser::normalizeStability($match[1])];
 
                 if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) {
                     continue;
@@ -135,7 +135,7 @@ class RootPackageLoader extends ArrayLoader
             // infer flags for requirements that have an explicit -dev or -beta version specified for example
             if (preg_match('{^[^,\s@]+$}', $reqVersion) && 'stable' !== ($stabilityName = VersionParser::parseStability($reqVersion))) {
                 $name = strtolower($reqName);
-                $stability = array_search($stabilityName, $stabilities);
+                $stability = $stabilities[$stabilityName];
                 if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) {
                     continue;
                 }

+ 1 - 0
src/Composer/Package/MemoryPackage.php

@@ -48,6 +48,7 @@ class MemoryPackage extends BasePackage
     protected $prettyAlias;
     protected $dev;
 
+    // TODO BC change dev to stable end of june?
     protected $minimumStability = 'dev';
     protected $stabilityFlags = array();
 

+ 1 - 1
src/Composer/Package/Version/VersionParser.php

@@ -151,7 +151,7 @@ class VersionParser
      */
     public function parseConstraints($constraints)
     {
-        if (preg_match('{^([^,\s]*?)@('.implode('|', BasePackage::$stabilities).')$}i', $constraints, $match)) {
+        if (preg_match('{^([^,\s]*?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraints, $match)) {
             $constraints = empty($match[1]) ? '*' : $match[1];
         }
 

+ 17 - 0
tests/Composer/Test/DependencyResolver/PoolTest.php

@@ -14,6 +14,7 @@ namespace Composer\Test\DependencyResolver;
 
 use Composer\DependencyResolver\Pool;
 use Composer\Repository\ArrayRepository;
+use Composer\Package\BasePackage;
 use Composer\Test\TestCase;
 
 class PoolTest extends TestCase
@@ -31,6 +32,22 @@ class PoolTest extends TestCase
         $this->assertEquals(array($package), $pool->whatProvides('foo'));
     }
 
+    public function testPoolIgnoresIrrelevantPackages()
+    {
+        $pool = new Pool('stable', array('bar' => BasePackage::STABILITY_BETA));
+        $repo = new ArrayRepository;
+        $repo->addPackage($package = $this->getPackage('bar', '1'));
+        $repo->addPackage($betaPackage = $this->getPackage('bar', '1-beta'));
+        $repo->addPackage($alphaPackage = $this->getPackage('bar', '1-alpha'));
+        $repo->addPackage($package2 = $this->getPackage('foo', '1'));
+        $repo->addPackage($rcPackage2 = $this->getPackage('foo', '1rc'));
+
+        $pool->addRepository($repo);
+
+        $this->assertEquals(array($package, $betaPackage), $pool->whatProvides('bar'));
+        $this->assertEquals(array($package2), $pool->whatProvides('foo'));
+    }
+
     /**
      * @expectedException \RuntimeException
      */