2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2011-11-05 22:51:35 +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.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 14:23:32 +00:00
|
|
|
namespace Composer\Test\Plugin;
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
use Composer\Composer;
|
|
|
|
use Composer\Config;
|
2013-08-13 11:25:21 +00:00
|
|
|
use Composer\Installer\PluginInstaller;
|
2022-07-05 14:22:29 +00:00
|
|
|
use Composer\Json\JsonFile;
|
2022-01-21 11:48:16 +00:00
|
|
|
use Composer\Package\CompleteAliasPackage;
|
2015-06-02 01:21:06 +00:00
|
|
|
use Composer\Package\CompletePackage;
|
2011-11-05 22:51:35 +00:00
|
|
|
use Composer\Package\Loader\JsonLoader;
|
2012-07-18 15:20:01 +00:00
|
|
|
use Composer\Package\Loader\ArrayLoader;
|
2022-07-05 14:22:29 +00:00
|
|
|
use Composer\Package\Locker;
|
2021-01-12 13:00:02 +00:00
|
|
|
use Composer\Package\RootPackage;
|
2013-09-05 12:32:09 +00:00
|
|
|
use Composer\Plugin\PluginManager;
|
2019-02-18 17:14:46 +00:00
|
|
|
use Composer\IO\BufferIO;
|
|
|
|
use Composer\EventDispatcher\EventDispatcher;
|
2013-01-06 19:34:52 +00:00
|
|
|
use Composer\Autoload\AutoloadGenerator;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2013-09-20 03:39:35 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2022-07-05 14:22:29 +00:00
|
|
|
use Composer\Util\Platform;
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2015-06-02 01:21:06 +00:00
|
|
|
class PluginInstallerTest extends TestCase
|
2011-11-05 22:51:35 +00:00
|
|
|
{
|
2015-06-02 01:21:06 +00:00
|
|
|
/**
|
|
|
|
* @var Composer
|
|
|
|
*/
|
2012-06-24 18:11:06 +00:00
|
|
|
protected $composer;
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PluginManager
|
|
|
|
*/
|
|
|
|
protected $pm;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AutoloadGenerator
|
|
|
|
*/
|
|
|
|
protected $autoloadGenerator;
|
|
|
|
|
|
|
|
/**
|
2022-01-21 11:48:16 +00:00
|
|
|
* @var array<CompletePackage|CompleteAliasPackage>
|
2015-06-02 01:21:06 +00:00
|
|
|
*/
|
2012-06-24 18:11:06 +00:00
|
|
|
protected $packages;
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $directory;
|
|
|
|
|
|
|
|
/**
|
2021-08-21 15:41:52 +00:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject&\Composer\Installer\InstallationManager
|
2015-06-02 01:21:06 +00:00
|
|
|
*/
|
2012-06-24 18:11:06 +00:00
|
|
|
protected $im;
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-21 15:41:52 +00:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject&\Composer\Repository\InstalledRepositoryInterface
|
2015-06-02 01:21:06 +00:00
|
|
|
*/
|
2012-06-24 18:11:06 +00:00
|
|
|
protected $repository;
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-09 14:49:40 +00:00
|
|
|
* @var BufferIO
|
2015-06-02 01:21:06 +00:00
|
|
|
*/
|
2012-06-24 18:11:06 +00:00
|
|
|
protected $io;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2011-11-05 22:51:35 +00:00
|
|
|
{
|
2012-07-18 15:20:01 +00:00
|
|
|
$loader = new JsonLoader(new ArrayLoader());
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->packages = [];
|
2022-05-11 14:05:35 +00:00
|
|
|
$this->directory = self::getUniqueTmpDirectory();
|
2016-04-28 19:11:33 +00:00
|
|
|
for ($i = 1; $i <= 8; $i++) {
|
2013-09-20 03:39:35 +00:00
|
|
|
$filename = '/Fixtures/plugin-v'.$i.'/composer.json';
|
2014-10-17 17:46:01 +00:00
|
|
|
mkdir(dirname($this->directory . $filename), 0777, true);
|
2013-09-20 03:39:35 +00:00
|
|
|
$this->packages[] = $loader->load(__DIR__ . $filename);
|
2011-11-05 22:51:35 +00:00
|
|
|
}
|
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
$dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
|
2011-11-05 22:51:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2022-02-22 21:10:52 +00:00
|
|
|
$dm->expects($this->any())
|
|
|
|
->method('install')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2022-02-22 21:10:52 +00:00
|
|
|
$dm->expects($this->any())
|
|
|
|
->method('update')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2022-02-22 21:10:52 +00:00
|
|
|
$dm->expects($this->any())
|
|
|
|
->method('remove')
|
2022-03-18 08:20:42 +00:00
|
|
|
->will($this->returnValue(\React\Promise\resolve(null)));
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
|
2012-01-17 22:13:35 +00:00
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
$rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$rm->expects($this->any())
|
2013-03-03 16:18:50 +00:00
|
|
|
->method('getLocalRepository')
|
|
|
|
->will($this->returnValue($this->repository));
|
2012-06-24 18:11:06 +00:00
|
|
|
|
2019-01-17 16:12:33 +00:00
|
|
|
$im = $this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock();
|
2013-09-12 11:28:17 +00:00
|
|
|
$im->expects($this->any())
|
|
|
|
->method('getInstallPath')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static function ($package): string {
|
2013-09-12 11:28:17 +00:00
|
|
|
return __DIR__.'/Fixtures/'.$package->getPrettyName();
|
|
|
|
}));
|
|
|
|
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->io = new BufferIO();
|
2012-06-24 18:11:06 +00:00
|
|
|
|
2013-08-14 15:42:11 +00:00
|
|
|
$dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
|
2013-01-06 19:34:52 +00:00
|
|
|
$this->autoloadGenerator = new AutoloadGenerator($dispatcher);
|
|
|
|
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->composer = new Composer();
|
2020-02-07 04:43:57 +00:00
|
|
|
$config = new Config(false);
|
2012-06-24 18:11:06 +00:00
|
|
|
$this->composer->setConfig($config);
|
|
|
|
$this->composer->setDownloadManager($dm);
|
|
|
|
$this->composer->setRepositoryManager($rm);
|
2013-09-12 11:28:17 +00:00
|
|
|
$this->composer->setInstallationManager($im);
|
2013-01-06 19:34:52 +00:00
|
|
|
$this->composer->setAutoloadGenerator($this->autoloadGenerator);
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->composer->setEventDispatcher(new EventDispatcher($this->composer, $this->io));
|
2021-01-12 13:00:02 +00:00
|
|
|
$this->composer->setPackage(new RootPackage('dummy/root', '1.0.0.0', '1.0.0'));
|
2022-07-05 14:22:29 +00:00
|
|
|
$this->composer->setLocker(new Locker($this->io, new JsonFile(Platform::getDevNull()), $im, '{}'));
|
2012-06-24 18:11:06 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$config->merge([
|
|
|
|
'config' => [
|
2013-09-20 03:39:35 +00:00
|
|
|
'vendor-dir' => $this->directory.'/Fixtures/',
|
|
|
|
'home' => $this->directory.'/Fixtures',
|
|
|
|
'bin-dir' => $this->directory.'/Fixtures/bin',
|
2021-12-07 22:00:48 +00:00
|
|
|
'allow-plugins' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
]);
|
2021-12-07 22:00:48 +00:00
|
|
|
|
|
|
|
$this->pm = new PluginManager($this->io, $this->composer);
|
|
|
|
$this->composer->setPluginManager($this->pm);
|
2011-11-05 22:51:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2013-09-22 17:41:54 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2013-09-20 03:39:35 +00:00
|
|
|
$filesystem = new Filesystem();
|
2013-09-20 04:31:06 +00:00
|
|
|
$filesystem->removeDirectory($this->directory);
|
2013-09-20 03:39:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstallNewPlugin(): void
|
2011-11-05 22:51:35 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2011-11-05 22:51:35 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([]));
|
2013-08-13 17:13:17 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$installer->install($this->repository, $this->packages[0]);
|
2013-08-13 17:13:17 +00:00
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('installer-v1', $plugins[0]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2021-12-07 22:00:48 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
'activate v1'.PHP_EOL,
|
|
|
|
$this->io->getOutput()
|
|
|
|
);
|
2011-11-05 22:51:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstallPluginWithRootPackageHavingFilesAutoload(): void
|
2021-12-22 15:04:34 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([]));
|
2021-12-22 15:04:34 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
|
|
|
|
|
|
|
$this->autoloadGenerator->setDevMode(true);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->composer->getPackage()->setAutoload(['files' => [__DIR__ . '/Fixtures/files_autoload_which_should_not_run.php']]);
|
|
|
|
$this->composer->getPackage()->setDevAutoload(['files' => [__DIR__ . '/Fixtures/files_autoload_which_should_not_run.php']]);
|
2021-12-22 15:04:34 +00:00
|
|
|
$installer->install($this->repository, $this->packages[0]);
|
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
|
|
|
$this->assertEquals(
|
|
|
|
'activate v1'.PHP_EOL,
|
|
|
|
$this->io->getOutput()
|
|
|
|
);
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('installer-v1', $plugins[0]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2021-12-22 15:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstallMultiplePlugins(): void
|
2012-05-25 08:28:53 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2012-05-25 08:28:53 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([$this->packages[3]]));
|
2013-08-13 17:13:17 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
2012-05-25 08:28:53 +00:00
|
|
|
|
|
|
|
$installer->install($this->repository, $this->packages[3]);
|
2013-08-13 17:13:17 +00:00
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('plugin1', $plugins[0]->name); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
|
|
|
$this->assertEquals('installer-v4', $plugins[0]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
|
|
|
$this->assertEquals('plugin2', $plugins[1]->name); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
|
|
|
$this->assertEquals('installer-v4', $plugins[1]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->assertEquals('activate v4-plugin1'.PHP_EOL.'activate v4-plugin2'.PHP_EOL, $this->io->getOutput());
|
2012-05-25 08:28:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUpgradeWithNewClassName(): void
|
2011-11-05 22:51:35 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2011-11-05 22:51:35 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([$this->packages[0]]));
|
2011-11-05 22:51:35 +00:00
|
|
|
$this->repository
|
2012-02-18 22:21:45 +00:00
|
|
|
->expects($this->exactly(2))
|
2011-11-05 22:51:35 +00:00
|
|
|
->method('hasPackage')
|
2012-02-18 22:21:45 +00:00
|
|
|
->will($this->onConsecutiveCalls(true, false));
|
2013-08-13 17:13:17 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$installer->update($this->repository, $this->packages[0], $this->packages[1]);
|
2013-08-13 17:13:17 +00:00
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->assertCount(1, $plugins);
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('installer-v2', $plugins[1]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->assertEquals('activate v1'.PHP_EOL.'deactivate v1'.PHP_EOL.'activate v2'.PHP_EOL, $this->io->getOutput());
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUninstall(): void
|
2019-02-18 17:14:46 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2019-02-18 17:14:46 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([$this->packages[0]]));
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->repository
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('hasPackage')
|
|
|
|
->will($this->onConsecutiveCalls(true, false));
|
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
|
|
|
|
|
|
|
$installer->uninstall($this->repository, $this->packages[0]);
|
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
|
|
|
$this->assertCount(0, $plugins);
|
|
|
|
$this->assertEquals('activate v1'.PHP_EOL.'deactivate v1'.PHP_EOL.'uninstall v1'.PHP_EOL, $this->io->getOutput());
|
2011-11-05 22:51:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testUpgradeWithSameClassName(): void
|
2011-11-05 22:51:35 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2011-11-05 22:51:35 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([$this->packages[1]]));
|
2011-11-05 22:51:35 +00:00
|
|
|
$this->repository
|
2012-02-18 22:21:45 +00:00
|
|
|
->expects($this->exactly(2))
|
2011-11-05 22:51:35 +00:00
|
|
|
->method('hasPackage')
|
2012-02-18 22:21:45 +00:00
|
|
|
->will($this->onConsecutiveCalls(true, false));
|
2013-08-13 17:13:17 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2012-04-14 13:45:25 +00:00
|
|
|
$installer->update($this->repository, $this->packages[1], $this->packages[2]);
|
2011-11-05 22:51:35 +00:00
|
|
|
|
2013-08-13 17:13:17 +00:00
|
|
|
$plugins = $this->pm->getPlugins();
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('installer-v3', $plugins[1]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->assertEquals('activate v2'.PHP_EOL.'deactivate v2'.PHP_EOL.'activate v3'.PHP_EOL, $this->io->getOutput());
|
2011-11-05 22:51:35 +00:00
|
|
|
}
|
2014-07-21 13:07:27 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testRegisterPluginOnlyOneTime(): void
|
2014-07-21 13:07:27 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2014-07-21 13:07:27 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([]));
|
2014-07-21 13:07:27 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
|
|
|
|
|
|
|
$installer->install($this->repository, $this->packages[0]);
|
|
|
|
$installer->install($this->repository, clone $this->packages[0]);
|
|
|
|
|
|
|
|
$plugins = $this->pm->getPlugins();
|
|
|
|
$this->assertCount(1, $plugins);
|
2024-05-22 07:09:04 +00:00
|
|
|
$this->assertEquals('installer-v1', $plugins[0]->version); // @phpstan-ignore staticMethod.dynamicCall, property.notFound
|
2019-02-18 17:14:46 +00:00
|
|
|
$this->assertEquals('activate v1'.PHP_EOL, $this->io->getOutput());
|
2014-07-21 13:07:27 +00:00
|
|
|
}
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-21 11:48:16 +00:00
|
|
|
* @param array<CompletePackage|CompleteAliasPackage> $plugins
|
2015-06-02 01:21:06 +00:00
|
|
|
*/
|
2022-08-17 12:20:07 +00:00
|
|
|
private function setPluginApiVersionWithPlugins(string $newPluginApiVersion, array $plugins = []): void
|
2015-06-02 01:21:06 +00:00
|
|
|
{
|
|
|
|
// reset the plugin manager's installed plugins
|
2015-06-02 16:39:02 +00:00
|
|
|
$this->pm = $this->getMockBuilder('Composer\Plugin\PluginManager')
|
2022-08-17 12:20:07 +00:00
|
|
|
->onlyMethods(['getPluginApiVersion'])
|
|
|
|
->setConstructorArgs([$this->io, $this->composer])
|
2015-06-02 16:39:02 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// mock the Plugin API version
|
|
|
|
$this->pm->expects($this->any())
|
|
|
|
->method('getPluginApiVersion')
|
|
|
|
->will($this->returnValue($newPluginApiVersion));
|
2015-06-02 01:21:06 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
$plugApiInternalPackage = self::getPackage(
|
2015-06-02 01:21:06 +00:00
|
|
|
'composer-plugin-api',
|
|
|
|
$newPluginApiVersion,
|
|
|
|
'Composer\Package\CompletePackage'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add the plugins to the repo along with the internal Plugin package on which they all rely.
|
|
|
|
$this->repository
|
2018-09-11 11:33:29 +00:00
|
|
|
->expects($this->any())
|
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static function () use ($plugApiInternalPackage, $plugins): array {
|
|
|
|
return array_merge([$plugApiInternalPackage], $plugins);
|
2018-09-11 11:33:29 +00:00
|
|
|
}));
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
$this->pm->loadInstalledPlugins();
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStarPluginVersionWorksWithAnyAPIVersion(): void
|
2015-06-02 01:21:06 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$starVersionPlugin = [$this->packages[4]];
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.0.0', $starVersionPlugin);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.9.9', $starVersionPlugin);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('2.0.0-dev', $starVersionPlugin);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('100.0.0-stable', $starVersionPlugin);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testPluginConstraintWorksOnlyWithCertainAPIVersion(): void
|
2015-06-02 01:21:06 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$pluginWithApiConstraint = [$this->packages[5]];
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.0.0', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(0, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.1.9', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(0, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.2.0', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.9.9', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testPluginRangeConstraintsWorkOnlyWithCertainAPIVersion(): void
|
2015-06-02 01:21:06 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$pluginWithApiConstraint = [$this->packages[6]];
|
2015-06-02 01:21:06 +00:00
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('1.0.0', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(0, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('3.0.0', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(1, $this->pm->getPlugins());
|
|
|
|
|
|
|
|
$this->setPluginApiVersionWithPlugins('5.5.0', $pluginWithApiConstraint);
|
|
|
|
$this->assertCount(0, $this->pm->getPlugins());
|
|
|
|
}
|
2015-06-09 10:33:57 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCommandProviderCapability(): void
|
2016-04-28 19:37:34 +00:00
|
|
|
{
|
|
|
|
$this->repository
|
2020-02-13 16:51:22 +00:00
|
|
|
->expects($this->any())
|
2016-04-28 19:37:34 +00:00
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([$this->packages[7]]));
|
2016-04-28 19:37:34 +00:00
|
|
|
$installer = new PluginInstaller($this->io, $this->composer);
|
|
|
|
$this->pm->loadInstalledPlugins();
|
|
|
|
|
2021-03-09 14:49:40 +00:00
|
|
|
/** @var \Composer\Plugin\Capability\CommandProvider[] $caps */
|
2022-08-17 12:20:07 +00:00
|
|
|
$caps = $this->pm->getPluginCapabilities('Composer\Plugin\Capability\CommandProvider', ['composer' => $this->composer, 'io' => $this->io]);
|
2016-04-28 19:37:34 +00:00
|
|
|
$this->assertCount(1, $caps);
|
|
|
|
$this->assertInstanceOf('Composer\Plugin\Capability\CommandProvider', $caps[0]);
|
|
|
|
|
|
|
|
$commands = $caps[0]->getCommands();
|
|
|
|
$this->assertCount(1, $commands);
|
|
|
|
$this->assertInstanceOf('Composer\Command\BaseCommand', $commands[0]);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIncapablePluginIsCorrectlyDetected(): void
|
2015-06-09 10:33:57 +00:00
|
|
|
{
|
|
|
|
$plugin = $this->getMockBuilder('Composer\Plugin\PluginInterface')
|
|
|
|
->getMock();
|
2016-01-22 13:48:29 +00:00
|
|
|
$this->assertNull($this->pm->getPluginCapability($plugin, 'Fake\Ability'));
|
2015-06-09 10:33:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCapabilityImplementsComposerPluginApiClassAndIsConstructedWithArgs(): void
|
2015-06-09 10:33:57 +00:00
|
|
|
{
|
|
|
|
$capabilityApi = 'Composer\Plugin\Capability\Capability';
|
2016-01-22 13:48:29 +00:00
|
|
|
$capabilityImplementation = 'Composer\Test\Plugin\Mock\Capability';
|
2015-06-09 10:33:57 +00:00
|
|
|
|
|
|
|
$plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$plugin->expects($this->once())
|
|
|
|
->method('getCapabilities')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static function () use ($capabilityImplementation, $capabilityApi): array {
|
|
|
|
return [$capabilityApi => $capabilityImplementation];
|
2015-06-09 10:33:57 +00:00
|
|
|
}));
|
|
|
|
|
2021-03-09 14:49:40 +00:00
|
|
|
/** @var \Composer\Test\Plugin\Mock\Capability $capability */
|
2022-08-17 12:20:07 +00:00
|
|
|
$capability = $this->pm->getPluginCapability($plugin, $capabilityApi, ['a' => 1, 'b' => 2]);
|
2015-06-09 10:33:57 +00:00
|
|
|
|
|
|
|
$this->assertInstanceOf($capabilityApi, $capability);
|
2016-01-22 13:48:29 +00:00
|
|
|
$this->assertInstanceOf($capabilityImplementation, $capability);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertSame(['a' => 1, 'b' => 2, 'plugin' => $plugin], $capability->args);
|
2015-06-09 10:33:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:52:34 +00:00
|
|
|
/** @return mixed[] */
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function invalidImplementationClassNames(): array
|
2015-06-09 10:33:57 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
[""],
|
|
|
|
[0],
|
|
|
|
[1000],
|
|
|
|
[" "],
|
|
|
|
[[1]],
|
|
|
|
[[]],
|
|
|
|
[new \stdClass()],
|
|
|
|
];
|
2016-01-22 13:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 10:33:57 +00:00
|
|
|
/**
|
2016-01-22 13:48:29 +00:00
|
|
|
* @dataProvider invalidImplementationClassNames
|
2022-02-22 15:47:09 +00:00
|
|
|
* @param mixed $invalidImplementationClassNames
|
2021-12-09 19:55:26 +00:00
|
|
|
* @param class-string<\Throwable> $expect
|
2015-06-09 10:33:57 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testQueryingWithInvalidCapabilityClassNameThrows($invalidImplementationClassNames, string $expect = 'UnexpectedValueException'): void
|
2015-06-09 10:33:57 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException($expect);
|
2020-09-10 15:21:11 +00:00
|
|
|
|
2015-06-09 10:33:57 +00:00
|
|
|
$capabilityApi = 'Composer\Plugin\Capability\Capability';
|
|
|
|
|
|
|
|
$plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$plugin->expects($this->once())
|
|
|
|
->method('getCapabilities')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static function () use ($invalidImplementationClassNames, $capabilityApi): array {
|
|
|
|
return [$capabilityApi => $invalidImplementationClassNames];
|
2015-06-09 10:33:57 +00:00
|
|
|
}));
|
|
|
|
|
2016-01-22 13:48:29 +00:00
|
|
|
$this->pm->getPluginCapability($plugin, $capabilityApi);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testQueryingNonProvidedCapabilityReturnsNullSafely(): void
|
2016-01-22 19:09:44 +00:00
|
|
|
{
|
|
|
|
$capabilityApi = 'Composer\Plugin\Capability\MadeUpCapability';
|
|
|
|
|
|
|
|
$plugin = $this->getMockBuilder('Composer\Test\Plugin\Mock\CapablePluginInterface')
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$plugin->expects($this->once())
|
|
|
|
->method('getCapabilities')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnCallback(static function (): array {
|
|
|
|
return [];
|
2016-01-22 19:09:44 +00:00
|
|
|
}));
|
2021-10-27 14:18:24 +00:00
|
|
|
|
2016-01-22 19:09:44 +00:00
|
|
|
$this->assertNull($this->pm->getPluginCapability($plugin, $capabilityApi));
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
/** @return mixed[] */
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function nonExistingOrInvalidImplementationClassTypes(): array
|
2022-02-22 15:47:09 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['\stdClass'],
|
|
|
|
['NonExistentClassLikeMiddleClass'],
|
|
|
|
];
|
2022-02-22 15:47:09 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:48:29 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider nonExistingOrInvalidImplementationClassTypes
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testQueryingWithNonExistingOrWrongCapabilityClassTypesThrows(string $wrongImplementationClassTypes): void
|
2016-01-22 13:48:29 +00:00
|
|
|
{
|
2020-09-10 15:21:11 +00:00
|
|
|
$this->testQueryingWithInvalidCapabilityClassNameThrows($wrongImplementationClassTypes, 'RuntimeException');
|
2015-06-09 10:33:57 +00:00
|
|
|
}
|
2012-06-14 10:10:01 +00:00
|
|
|
}
|