Skip to content

Commit

Permalink
Merge branch 'dev' into adding-typing
Browse files Browse the repository at this point in the history
# Conflicts:
#	classes/TaskRunner/Miscellaneous/CompareReleases.php
#	classes/TaskRunner/Upgrade/UpgradeNow.php
#	classes/VersionUtils.php
  • Loading branch information
Morgan Pichat committed Jul 2, 2024
2 parents 20ca644 + dd83cb6 commit 79cd041
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 44 deletions.
3 changes: 1 addition & 2 deletions classes/ChannelInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function __construct(Upgrader $upgrader, UpgradeConfiguration $config, st
$this->channel = $channel;
$publicChannels = ['minor', 'major', 'rc', 'beta', 'alpha'];

preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches);
$upgrader->branch = $matches[1];
$upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major'];
$upgrader->channel = $channel;

if (in_array($channel, $publicChannels)) {
Expand Down
4 changes: 2 additions & 2 deletions classes/TaskRunner/Miscellaneous/CompareReleases.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\VersionUtils;
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;

/**
Expand All @@ -56,8 +57,7 @@ public function run(): int
$version = $this->container->getUpgradeConfiguration()->get('directory.version_num');
break;
default:
preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches);
$upgrader->branch = $matches[1];
$upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major'];
$upgrader->channel = $channel;
if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) {
$upgrader->checkPSVersion(false, ['private', 'minor']);
Expand Down
5 changes: 2 additions & 3 deletions classes/TaskRunner/Upgrade/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\FilesystemAdapter;
use PrestaShop\Module\AutoUpgrade\VersionUtils;

/**
* Download PrestaShop archive according to the chosen channel.
Expand All @@ -51,10 +52,8 @@ public function run(): int
}

$upgrader = $this->container->getUpgrader();
// regex optimization
preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches);
$upgrader->channel = $this->container->getUpgradeConfiguration()->get('channel');
$upgrader->branch = $matches[1];
$upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major'];
if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) {
$upgrader->checkPSVersion(false, ['private', 'minor']);
} else {
Expand Down
5 changes: 3 additions & 2 deletions classes/TaskRunner/Upgrade/UpgradeNow.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Exception;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;
use PrestaShop\Module\AutoUpgrade\VersionUtils;

/**
* very first step of the upgrade process. The only thing done is the selection
Expand All @@ -49,8 +50,8 @@ public function run(): int
$channel = $this->container->getUpgradeConfiguration()->get('channel');
$upgrader = $this->container->getUpgrader();
$this->next = 'download';
preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', _PS_VERSION_, $matches);
$upgrader->branch = $matches[1];

$upgrader->branch = VersionUtils::splitPrestaShopVersion(_PS_VERSION_)['major'];
$upgrader->channel = $channel;
if ($this->container->getUpgradeConfiguration()->get('channel') == 'private' && !$this->container->getUpgradeConfiguration()->get('private_allow_major')) {
$upgrader->checkPSVersion(false, ['private', 'minor']);
Expand Down
3 changes: 1 addition & 2 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ public function getUpgrader(): Upgrader
$this->getProperty(self::PS_VERSION),
$fileLoader
);
preg_match('#([0-9]+\.[0-9]+)(?:\.[0-9]+){1,2}#', $this->getProperty(self::PS_VERSION), $matches);
$upgrader->branch = $matches[1];
$upgrader->branch = VersionUtils::splitPrestaShopVersion($this->getProperty(self::PS_VERSION))['major'];
$upgradeConfiguration = $this->getUpgradeConfiguration();
$channel = $upgradeConfiguration->get('channel');
switch ($channel) {
Expand Down
6 changes: 3 additions & 3 deletions classes/UpgradeSelfCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,13 @@ public function getPhpCompatibilityRange(): ?array
}

if (version_compare($targetVersion, '8', '<')) {
$targetVersion = VersionUtils::getPrestashopMinorVersion($targetVersion);
$targetMinorVersion = VersionUtils::splitPrestaShopVersion($targetVersion)['minor'];

if (!isset($this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetVersion])) {
if (!isset($this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetMinorVersion])) {
return null;
}

$range = $this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetVersion];
$range = $this::PRESTASHOP_17_PHP_REQUIREMENTS[$targetMinorVersion];
} else {
try {
$range = $this->distributionApiService->getPhpVersionRequirements($targetVersion);
Expand Down
26 changes: 14 additions & 12 deletions classes/VersionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public static function isActualPHPVersionCompatible()
/**
* @param string $version
*
* @return string
* @return array{'major': string,'minor': string,'patch': string}|null
*/
public static function getPrestashopMinorVersion($version)
public static function splitPrestaShopVersion($version)
{
if (!is_string($version)) {
throw new InvalidArgumentException('Version must be a string.');
Expand All @@ -128,18 +128,20 @@ public static function getPrestashopMinorVersion($version)
throw new InvalidArgumentException('Version string cannot be empty.');
}

if (!preg_match('/^\d+\.\d+\.\d+(\.\d+)?$/', $version)) {
throw new InvalidArgumentException('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W');
}
preg_match(
'#^(?<patch>(?<minor>(?<major>([0-1]{1}\.[0-9]+)|([0-9]+))(?:\.[0-9]+){1})(?:\.[0-9]+){1})$#',
$version,
$matches
);

$parts = explode('.', $version);
$versionString = implode('.', $parts);
if (strlen($versionString) >= 8) {
$minorVersionParts = array_slice($parts, 0, 3);
} else {
$minorVersionParts = array_slice($parts, 0, 2);
if (empty($matches)) {
throw new InvalidArgumentException('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W');
}

return implode('.', $minorVersionParts);
return [
'major' => $matches['major'],
'minor' => $matches['minor'],
'patch' => $matches['patch'],
];
}
}
39 changes: 21 additions & 18 deletions tests/unit/VersionUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,54 +103,57 @@ public function testGetPhpMajorMinorVersionId()
$this->assertSame(PHP_MAJOR_VERSION * 10000 + PHP_MINOR_VERSION * 100, $version);
}

public function testGetPrestashopMinorVersion()
/**
* @dataProvider providerOfPrestaShopVersions
*/
public function testSplitOfPrestaShopVersion(string $inputVersion, array $expected)
{
$version = VersionUtils::getPrestashopMinorVersion('1.7.8.11');
$version = VersionUtils::splitPrestaShopVersion($inputVersion);

$this->assertSame('1.7.8', $version);

$version = VersionUtils::getPrestashopMinorVersion('8.1.5');

$this->assertSame('8.1', $version);

$version = VersionUtils::getPrestashopMinorVersion('8.1.5');

$this->assertSame('8.1', $version);

$version = VersionUtils::getPrestashopMinorVersion('10.1.5');
$this->assertEquals($expected, $version);
}

$this->assertSame('10.1', $version);
public function providerOfPrestaShopVersions()
{
return [
['1.6.1.12', ['major' => '1.6', 'minor' => '1.6.1', 'patch' => '1.6.1.12']],
['1.7.8.11', ['major' => '1.7', 'minor' => '1.7.8', 'patch' => '1.7.8.11']],
['8.1.5', ['major' => '8', 'minor' => '8.1', 'patch' => '8.1.5']],
['8.1.5', ['major' => '8', 'minor' => '8.1', 'patch' => '8.1.5']],
['9.0.0', ['major' => '9', 'minor' => '9.0', 'patch' => '9.0.0']],
['10.1.5', ['major' => '10', 'minor' => '10.1', 'patch' => '10.1.5']],
];
}

public function testGetPrestashopMinorVersionFailForBadType()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Version must be a string.');

VersionUtils::getPrestashopMinorVersion(1);
VersionUtils::splitPrestaShopVersion(1)['minor'];
}

public function testGetPrestashopMinorVersionFailForEmptyString()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Version string cannot be empty.');

VersionUtils::getPrestashopMinorVersion('');
VersionUtils::splitPrestaShopVersion('')['minor'];
}

public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV1()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W');

VersionUtils::getPrestashopMinorVersion('1');
VersionUtils::splitPrestaShopVersion('1')['minor'];
}

public function testGetPrestashopMinorVersionFailForIncorrectEntryFormatV2()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid version format. Expected format: X.Y.Z or X.Y.Z.W');

VersionUtils::getPrestashopMinorVersion('1.7.7.10.1');
VersionUtils::splitPrestaShopVersion('1.7.7.10.1')['minor'];
}
}

0 comments on commit 79cd041

Please sign in to comment.