2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Installer;
|
|
|
|
|
|
|
|
use Composer\Installer\LibraryInstaller;
|
2022-02-18 12:05:12 +00:00
|
|
|
use Composer\Repository\InstalledArrayRepository;
|
2012-02-09 17:45:28 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-06-24 18:11:06 +00:00
|
|
|
use Composer\Composer;
|
|
|
|
use Composer\Config;
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-02-24 11:28:41 +00:00
|
|
|
class LibraryInstallerTest extends TestCase
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2021-10-27 12:41:30 +00:00
|
|
|
/**
|
|
|
|
* @var \Composer\Composer
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $composer;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Config
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $config;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-21 12:01:55 +00:00
|
|
|
protected $rootDir;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $vendorDir;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $binDir;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Downloader\DownloadManager&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $dm;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Repository\InstalledRepositoryInterface&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $repository;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $io;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Util\Filesystem
|
|
|
|
*/
|
2012-08-16 09:22:30 +00:00
|
|
|
protected $fs;
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2012-02-16 20:43:12 +00:00
|
|
|
$this->fs = new Filesystem;
|
2011-12-03 14:39:06 +00:00
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->composer = new Composer();
|
2020-02-07 04:43:57 +00:00
|
|
|
$this->config = new Config(false);
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->composer->setConfig($this->config);
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->rootDir = $this->getUniqueTmpDirectory();
|
2016-01-26 10:23:08 +00:00
|
|
|
$this->vendorDir = $this->rootDir.DIRECTORY_SEPARATOR.'vendor';
|
2022-02-18 13:32:38 +00:00
|
|
|
self::ensureDirectoryExistsAndClear($this->vendorDir);
|
2011-12-03 14:39:06 +00:00
|
|
|
|
2016-01-26 10:23:08 +00:00
|
|
|
$this->binDir = $this->rootDir.DIRECTORY_SEPARATOR.'bin';
|
2022-02-18 13:32:38 +00:00
|
|
|
self::ensureDirectoryExistsAndClear($this->binDir);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->config->merge(array(
|
|
|
|
'config' => array(
|
|
|
|
'vendor-dir' => $this->vendorDir,
|
|
|
|
'bin-dir' => $this->binDir,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
2011-09-23 21:23:16 +00:00
|
|
|
$this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->composer->setDownloadManager($this->dm);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
|
|
|
|
$this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2012-02-24 11:28:41 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->fs->removeDirectory($this->rootDir);
|
2012-02-24 09:40:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstallerCreationShouldNotCreateVendorDirectory(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2012-02-16 20:43:12 +00:00
|
|
|
$this->fs->removeDirectory($this->vendorDir);
|
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
new LibraryInstaller($this->io, $this->composer);
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist($this->vendorDir);
|
2012-02-16 20:43:12 +00:00
|
|
|
}
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstallerCreationShouldNotCreateBinDirectory(): void
|
2012-02-16 20:43:12 +00:00
|
|
|
{
|
|
|
|
$this->fs->removeDirectory($this->binDir);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
new LibraryInstaller($this->io, $this->composer);
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist($this->binDir);
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsInstalled(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2012-06-24 18:11:06 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('test/pkg', '1.0.0');
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
$repository = new InstalledArrayRepository();
|
|
|
|
$this->assertFalse($library->isInstalled($repository, $package));
|
|
|
|
|
|
|
|
// package being in repo is not enough to be installed
|
|
|
|
$repository->addPackage($package);
|
|
|
|
$this->assertFalse($library->isInstalled($repository, $package));
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
// package being in repo and vendor/pkg/foo dir present means it is seen as installed
|
2022-02-18 13:32:38 +00:00
|
|
|
self::ensureDirectoryExistsAndClear($this->vendorDir.'/'.$package->getPrettyName());
|
2022-02-18 12:05:12 +00:00
|
|
|
$this->assertTrue($library->isInstalled($repository, $package));
|
|
|
|
|
|
|
|
$repository->removePackage($package);
|
|
|
|
$this->assertFalse($library->isInstalled($repository, $package));
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 20:43:12 +00:00
|
|
|
/**
|
|
|
|
* @depends testInstallerCreationShouldNotCreateVendorDirectory
|
|
|
|
* @depends testInstallerCreationShouldNotCreateBinDirectory
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstall(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2012-06-24 18:11:06 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('some/package', '1.0.0');
|
2011-09-23 22:30:17 +00:00
|
|
|
|
2011-09-23 21:23:16 +00:00
|
|
|
$this->dm
|
|
|
|
->expects($this->once())
|
2019-01-17 16:12:33 +00:00
|
|
|
->method('install')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with($package, $this->vendorDir.'/some/package')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2011-09-23 21:23:16 +00:00
|
|
|
->expects($this->once())
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('addPackage')
|
|
|
|
->with($package);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$library->install($this->repository, $package);
|
2012-02-16 20:43:12 +00:00
|
|
|
$this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
|
|
|
|
$this->assertFileExists($this->binDir, 'Bin dir should be created');
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 20:43:12 +00:00
|
|
|
/**
|
|
|
|
* @depends testInstallerCreationShouldNotCreateVendorDirectory
|
|
|
|
* @depends testInstallerCreationShouldNotCreateBinDirectory
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUpdate(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2013-09-22 17:42:05 +00:00
|
|
|
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')
|
|
|
|
->getMock();
|
|
|
|
$filesystem
|
|
|
|
->expects($this->once())
|
2013-09-25 19:07:55 +00:00
|
|
|
->method('rename')
|
2022-02-18 12:05:12 +00:00
|
|
|
->with($this->vendorDir.'/vendor/package1/oldtarget', $this->vendorDir.'/vendor/package1/newtarget');
|
2013-11-04 12:36:30 +00:00
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
$initial = $this->getPackage('vendor/package1', '1.0.0');
|
|
|
|
$target = $this->getPackage('vendor/package1', '2.0.0');
|
2011-09-23 22:30:17 +00:00
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
$initial->setTargetDir('oldtarget');
|
|
|
|
$target->setTargetDir('newtarget');
|
2013-09-22 17:42:05 +00:00
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2012-02-18 22:21:45 +00:00
|
|
|
->expects($this->exactly(3))
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('hasPackage')
|
2012-02-18 22:21:45 +00:00
|
|
|
->will($this->onConsecutiveCalls(true, false, false));
|
2011-09-23 21:23:16 +00:00
|
|
|
|
|
|
|
$this->dm
|
|
|
|
->expects($this->once())
|
|
|
|
->method('update')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with($initial, $target, $this->vendorDir.'/vendor/package1/newtarget')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2011-09-23 21:23:16 +00:00
|
|
|
->expects($this->once())
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('removePackage')
|
2011-09-23 21:23:16 +00:00
|
|
|
->with($initial);
|
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2011-09-23 21:23:16 +00:00
|
|
|
->expects($this->once())
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('addPackage')
|
|
|
|
->with($target);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2013-09-22 17:42:05 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer, 'library', $filesystem);
|
2012-04-14 13:45:25 +00:00
|
|
|
$library->update($this->repository, $initial, $target);
|
2012-02-16 20:43:12 +00:00
|
|
|
$this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
|
|
|
|
$this->assertFileExists($this->binDir, 'Bin dir should be created');
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('InvalidArgumentException');
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$library->update($this->repository, $initial, $target);
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUninstall(): void
|
2011-09-23 21:23:16 +00:00
|
|
|
{
|
2012-06-24 18:11:06 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('vendor/pkg', '1.0.0');
|
2011-09-23 22:30:17 +00:00
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2011-09-23 21:23:16 +00:00
|
|
|
->expects($this->exactly(2))
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('hasPackage')
|
2011-09-23 21:23:16 +00:00
|
|
|
->with($package)
|
|
|
|
->will($this->onConsecutiveCalls(true, false));
|
|
|
|
|
|
|
|
$this->dm
|
|
|
|
->expects($this->once())
|
|
|
|
->method('remove')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with($package, $this->vendorDir.'/vendor/pkg')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2011-09-25 12:44:41 +00:00
|
|
|
$this->repository
|
2011-09-23 21:23:16 +00:00
|
|
|
->expects($this->once())
|
2011-09-25 12:44:41 +00:00
|
|
|
->method('removePackage')
|
2011-09-23 21:23:16 +00:00
|
|
|
->with($package);
|
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$library->uninstall($this->repository, $package);
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('InvalidArgumentException');
|
2011-09-23 21:23:16 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$library->uninstall($this->repository, $package);
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
public function testGetInstallPathWithoutTargetDir(): void
|
2011-10-30 18:10:37 +00:00
|
|
|
{
|
2012-06-24 18:11:06 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('Vendor/Pkg', '1.0.0');
|
2011-10-30 18:10:37 +00:00
|
|
|
|
2022-02-18 12:05:12 +00:00
|
|
|
$this->assertEquals($this->vendorDir.'/'.$package->getPrettyName(), $library->getInstallPath($package));
|
2011-10-30 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetInstallPathWithTargetDir(): void
|
2011-10-30 18:10:37 +00:00
|
|
|
{
|
2012-06-24 18:11:06 +00:00
|
|
|
$library = new LibraryInstaller($this->io, $this->composer);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('Foo/Bar', '1.0.0');
|
|
|
|
$package->setTargetDir('Some/Namespace');
|
2011-10-30 18:10:37 +00:00
|
|
|
|
2011-12-15 14:14:33 +00:00
|
|
|
$this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
|
2011-10-30 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 21:15:13 +00:00
|
|
|
/**
|
|
|
|
* @depends testInstallerCreationShouldNotCreateVendorDirectory
|
|
|
|
* @depends testInstallerCreationShouldNotCreateBinDirectory
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEnsureBinariesInstalled(): void
|
2016-03-28 21:15:13 +00:00
|
|
|
{
|
|
|
|
$binaryInstallerMock = $this->getMockBuilder('Composer\Installer\BinaryInstaller')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$library = new LibraryInstaller($this->io, $this->composer, 'library', null, $binaryInstallerMock);
|
2022-02-18 12:05:12 +00:00
|
|
|
$package = $this->getPackage('foo/bar', '1.0.0');
|
2016-03-28 21:15:13 +00:00
|
|
|
|
|
|
|
$binaryInstallerMock
|
2016-07-02 15:35:09 +00:00
|
|
|
->expects($this->never())
|
2016-03-28 21:15:13 +00:00
|
|
|
->method('removeBinaries')
|
|
|
|
->with($package);
|
|
|
|
|
|
|
|
$binaryInstallerMock
|
|
|
|
->expects($this->once())
|
|
|
|
->method('installBinaries')
|
2016-07-02 15:35:09 +00:00
|
|
|
->with($package, $library->getInstallPath($package), false);
|
2016-03-28 21:15:13 +00:00
|
|
|
|
2016-07-02 15:35:09 +00:00
|
|
|
$library->ensureBinariesPresence($package);
|
2016-03-28 21:15:13 +00:00
|
|
|
}
|
2011-09-23 21:23:16 +00:00
|
|
|
}
|