1
0
Fork 0
composer/tests/Composer/Test/Installer/LibraryInstallerTest.php

289 lines
9.0 KiB
PHP
Raw Normal View History

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;
use Composer\Util\Filesystem;
use Composer\Test\TestCase;
use Composer\Composer;
use Composer\Config;
2011-09-23 21:23:16 +00:00
class LibraryInstallerTest extends TestCase
2011-09-23 21:23:16 +00:00
{
/**
* @var \Composer\Composer
*/
protected $composer;
/**
* @var \Composer\Config
*/
protected $config;
/**
* @var string
*/
protected $rootDir;
/**
* @var string
*/
protected $vendorDir;
/**
* @var string
*/
protected $binDir;
/**
* @var \Composer\Downloader\DownloadManager&\PHPUnit\Framework\MockObject\MockObject
*/
protected $dm;
/**
* @var \Composer\Repository\InstalledRepositoryInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected $repository;
/**
* @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject
*/
protected $io;
/**
* @var \Composer\Util\Filesystem
*/
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
{
$this->fs = new Filesystem;
2011-12-03 14:39:06 +00:00
$this->composer = new Composer();
2020-02-07 04:43:57 +00:00
$this->config = new Config(false);
$this->composer->setConfig($this->config);
$this->rootDir = self::getUniqueTmpDirectory();
$this->vendorDir = $this->rootDir.DIRECTORY_SEPARATOR.'vendor';
self::ensureDirectoryExistsAndClear($this->vendorDir);
2011-12-03 14:39:06 +00:00
$this->binDir = $this->rootDir.DIRECTORY_SEPARATOR.'bin';
self::ensureDirectoryExistsAndClear($this->binDir);
2011-09-23 21:23:16 +00:00
2022-08-17 12:20:07 +00:00
$this->config->merge([
'config' => [
'vendor-dir' => $this->vendorDir,
'bin-dir' => $this->binDir,
2022-08-17 12:20:07 +00:00
],
]);
2011-09-23 21:23:16 +00:00
$this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
->disableOriginalConstructor()
->getMock();
$this->composer->setDownloadManager($this->dm);
2011-09-23 21:23:16 +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
{
parent::tearDown();
$this->fs->removeDirectory($this->rootDir);
}
public function testInstallerCreationShouldNotCreateVendorDirectory(): void
2011-09-23 21:23:16 +00:00
{
$this->fs->removeDirectory($this->vendorDir);
new LibraryInstaller($this->io, $this->composer);
2020-09-11 09:27:26 +00:00
$this->assertFileDoesNotExist($this->vendorDir);
}
2011-09-23 21:23:16 +00:00
public function testInstallerCreationShouldNotCreateBinDirectory(): void
{
$this->fs->removeDirectory($this->binDir);
2011-09-23 21:23:16 +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
}
public function testIsInstalled(): void
2011-09-23 21:23:16 +00:00
{
$library = new LibraryInstaller($this->io, $this->composer);
$package = self::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
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
}
/**
* @depends testInstallerCreationShouldNotCreateVendorDirectory
* @depends testInstallerCreationShouldNotCreateBinDirectory
*/
public function testInstall(): void
2011-09-23 21:23:16 +00:00
{
$library = new LibraryInstaller($this->io, $this->composer);
$package = self::getPackage('some/package', '1.0.0');
2011-09-23 21:23:16 +00:00
$this->dm
->expects($this->once())
->method('install')
2022-02-22 21:10:52 +00:00
->with($package, $this->vendorDir.'/some/package')
->will($this->returnValue(\React\Promise\resolve(null)));
2011-09-23 21:23:16 +00:00
$this->repository
2011-09-23 21:23:16 +00:00
->expects($this->once())
->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);
$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
}
/**
* @depends testInstallerCreationShouldNotCreateVendorDirectory
* @depends testInstallerCreationShouldNotCreateBinDirectory
*/
public function testUpdate(): void
2011-09-23 21:23:16 +00:00
{
$filesystem = $this->getMockBuilder('Composer\Util\Filesystem')
->getMock();
$filesystem
->expects($this->once())
->method('rename')
2022-02-18 12:05:12 +00:00
->with($this->vendorDir.'/vendor/package1/oldtarget', $this->vendorDir.'/vendor/package1/newtarget');
$initial = self::getPackage('vendor/package1', '1.0.0');
$target = self::getPackage('vendor/package1', '2.0.0');
2022-02-18 12:05:12 +00:00
$initial->setTargetDir('oldtarget');
$target->setTargetDir('newtarget');
$this->repository
->expects($this->exactly(3))
->method('hasPackage')
->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')
->will($this->returnValue(\React\Promise\resolve(null)));
2011-09-23 21:23:16 +00:00
$this->repository
2011-09-23 21:23:16 +00:00
->expects($this->once())
->method('removePackage')
2011-09-23 21:23:16 +00:00
->with($initial);
$this->repository
2011-09-23 21:23:16 +00:00
->expects($this->once())
->method('addPackage')
->with($target);
2011-09-23 21:23:16 +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);
$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
}
public function testUninstall(): void
2011-09-23 21:23:16 +00:00
{
$library = new LibraryInstaller($this->io, $this->composer);
$package = self::getPackage('vendor/pkg', '1.0.0');
$this->repository
2011-09-23 21:23:16 +00:00
->expects($this->exactly(2))
->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')
->will($this->returnValue(\React\Promise\resolve(null)));
2011-09-23 21:23:16 +00:00
$this->repository
2011-09-23 21:23:16 +00:00
->expects($this->once())
->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
{
$library = new LibraryInstaller($this->io, $this->composer);
$package = self::getPackage('Vendor/Pkg', '1.0.0');
2022-02-18 12:05:12 +00:00
$this->assertEquals($this->vendorDir.'/'.$package->getPrettyName(), $library->getInstallPath($package));
}
public function testGetInstallPathWithTargetDir(): void
{
$library = new LibraryInstaller($this->io, $this->composer);
$package = self::getPackage('Foo/Bar', '1.0.0');
2022-02-18 12:05:12 +00:00
$package->setTargetDir('Some/Namespace');
2011-12-15 14:14:33 +00:00
$this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
}
/**
* @depends testInstallerCreationShouldNotCreateVendorDirectory
* @depends testInstallerCreationShouldNotCreateBinDirectory
*/
public function testEnsureBinariesInstalled(): void
{
$binaryInstallerMock = $this->getMockBuilder('Composer\Installer\BinaryInstaller')
->disableOriginalConstructor()
->getMock();
$library = new LibraryInstaller($this->io, $this->composer, 'library', null, $binaryInstallerMock);
$package = self::getPackage('foo/bar', '1.0.0');
$binaryInstallerMock
->expects($this->never())
->method('removeBinaries')
->with($package);
$binaryInstallerMock
->expects($this->once())
->method('installBinaries')
->with($package, $library->getInstallPath($package), false);
$library->ensureBinariesPresence($package);
}
2011-09-23 21:23:16 +00:00
}