Browse Source

Merge pull request #4881 from alcohol/issue-4319

"smarter" php version conflict message
Jordi Boggiano 9 years ago
parent
commit
03a12ac2b9

+ 23 - 6
src/Composer/DependencyResolver/Rule.php

@@ -12,6 +12,8 @@
 
 namespace Composer\DependencyResolver;
 
+use Composer\Package\CompletePackage;
+
 /**
  * @author Nils Adermann <naderman@naderman.de>
  */
@@ -203,25 +205,40 @@ class Rule
                     if ($targetName === 'php' || $targetName === 'php-64bit' || $targetName === 'hhvm') {
                         // handle php/hhvm
                         if (defined('HHVM_VERSION')) {
-                            $text .= ' -> your HHVM version does not satisfy that requirement.';
+                            return $text . ' -> your HHVM version does not satisfy that requirement.';
                         } elseif ($targetName === 'hhvm') {
-                            $text .= ' -> you are running this with PHP and not HHVM.';
+                            return $text . ' -> you are running this with PHP and not HHVM.';
                         } else {
-                            $text .= ' -> your PHP version ('. phpversion() .') or value of "config.platform.php" in composer.json does not satisfy that requirement.';
+                            $packages = $pool->whatProvides($targetName);
+                            $package = count($packages) ? current($packages) : phpversion();
+
+                            if (!($package instanceof CompletePackage)) {
+                                return $text . ' -> your PHP version ('.phpversion().') does not satisfy that requirement.';
+                            }
+
+                            $extra = $package->getExtra();
+
+                            if (!empty($extra['config.platform'])) {
+                                $text .= ' -> your PHP version ('.phpversion().') overriden by "config.platform.php" version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
+                            } else {
+                                $text .= ' -> your PHP version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
+                            }
+
+                            return $text;
                         }
                     } elseif (0 === strpos($targetName, 'ext-')) {
                         // handle php extensions
                         $ext = substr($targetName, 4);
                         $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
 
-                        $text .= ' -> the requested PHP extension '.$ext.' '.$error.'.';
+                        return $text . ' -> the requested PHP extension '.$ext.' '.$error.'.';
                     } elseif (0 === strpos($targetName, 'lib-')) {
                         // handle linked libs
                         $lib = substr($targetName, 4);
 
-                        $text .= ' -> the requested linked library '.$lib.' has the wrong version installed or is missing from your system, make sure to have the extension providing it.';
+                        return $text . ' -> the requested linked library '.$lib.' has the wrong version installed or is missing from your system, make sure to have the extension providing it.';
                     } else {
-                        $text .= ' -> no matching package found.';
+                        return $text . ' -> no matching package found.';
                     }
                 }
 

+ 1 - 0
src/Composer/Repository/PlatformRepository.php

@@ -59,6 +59,7 @@ class PlatformRepository extends ArrayRepository
             $version = $versionParser->normalize($override['version']);
             $package = new CompletePackage($override['name'], $version, $override['version']);
             $package->setDescription('Package overridden via config.platform');
+            $package->setExtra(array('config.platform' => true));
             parent::addPackage($package);
         }
 

+ 45 - 0
tests/Composer/Test/Fixtures/installer/github-issues-4319.test

@@ -0,0 +1,45 @@
+--TEST--
+
+See Github issue #4319 ( github.com/composer/composer/issues/4319 ).
+
+Present a clear error message when config.platform.php version results in a conflict rule.
+
+--CONDITION--
+!defined('HHVM_VERSION')
+
+--COMPOSER--
+{
+    "repositories": [
+        {
+            "type": "package",
+            "package": [
+                { "name": "a", "version": "1.0.0", "require": { "php": "5.5" } }
+            ]
+        }
+    ],
+    "require": {
+        "a": "~1.0"
+    },
+    "config": {
+        "platform": {
+            "php": "5.3"
+        }
+    }
+}
+
+--RUN--
+install
+
+--EXPECT-OUTPUT--
+Loading composer repositories with package information
+Installing dependencies (including require-dev)
+Your requirements could not be resolved to an installable set of packages.
+
+  Problem 1
+    - Installation request for a ~1.0 -> satisfiable by a[1.0.0].
+    - a 1.0.0 requires php 5.5 -> your PHP version (%s) overriden by "config.platform.php" version (5.3) does not satisfy that requirement.
+
+--EXPECT--
+
+--EXPECT-EXIT-CODE--
+2

+ 1 - 1
tests/Composer/Test/InstallerTest.php

@@ -252,7 +252,7 @@ class InstallerTest extends TestCase
         $this->assertSame(rtrim($expect), implode("\n", $installationManager->getTrace()));
 
         if ($expectOutput) {
-            $this->assertEquals(rtrim($expectOutput), rtrim($output));
+            $this->assertStringMatchesFormat(rtrim($expectOutput), rtrim($output));
         }
     }