Skip to content
Snippets Groups Projects
Commit 574cf7fa authored by vnayda's avatar vnayda
Browse files

Merge remote-tracking branch 'origin/develop' into MAGETWO-52717

parents 7f30596d 073f227d
No related merge requests found
......@@ -409,6 +409,7 @@ class PackagesData
return in_array(
$item['package_type'],
[
\Magento\Setup\Model\Grid\TypeMapper::LANGUAGE_PACKAGE_TYPE,
\Magento\Setup\Model\Grid\TypeMapper::MODULE_PACKAGE_TYPE,
\Magento\Setup\Model\Grid\TypeMapper::EXTENSION_PACKAGE_TYPE,
\Magento\Setup\Model\Grid\TypeMapper::THEME_PACKAGE_TYPE,
......@@ -491,26 +492,38 @@ class PackagesData
return array_keys($packageVersions);
}
} else {
$versionsPattern = '/^versions\s*\:\s(.+)$/m';
$commandParams = [
self::PARAM_COMMAND => self::COMPOSER_SHOW,
self::PARAM_PACKAGE => $package,
self::PARAM_AVAILABLE => true
];
$applicationFactory = $this->objectManagerProvider->get()
->get(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class);
/** @var \Magento\Composer\MagentoComposerApplication $application */
$application = $applicationFactory->create();
$result = $application->runComposerCommand($commandParams);
$matches = [];
preg_match($versionsPattern, $result, $matches);
if (isset($matches[1])) {
return explode(', ', $matches[1]);
}
}
return $this->getAvailableVersionsFromAllRepositories($package);
}
/**
* Get available versions of package by "composer show" command
*
* @param string $package
* @return array
* @exception \RuntimeException
*/
private function getAvailableVersionsFromAllRepositories($package)
{
$versionsPattern = '/^versions\s*\:\s(.+)$/m';
$commandParams = [
self::PARAM_COMMAND => self::COMPOSER_SHOW,
self::PARAM_PACKAGE => $package,
self::PARAM_AVAILABLE => true
];
$applicationFactory = $this->objectManagerProvider->get()
->get(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class);
/** @var \Magento\Composer\MagentoComposerApplication $application */
$application = $applicationFactory->create();
$result = $application->runComposerCommand($commandParams);
$matches = [];
preg_match($versionsPattern, $result, $matches);
if (isset($matches[1])) {
return explode(', ', $matches[1]);
}
throw new \RuntimeException(
......
......@@ -22,38 +22,96 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
*/
private $packagesData;
/**
* @var ComposerInformation|MockObject
*/
private $composerInformation;
/**
* @var \Magento\Setup\Model\DateTime\TimeZoneProvider|MockObject
*/
private $timeZoneProvider;
/**
* @var \Magento\Setup\Model\PackagesAuth|MockObject
*/
private $packagesAuth;
/**
* @var \Magento\Framework\Filesystem|MockObject
*/
private $filesystem;
/**
* @var \Magento\Setup\Model\ObjectManagerProvider|MockObject
*/
private $objectManagerProvider;
/**
* @var \Magento\Setup\Model\Grid\TypeMapper|MockObject
*/
private $typeMapper;
public function setUp()
{
$composerInformation = $this->getComposerInformation();
$timeZoneProvider = $this->getMock(\Magento\Setup\Model\DateTime\TimeZoneProvider::class, [], [], '', false);
$this->composerInformation = $this->getComposerInformation();
$this->timeZoneProvider = $this->getMockBuilder(\Magento\Setup\Model\DateTime\TimeZoneProvider::class)
->disableOriginalConstructor()
->getMock();
$timeZone = $this->getMock(\Magento\Framework\Stdlib\DateTime\Timezone::class, [], [], '', false);
$timeZoneProvider->expects($this->any())->method('get')->willReturn($timeZone);
$packagesAuth = $this->getMock(\Magento\Setup\Model\PackagesAuth::class, [], [], '', false);
$filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
$objectManagerProvider = $this->getMock(\Magento\Setup\Model\ObjectManagerProvider::class, [], [], '', false);
$this->timeZoneProvider->expects($this->any())->method('get')->willReturn($timeZone);
$this->packagesAuth = $this->getMock(\Magento\Setup\Model\PackagesAuth::class, [], [], '', false);
$this->filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
$this->objectManagerProvider = $this->getMockBuilder(\Magento\Setup\Model\ObjectManagerProvider::class)
->disableOriginalConstructor()
->getMock();
$objectManager = $this->getMockForAbstractClass(\Magento\Framework\ObjectManagerInterface::class);
$applicationFactory = $this->getMock(
\Magento\Framework\Composer\MagentoComposerApplicationFactory::class,
[],
[],
'',
false
);
$appFactory = $this->getMockBuilder(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class)
->disableOriginalConstructor()
->getMock();
$application = $this->getMock(\Magento\Composer\MagentoComposerApplication::class, [], [], '', false);
$application->expects($this->any())
->method('runComposerCommand')
->willReturn('versions: 2.0.1');
$applicationFactory->expects($this->any())->method('create')->willReturn($application);
->willReturnMap([
[
[
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
PackagesData::PARAM_PACKAGE => 'magento/package-1',
PackagesData::PARAM_AVAILABLE => true,
],
null,
'versions: 2.0.1'
],
[
[
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
PackagesData::PARAM_PACKAGE => 'magento/package-2',
PackagesData::PARAM_AVAILABLE => true,
],
null,
'versions: 2.0.1'
],
[
[
PackagesData::PARAM_COMMAND => PackagesData::COMPOSER_SHOW,
PackagesData::PARAM_PACKAGE => 'partner/package-3',
PackagesData::PARAM_AVAILABLE => true,
],
null,
'versions: 3.0.1'
],
]);
$appFactory->expects($this->any())->method('create')->willReturn($application);
$objectManager->expects($this->any())
->method('get')
->with(\Magento\Framework\Composer\MagentoComposerApplicationFactory::class)
->willReturn($applicationFactory);
$objectManagerProvider->expects($this->any())->method('get')->willReturn($objectManager);
->willReturn($appFactory);
$this->objectManagerProvider->expects($this->any())->method('get')->willReturn($objectManager);
$directoryWrite = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
$directoryRead = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
$filesystem->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryRead));
$filesystem->expects($this->any())
$this->filesystem->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryRead));
$this->filesystem->expects($this->any())
->method('getDirectoryWrite')
->will($this->returnValue($directoryWrite));
$directoryWrite->expects($this->any())->method('isExist')->willReturn(true);
......@@ -81,32 +139,41 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
. '}}}'
);
$typeMapper = $this->getMockBuilder(\Magento\Setup\Model\Grid\TypeMapper::class)
$this->typeMapper = $this->getMockBuilder(\Magento\Setup\Model\Grid\TypeMapper::class)
->disableOriginalConstructor()
->getMock();
$typeMapper->expects(static::any())
$this->typeMapper->expects(static::any())
->method('map')
->willReturnMap([
[ComposerInformation::MODULE_PACKAGE_TYPE, \Magento\Setup\Model\Grid\TypeMapper::MODULE_PACKAGE_TYPE],
]);
$this->createPackagesData();
}
private function createPackagesData()
{
$this->packagesData = new PackagesData(
$composerInformation,
$timeZoneProvider,
$packagesAuth,
$filesystem,
$objectManagerProvider,
$typeMapper
$this->composerInformation,
$this->timeZoneProvider,
$this->packagesAuth,
$this->filesystem,
$this->objectManagerProvider,
$this->typeMapper
);
}
/**
* @param array $requiredPackages
* @param array $installedPackages
* @param array $repo
* @return ComposerInformation|MockObject
*/
private function getComposerInformation()
private function getComposerInformation($requiredPackages = [], $installedPackages = [], $repo = [])
{
$composerInformation = $this->getMock(ComposerInformation::class, [], [], '', false);
$composerInformation->expects($this->any())->method('getInstalledMagentoPackages')->willReturn(
$installedPackages ?:
[
'magento/package-1' => [
'name' => 'magento/package-1',
......@@ -117,21 +184,30 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
'name' => 'magento/package-2',
'type' => 'magento2-module',
'version'=> '1.0.1'
]
],
'partner/package-3' => [
'name' => 'partner/package-3',
'type' => 'magento2-module',
'version'=> '3.0.0'
],
]
);
$composerInformation->expects($this->any())->method('getRootRepositories')
->willReturn(['repo1', 'repo2']);
->willReturn($repo ?: ['repo1', 'repo2']);
$composerInformation->expects($this->any())->method('getPackagesTypes')
->willReturn(['magento2-module']);
$rootPackage = $this->getMock(RootPackage::class, [], ['magento/project', '2.1.0', '2']);
$rootPackage->expects($this->any())
->method('getRequires')
->willReturn([
'magento/package-1' => '1.0.0',
'magento/package-2' => '1.0.1'
]);
->willReturn(
$requiredPackages ?:
[
'magento/package-1' => '1.0.0',
'magento/package-2' => '1.0.1',
'partner/package-3' => '3.0.0',
]
);
$composerInformation->expects($this->any())
->method('getRootPackage')
->willReturn($rootPackage);
......@@ -146,19 +222,57 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
$this->assertArrayHasKey('date', $latestData['lastSyncDate']);
$this->assertArrayHasKey('time', $latestData['lastSyncDate']);
$this->assertArrayHasKey('packages', $latestData);
$this->assertSame(2, count($latestData['packages']));
$this->assertSame(2, $latestData['countOfUpdate']);
$this->assertSame(3, count($latestData['packages']));
$this->assertSame(3, $latestData['countOfUpdate']);
$this->assertArrayHasKey('installPackages', $latestData);
$this->assertSame(1, count($latestData['installPackages']));
$this->assertSame(1, $latestData['countOfInstall']);
}
public function testGetPackagesForUpdate()
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Couldn't get available versions for package partner/package-4
*/
public function testGetPackagesForUpdateWithException()
{
$requiredPackages = [
'partner/package-4' => '4.0.4',
];
$installedPackages = [
'partner/package-4' => [
'name' => 'partner/package-4',
'type' => 'magento2-module',
'version'=> '4.0.4'
],
];
$this->composerInformation = $this->getComposerInformation($requiredPackages, $installedPackages);
$this->createPackagesData();
$this->packagesData->getPackagesForUpdate();
}
public function testPackagesForUpdateFromJson()
{
$this->composerInformation = $this->getComposerInformation([], [], ['https://repo1']);
$this->packagesAuth->expects($this->atLeastOnce())
->method('getCredentialBaseUrl')
->willReturn('repo1');
$this->createPackagesData();
$packages = $this->packagesData->getPackagesForUpdate();
$this->assertEquals(2, count($packages));
$this->assertArrayHasKey('magento/package-1', $packages);
$this->assertArrayHasKey('partner/package-3', $packages);
$firstPackage = array_values($packages)[0];
$this->assertArrayHasKey('latestVersion', $firstPackage);
$this->assertArrayHasKey('versions', $firstPackage);
}
public function testGetPackagesForUpdate()
{
$packages = $this->packagesData->getPackagesForUpdate();
$this->assertEquals(3, count($packages));
$this->assertArrayHasKey('magento/package-1', $packages);
$this->assertArrayHasKey('magento/package-2', $packages);
$this->assertArrayHasKey('partner/package-3', $packages);
$firstPackage = array_values($packages)[0];
$this->assertArrayHasKey('latestVersion', $firstPackage);
$this->assertArrayHasKey('versions', $firstPackage);
......@@ -167,9 +281,10 @@ class PackagesDataTest extends \PHPUnit_Framework_TestCase
public function testGetInstalledPackages()
{
$installedPackages = $this->packagesData->getInstalledPackages();
$this->assertEquals(2, count($installedPackages));
$this->assertEquals(3, count($installedPackages));
$this->assertArrayHasKey('magento/package-1', $installedPackages);
$this->assertArrayHasKey('magento/package-2', $installedPackages);
$this->assertArrayHasKey('partner/package-3', $installedPackages);
}
public function testGetMetaPackagesMap()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment