Browse Source

add tests to cover different use cases of package install preferences

Steve Buzonas 10 years ago
parent
commit
b44c3bee52

+ 9 - 8
src/Composer/Downloader/DownloadManager.php

@@ -199,7 +199,7 @@ class DownloadManager
             throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified');
         }
 
-        if (Config::INSTALL_PREFERENCE_DIST === $this->resolvePackageInstallPreference($package, $preferSource)) {
+        if (!$preferSource && ($this->preferDist || Config::INSTALL_PREFERENCE_DIST === $this->resolvePackageInstallPreference($package))) {
             $sources = array_reverse($sources);
         }
 
@@ -288,14 +288,15 @@ class DownloadManager
         }
     }
 
-    protected function resolvePackageInstallPreference(PackageInterface $package, $preferSource = false)
+    /**
+     * Determines the install preference of a package
+     *
+     * @param PackageInterface $package package instance
+     *
+     * @return string
+     */
+    protected function resolvePackageInstallPreference(PackageInterface $package)
     {
-        if ($this->preferSource || $preferSource) {
-            return Config::INSTALL_PREFERENCE_SOURCE;
-        }
-        if ($this->preferDist) {
-            return Config::INSTALL_PREFERENCE_DIST;
-        }
         foreach ($this->packagePreferences as $pattern => $preference) {
             $pattern = '{^'.str_replace('*', '.*', $pattern).'$}i';
             if (preg_match($pattern, $package->getName())) {

+ 360 - 0
tests/Composer/Test/Downloader/DownloadManagerTest.php

@@ -757,6 +757,366 @@ class DownloadManagerTest extends \PHPUnit_Framework_TestCase
         $manager->remove($package, 'vendor/bundles/FOS/UserBundle');
     }
 
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithoutPreferenceDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(true));
+
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('source');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithoutPreferenceNoDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(false));
+
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('dist');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+
+        $manager->download($package, 'target_dir');
+    }
+    
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithoutMatchDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(true));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('bar/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('source');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'source'));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithoutMatchNoDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(false));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('bar/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('dist');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'source'));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithMatchAutoDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(true));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('foo/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('source');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'auto'));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithMatchAutoNoDev()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('isDev')
+            ->will($this->returnValue(false));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('foo/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('dist');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'auto'));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithMatchSource()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('foo/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('source');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'source'));
+
+        $manager->download($package, 'target_dir');
+    }
+
+    /**
+     * @covers Composer\Downloader\DownloadManager::resolvePackageInstallPreference
+     */
+    public function testInstallPreferenceWithMatchDist()
+    {
+        $package = $this->createPackageMock();
+        $package
+            ->expects($this->once())
+            ->method('getSourceType')
+            ->will($this->returnValue('git'));
+        $package
+            ->expects($this->once())
+            ->method('getDistType')
+            ->will($this->returnValue('pear'));
+        $package
+            ->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('foo/package'));
+        $package
+            ->expects($this->once())
+            ->method('setInstallationSource')
+            ->with('dist');
+
+        $downloader = $this->createDownloaderMock();
+        $downloader
+            ->expects($this->once())
+            ->method('download')
+            ->with($package, 'target_dir');
+
+        $manager = $this->getMockBuilder('Composer\Downloader\DownloadManager')
+            ->setConstructorArgs(array($this->io, false, $this->filesystem))
+            ->setMethods(array('getDownloaderForInstalledPackage'))
+            ->getMock();
+        $manager
+            ->expects($this->once())
+            ->method('getDownloaderForInstalledPackage')
+            ->with($package)
+            ->will($this->returnValue($downloader));
+        $manager->setPreferences(array('foo/*' => 'dist'));
+
+        $manager->download($package, 'target_dir');
+    }
+
     private function createDownloaderMock()
     {
         return $this->getMockBuilder('Composer\Downloader\DownloaderInterface')