2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2016-03-02 21:13:06 +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;
|
|
|
|
|
2022-02-02 14:01:55 +00:00
|
|
|
use Composer\InstalledVersions;
|
2016-03-02 21:13:06 +00:00
|
|
|
use Composer\Installer\SuggestedPackagesReporter;
|
2022-02-02 14:01:55 +00:00
|
|
|
use Composer\Semver\VersionParser;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass Composer\Installer\SuggestedPackagesReporter
|
|
|
|
*/
|
2017-11-04 14:52:13 +00:00
|
|
|
class SuggestedPackagesReporterTest extends TestCase
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2021-10-27 12:41:30 +00:00
|
|
|
/**
|
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2016-03-02 21:13:06 +00:00
|
|
|
private $io;
|
2021-10-27 12:41:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Composer\Installer\SuggestedPackagesReporter
|
|
|
|
*/
|
2016-03-02 21:13:06 +00:00
|
|
|
private $suggestedPackagesReporter;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2018-04-12 08:24:56 +00:00
|
|
|
$this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
$this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::__construct
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testConstructor(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$this->io->expects($this->once())
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write');
|
2016-03-02 21:13:06 +00:00
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
|
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_LIST);
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getPackages
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetPackagesEmptyByDefault(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2017-11-30 14:58:10 +00:00
|
|
|
$this->assertEmpty($this->suggestedPackagesReporter->getPackages());
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::getPackages
|
|
|
|
* @covers ::addPackage
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetPackages(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$suggestedPackage = $this->getSuggestedPackageArray();
|
|
|
|
$this->suggestedPackagesReporter->addPackage(
|
|
|
|
$suggestedPackage['source'],
|
|
|
|
$suggestedPackage['target'],
|
|
|
|
$suggestedPackage['reason']
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2022-08-17 12:20:07 +00:00
|
|
|
[$suggestedPackage],
|
2016-03-02 21:13:06 +00:00
|
|
|
$this->suggestedPackagesReporter->getPackages()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test addPackage appends packages.
|
|
|
|
* Also test targets can be duplicated.
|
|
|
|
*
|
|
|
|
* @covers ::addPackage
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddPackageAppends(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$suggestedPackageA = $this->getSuggestedPackageArray();
|
|
|
|
$suggestedPackageB = $this->getSuggestedPackageArray();
|
|
|
|
$suggestedPackageB['source'] = 'different source';
|
|
|
|
$suggestedPackageB['reason'] = 'different reason';
|
|
|
|
$this->suggestedPackagesReporter->addPackage(
|
|
|
|
$suggestedPackageA['source'],
|
|
|
|
$suggestedPackageA['target'],
|
|
|
|
$suggestedPackageA['reason']
|
|
|
|
);
|
|
|
|
$this->suggestedPackagesReporter->addPackage(
|
|
|
|
$suggestedPackageB['source'],
|
|
|
|
$suggestedPackageB['target'],
|
|
|
|
$suggestedPackageB['reason']
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
2022-08-17 12:20:07 +00:00
|
|
|
[$suggestedPackageA, $suggestedPackageB],
|
2016-03-02 21:13:06 +00:00
|
|
|
$this->suggestedPackagesReporter->getPackages()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::addSuggestionsFromPackage
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddSuggestionsFromPackage(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$package = $this->createPackageMock();
|
|
|
|
$package->expects($this->once())
|
|
|
|
->method('getSuggests')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([
|
2016-03-02 21:13:06 +00:00
|
|
|
'target-a' => 'reason-a',
|
|
|
|
'target-b' => 'reason-b',
|
2022-08-17 12:20:07 +00:00
|
|
|
]));
|
2016-03-02 21:13:06 +00:00
|
|
|
$package->expects($this->once())
|
|
|
|
->method('getPrettyName')
|
|
|
|
->will($this->returnValue('package-pretty-name'));
|
|
|
|
|
|
|
|
$this->suggestedPackagesReporter->addSuggestionsFromPackage($package);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertSame([
|
|
|
|
[
|
2016-03-02 21:13:06 +00:00
|
|
|
'source' => 'package-pretty-name',
|
|
|
|
'target' => 'target-a',
|
|
|
|
'reason' => 'reason-a',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2016-03-02 21:13:06 +00:00
|
|
|
'source' => 'package-pretty-name',
|
|
|
|
'target' => 'target-b',
|
|
|
|
'reason' => 'reason-b',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
], $this->suggestedPackagesReporter->getPackages());
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutput(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
|
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->io->expects($this->exactly(3))
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive(
|
|
|
|
['<comment>a</comment> suggests:'],
|
|
|
|
[' - <info>b</info>: c'],
|
|
|
|
['']
|
|
|
|
);
|
2016-03-02 21:13:06 +00:00
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 11:23:41 +00:00
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutputWithNoSuggestionReason(): void
|
2018-06-09 11:23:41 +00:00
|
|
|
{
|
|
|
|
$this->suggestedPackagesReporter->addPackage('a', 'b', '');
|
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->io->expects($this->exactly(3))
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive(
|
|
|
|
['<comment>a</comment> suggests:'],
|
|
|
|
[' - <info>b</info>'],
|
|
|
|
['']
|
|
|
|
);
|
2018-06-09 11:23:41 +00:00
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
|
2018-06-09 11:23:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 11:37:25 +00:00
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutputIgnoresFormatting(): void
|
2017-04-08 11:37:25 +00:00
|
|
|
{
|
2017-04-19 15:24:47 +00:00
|
|
|
$this->suggestedPackagesReporter->addPackage('source', 'target1', "\x1b[1;37;42m Like us\r\non Facebook \x1b[0m");
|
|
|
|
$this->suggestedPackagesReporter->addPackage('source', 'target2', "<bg=green>Like us on Facebook</>");
|
2017-04-08 11:37:25 +00:00
|
|
|
|
2022-02-02 14:01:55 +00:00
|
|
|
$expectedWrite = InstalledVersions::satisfies(new VersionParser(), 'symfony/console', '^4.4.37 || ~5.3.14 || ^5.4.3 || ^6.0.3')
|
|
|
|
? ' - <info>target2</info>: \\<bg=green\\>Like us on Facebook\\</\\>'
|
|
|
|
: ' - <info>target2</info>: \\<bg=green>Like us on Facebook\\</>';
|
2022-02-04 15:31:14 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->io->expects($this->exactly(4))
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive(
|
|
|
|
['<comment>source</comment> suggests:'],
|
|
|
|
[' - <info>target1</info>: [1;37;42m Like us on Facebook [0m'],
|
2022-02-04 15:31:14 +00:00
|
|
|
[$expectedWrite],
|
2021-12-09 16:09:07 +00:00
|
|
|
['']
|
|
|
|
);
|
2020-02-13 15:02:31 +00:00
|
|
|
|
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
|
2017-04-08 11:37:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 21:13:06 +00:00
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutputMultiplePackages(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
|
|
|
$this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
|
|
|
|
$this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
|
|
|
|
|
2022-02-18 07:50:11 +00:00
|
|
|
$this->io->expects($this->exactly(6))
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive(
|
|
|
|
['<comment>a</comment> suggests:'],
|
|
|
|
[' - <info>b</info>: c'],
|
|
|
|
[''],
|
|
|
|
['<comment>source package</comment> suggests:'],
|
|
|
|
[' - <info>target</info>: because reasons'],
|
|
|
|
['']
|
|
|
|
);
|
2016-03-02 21:13:06 +00:00
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE);
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutputSkipInstalledPackages(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2020-04-23 08:29:22 +00:00
|
|
|
$repository = $this->getMockBuilder('Composer\Repository\InstalledRepository')->disableOriginalConstructor()->getMock();
|
2018-04-12 08:24:56 +00:00
|
|
|
$package1 = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
|
|
|
$package2 = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
$package1->expects($this->once())
|
|
|
|
->method('getNames')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['x', 'y']));
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
$package2->expects($this->once())
|
|
|
|
->method('getNames')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue(['b']));
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
$repository->expects($this->once())
|
|
|
|
->method('getPackages')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([
|
2016-03-02 21:13:06 +00:00
|
|
|
$package1,
|
|
|
|
$package2,
|
2022-08-17 12:20:07 +00:00
|
|
|
]));
|
2016-03-02 21:13:06 +00:00
|
|
|
|
|
|
|
$this->suggestedPackagesReporter->addPackage('a', 'b', 'c');
|
|
|
|
$this->suggestedPackagesReporter->addPackage('source package', 'target', 'because reasons');
|
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$this->io->expects($this->exactly(3))
|
2020-02-13 15:02:31 +00:00
|
|
|
->method('write')
|
2021-12-09 16:09:07 +00:00
|
|
|
->withConsecutive(
|
|
|
|
['<comment>source package</comment> suggests:'],
|
|
|
|
[' - <info>target</info>: because reasons'],
|
|
|
|
['']
|
|
|
|
);
|
2016-03-02 21:13:06 +00:00
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE, $repository);
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::output
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOutputNotGettingInstalledPackagesWhenNoSuggestions(): void
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2020-04-23 08:29:22 +00:00
|
|
|
$repository = $this->getMockBuilder('Composer\Repository\InstalledRepository')->disableOriginalConstructor()->getMock();
|
2016-03-02 21:13:06 +00:00
|
|
|
$repository->expects($this->exactly(0))
|
|
|
|
->method('getPackages');
|
|
|
|
|
2020-02-13 15:02:31 +00:00
|
|
|
$this->suggestedPackagesReporter->output(SuggestedPackagesReporter::MODE_BY_PACKAGE, $repository);
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:41:30 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
2022-02-18 10:22:01 +00:00
|
|
|
private function getSuggestedPackageArray(): array
|
2016-03-02 21:13:06 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2016-03-02 21:13:06 +00:00
|
|
|
'source' => 'a',
|
|
|
|
'target' => 'b',
|
|
|
|
'reason' => 'c',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2016-03-02 21:13:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:41:30 +00:00
|
|
|
/**
|
|
|
|
* @return \Composer\Package\PackageInterface&\PHPUnit\Framework\MockObject\MockObject
|
|
|
|
*/
|
2016-03-02 21:13:06 +00:00
|
|
|
private function createPackageMock()
|
|
|
|
{
|
|
|
|
return $this->getMockBuilder('Composer\Package\Package')
|
2022-08-17 12:20:07 +00:00
|
|
|
->setConstructorArgs([md5((string) mt_rand()), '1.0.0.0', '1.0.0'])
|
2016-03-02 21:13:06 +00:00
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|