Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Jul 19, 2024
1 parent c96da96 commit 544d067
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 4 deletions.
15 changes: 11 additions & 4 deletions classes/Progress/CompletionCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace PrestaShop\Module\AutoUpgrade\Progress;

use InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreDb;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
Expand Down Expand Up @@ -83,17 +84,23 @@ private static function getPercentages(): array

/**
* @return int<0, 100>
*
* @throws InvalidArgumentException
*/
public function getBasePercentageOfTask(string $taskName): int
{
$percentages = self::getPercentages();
if (!isset($percentages[$taskName])) {
throw new InvalidArgumentException($taskName . ' has no percentage. Make sure to send an upgrade, backup or restore task.');
}

$withoutBackup = !$this->upgradeConfiguration->shouldBackupFiles();
$percentages = self::getPercentages()[$taskName];

if ($withoutBackup && $percentages['baseWithoutBackup'] !== null) {
return $percentages['baseWithoutBackup'];
if ($withoutBackup && isset($percentages[$taskName]['baseWithoutBackup'])) {
return $percentages[$taskName]['baseWithoutBackup'];
}

return $percentages['base'];
return $percentages[$taskName]['base'];
}

/**
Expand Down
File renamed without changes.
115 changes: 115 additions & 0 deletions tests/unit/Progress/CompletionCalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Progress\Backlog;
use PrestaShop\Module\AutoUpgrade\Progress\CompletionCalculator;
use PrestaShop\Module\AutoUpgrade\Task\Rollback\RestoreFiles;
use PrestaShop\Module\AutoUpgrade\Task\Runner\SingleTask;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeDb;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeFiles;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeModules;
use PrestaShop\Module\AutoUpgrade\Task\Upgrade\UpgradeNow;

class CompletionCalculatorTest extends TestCase
{
public function testRetrievalOfBasePercentages()
{
$completionCalculator = $this->getCompletionCalculator(true);

$this->assertSame(0, $completionCalculator->getBasePercentageOfTask(UpgradeNow::class));
$this->assertSame(90, $completionCalculator->getBasePercentageOfTask(UpgradeModules::class));
$this->assertSame(33, $completionCalculator->getBasePercentageOfTask(RestoreFiles::class));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(SingleTask::class . ' has no percentage. Make sure to send an upgrade, backup or restore task.');
$completionCalculator->getBasePercentageOfTask(SingleTask::class);
}

public function testRetrievalOfBasePercentagesWithoutBackup()
{
$completionCalculator = $this->getCompletionCalculator(false);

$this->assertSame(0, $completionCalculator->getBasePercentageOfTask(UpgradeNow::class));
$this->assertSame(80, $completionCalculator->getBasePercentageOfTask(UpgradeModules::class));
$this->assertSame(33, $completionCalculator->getBasePercentageOfTask(RestoreFiles::class));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(SingleTask::class . ' has no percentage. Make sure to send an upgrade, backup or restore task.');
$completionCalculator->getBasePercentageOfTask(SingleTask::class);
}

public function testComputationOfPercentages()
{
$completionCalculator = $this->getCompletionCalculator(false);

$backlog = new Backlog(['stuff', 'stuff', 'stuff'], 3);

$this->assertSame(
40,
$completionCalculator->computePercentage($backlog, UpgradeFiles::class, UpgradeDb::class)
);

$backlog->getNext();

$this->assertSame(
46,
$completionCalculator->computePercentage($backlog, UpgradeFiles::class, UpgradeDb::class)
);

$backlog->getNext();

$this->assertSame(
53,
$completionCalculator->computePercentage($backlog, UpgradeFiles::class, UpgradeDb::class)
);

$backlog->getNext();

$this->assertSame(
60,
$completionCalculator->computePercentage($backlog, UpgradeFiles::class, UpgradeDb::class)
);
}

private function getCompletionCalculator(bool $withBackup): CompletionCalculator
{
return new CompletionCalculator(
new UpgradeConfiguration([
'PS_AUTOUP_PERFORMANCE' => 5,
'PS_AUTOUP_CUSTOM_MOD_DESACT' => 0,
'PS_AUTOUP_UPDATE_DEFAULT_THEME' => 1,
'PS_AUTOUP_CHANGE_DEFAULT_THEME' => 1,
'PS_AUTOUP_KEEP_MAILS' => 0,
'PS_AUTOUP_BACKUP' => $withBackup,
'skip_backup' => !$withBackup,
'PS_AUTOUP_KEEP_IMAGES' => 0,
'channel' => 'major',
'archive.filename' => 'zip.zip',
])
);
}
}

0 comments on commit 544d067

Please sign in to comment.