Files
iTop/tests/php-unit-tests/unitary-tests/setup/sequencers/ApplicationInstallerSequencerTest.php
2026-04-17 10:41:54 +02:00

497 lines
13 KiB
PHP

<?php
namespace Combodo\iTop\Test\UnitTest\Setup\Sequencers;
use ApplicationInstallSequencer;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Config;
use PHPParameters;
use RunTimeEnvironment;
class ApplicationInstallerSequencerTest extends ItopTestCase
{
private \RunTimeEnvironment&\PHPUnit\Framework\MockObject\MockObject $oRunTimeEnvironment;
private ApplicationInstallSequencer $oSequencer;
protected function setUp(): void
{
static::LoadRequiredItopFiles();
parent::setUp();
$this->RequireOnceItopFile('/setup/sequencers/ApplicationInstallSequencer.php');
$this->RequireOnceItopFile('/setup/sequencers/DataAuditSequencer.php');
$this->RequireOnceItopFile('/setup/parameters.class.inc.php');
$this->RequireOnceItopFile('/setup/setuputils.class.inc.php');
$this->RequireOnceItopFile('/setup/runtimeenv.class.inc.php');
// Copy conf to production-test
\SetupUtils::copydir(APPROOT.'conf/production', APPROOT.'conf/production-test');
}
public static function FirstStepProvider()
{
return [
'next is db-update' => [
'next-step' => 'db-schema',
'next-step-label' => 'Updating database schema',
'percentage-completed' => 16,
'optional_steps' => [],
],
'next is log-parameters' => [
'next-step' => 'log-parameters',
'next-step-label' => 'Log parameters',
'percentage-completed' => 11,
'optional_steps' => [
'log-parameters' => true,
'backup' => true,
'migrate-before' => true,
],
],
'next is backup' => [
'next-step' => 'backup',
'next-step-label' => 'Performing a backup of the database',
'percentage-completed' => 12,
'optional_steps' => [
'backup' => true,
'migrate-before' => true,
],
],
'next is migrate-before' => [
'next-step' => 'migrate-before',
'next-step-label' => 'Migrate data before database upgrade',
'percentage-completed' => 14,
'optional_steps' => [
'migrate-before' => true,
],
],
];
}
/**
* @dataProvider FirstStepProvider
*/
public function testFirstStep($sNextStep, $sNextLabel, $iPercent, $aOptionalSteps)
{
$aAdditionalParams = [
'optional_steps' => $aOptionalSteps,
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$aRes = $this->oSequencer->ExecuteStep();
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => $sNextStep,
'next-step-label' => $sNextLabel,
'percentage-completed' => $iPercent,
];
$this->assertEquals($aExpected, $aRes);
}
public function testLogStep()
{
$this->GivenApplicationInstallSequencer();
$aRes = $this->oSequencer->ExecuteStep('log-parameters');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => 'db-schema',
'next-step-label' => 'Updating database schema',
'percentage-completed' => 16,
];
$this->assertEquals($aExpected, $aRes);
}
public static function BackupStepProvider()
{
return [
'next is db-update' => [
'next-step' => 'db-schema',
'next-step-label' => 'Updating database schema',
'percentage-completed' => 28,
'optional_steps' => [
'backup' => true,
],
],
'next is migrate-before' => [
'next-step' => 'migrate-before',
'next-step-label' => 'Migrate data before database upgrade',
'percentage-completed' => 25,
'optional_steps' => [
'backup' => true,
'migrate-before' => true,
],
],
];
}
/**
* @dataProvider BackupStepProvider
*/
public function testBackup($sNextStep, $sNextLabel, $iPercent, $aOptionalSteps)
{
$aAdditionalParams = [
'optional_steps' => $aOptionalSteps,
];
$aAdditionalParams['optional_steps']['backup'] = [
'destination' => '/my_backup_file_path',
'configuration_file' => '/my_config_file_path',
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('Backup');
$aRes = $this->oSequencer->ExecuteStep('backup');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => $sNextStep,
'next-step-label' => $sNextLabel,
'percentage-completed' => $iPercent,
];
$this->assertEquals($aExpected, $aRes);
}
public function testMigrateBefore()
{
$aAdditionalParams = [
'optional_steps' => [
'migrate-before' => true,
],
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('MigrateDataBeforeUpdateStructure');
$aRes = $this->oSequencer->ExecuteStep('migrate-before');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => 'db-schema',
'next-step-label' => 'Updating database schema',
'percentage-completed' => 28,
];
$this->assertEquals($aExpected, $aRes);
}
public static function DbUpdateStepProvider()
{
return [
'next is migrate-after' => [
'next-step' => 'migrate-after',
'next-step-label' => 'Migrate data after database upgrade',
'percentage-completed' => 28,
'optional_steps' => [
'migrate-after' => true,
],
],
'next is after-db-create' => [
'next-step' => 'after-db-create',
'next-step-label' => 'Load data after database create',
'percentage-completed' => 33,
'optional_steps' => [],
],
];
}
/**
* @dataProvider DbUpdateStepProvider
*/
public function testDbUpdate($sNextStep, $sNextLabel, $iPercent, $aOptionalSteps)
{
$aAdditionalParams = [
'selected_modules' => ["a" => "b"],
'optional_steps' => $aOptionalSteps,
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('UpdateDBSchema');
$this->oRunTimeEnvironment->expects($this->once())->method('SetDbUUID')
->with();
$aRes = $this->oSequencer->ExecuteStep('db-schema');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => $sNextStep,
'next-step-label' => $sNextLabel,
'percentage-completed' => $iPercent,
];
$this->assertEquals($aExpected, $aRes);
}
public function testMigrateAfter()
{
$aAdditionalParams = [
'optional_steps' => [
'migrate-after' => true,
],
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('MigrateDataAfterUpdateStructure');
$aRes = $this->oSequencer->ExecuteStep('migrate-after');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => 'after-db-create',
'next-step-label' => 'Load data after database create',
'percentage-completed' => 42,
];
$this->assertEquals($aExpected, $aRes);
}
public function testAfterDbCreate()
{
$aAdminParams = [
'user' => "ga",
'pwd' => "zo",
'language' => "meu",
];
$aAdditionalParams = [
'selected_modules' => ["a" => "b"],
'admin_account' => $aAdminParams,
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('AfterDBCreate');
$aRes = $this->oSequencer->ExecuteStep('after-db-create');
$aExpected = [
'status' => 1,
'message' => '',
'next-step' => 'load-data',
'next-step-label' => 'Loading data',
'percentage-completed' => 50,
];
$this->assertEquals($aExpected, $aRes);
}
public function testLoadData()
{
$aAdditionalParams = [
'selected_modules' => ["a" => "b"],
'sample_data' => 1,
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('DoLoadData');
$aRes = $this->oSequencer->ExecuteStep('load-data');
$aExpected = [
'message' => 'All data loaded',
'next-step' => 'create-config',
'next-step-label' => 'Creating the configuration File',
'percentage-completed' => 66,
'status' => 1,
];
$this->assertEquals($aExpected, $aRes);
}
public function testCreateConfig()
{
$aAdditionalParams = [
'datamodel_version' => "6.6.6",
'selected_extensions' => ["c" => "d"],
'selected_modules' => ["a" => "b"],
'sample_data' => 1,
];
$this->GivenApplicationInstallSequencer($aAdditionalParams);
$this->oRunTimeEnvironment->expects($this->once())->method('DoCreateConfig');
$aRes = $this->oSequencer->ExecuteStep('create-config');
$aExpected = [
'message' => '',
'next-step' => 'commit',
'next-step-label' => 'Finalize',
'percentage-completed' => 83,
'status' => 1,
];
$this->assertEquals($aExpected, $aRes);
}
public function testCommit()
{
$this->GivenApplicationInstallSequencer();
$this->oRunTimeEnvironment->expects($this->once())->method('Commit');
$this->oRunTimeEnvironment->expects($this->once())->method('ExitReadOnlyMode');
$aRes = $this->oSequencer->ExecuteStep('commit');
$aExpected = [
'message' => '',
'next-step' => '',
'next-step-label' => 'Completed',
'percentage-completed' => 100,
'status' => 1,
];
$this->assertEquals($aExpected, $aRes);
}
public function testAnyFailure()
{
$this->GivenApplicationInstallSequencer();
$this->oRunTimeEnvironment->expects($this->once())->method('UpdateDBSchema')
->willThrowException(new \CoreException('SHADOK MSG'));
$aRes = $this->oSequencer->ExecuteStep('db-schema');
$aExpected = [
'status' => 2,
'message' => 'SHADOK MSG',
'next-step' => '',
'next-step-label' => '',
'percentage-completed' => 100,
'error_code' => 0,
];
$this->assertEquals($aExpected, $aRes);
}
public function testUnknownStep()
{
$this->GivenApplicationInstallSequencer();
$aRes = $this->oSequencer->ExecuteStep('gabuzomeu');
$aExpected = [
'status' => 2,
'message' => '',
'next-step' => '',
'next-step-label' => 'Unknown setup step \'gabuzomeu\'.',
'percentage-completed' => 100,
];
$this->assertEquals($aExpected, $aRes);
}
private function GivenParams(array $aAdditionalParams = []): PHPParameters
{
$oParams = new PHPParameters();
$aParams = array_merge_recursive([
'mode' => 'install',
'database' => [
'server' => 'server',
'user' => 'user',
'pwd' => 'pwd',
'name' => 'name',
'prefix' => 'prefix',
'db_tls_enabled' => 'db_tls_enabled',
'db_tls_ca' => 'db_tls_ca',
],
'application_path' => '',
'language' => '',
'graphviz_path' => '',
'source_dir' => '',
], $aAdditionalParams);
$oParams->LoadFromHash($aParams);
return $oParams;
}
public function testGetStepNamesWithAllSteps()
{
$aAdditionalParams = [
'optional_steps' => [
'log-parameters' => true,
'backup' => true,
'migrate-before' => true,
'migrate-after' => true,
],
];
$this->GivenApplicationInstallSequencer($aAdditionalParams, true);
$expected = [
'',
'log-parameters',
'backup',
'migrate-before',
'db-schema',
'migrate-after',
'after-db-create',
'load-data',
'create-config',
'commit',
];
$this->assertEquals($expected, $this->oSequencer->GetStepNames());
}
public static function WithoutOneStepProvider()
{
return [
['log-parameters'],
['backup'],
['migrate-before'],
['migrate-after'],
];
}
/**
* @dataProvider WithoutOneStepProvider
*/
public function testAllWithoutOneStep($sMissingStepName)
{
$aAdditionalParams = [
'optional_steps' => [
'log-parameters' => true,
'backup' => true,
'migrate-before' => true,
'migrate-after' => true,
],
];
unset($aAdditionalParams['optional_steps'][$sMissingStepName]);
$this->GivenApplicationInstallSequencer($aAdditionalParams, true);
$expected = [
'' => true,
'log-parameters' => true,
'backup' => true,
'migrate-before' => true,
'db-schema' => true,
'migrate-after' => true,
'after-db-create' => true,
'load-data' => true,
'create-config' => true,
'commit' => true,
];
unset($expected[$sMissingStepName]);
$this->assertEquals(array_keys($expected), $this->oSequencer->GetStepNames());
}
public function testGetStepNamesWithOnlyMandatorySteps()
{
$this->GivenApplicationInstallSequencer([], true);
$expected = [
'',
'db-schema',
'after-db-create',
'load-data',
'create-config',
'commit',
];
$this->assertEquals($expected, $this->oSequencer->GetStepNames());
}
public function testGetStepAfterWithPercent()
{
$this->GivenApplicationInstallSequencer([], true);
$this->assertEquals(['db-schema', 16], $this->oSequencer->GetStepAfterWithPercent(''));
$this->assertEquals(['after-db-create', 33], $this->oSequencer->GetStepAfterWithPercent('db-schema'));
$this->assertEquals(['load-data', 50], $this->oSequencer->GetStepAfterWithPercent('after-db-create'));
$this->assertEquals(['create-config', 66], $this->oSequencer->GetStepAfterWithPercent('load-data'));
$this->assertEquals(['commit', 83], $this->oSequencer->GetStepAfterWithPercent('create-config'));
$this->assertEquals(['', 100], $this->oSequencer->GetStepAfterWithPercent('commit'));
}
private function GivenRunTimeEnvironment(bool $bStepComputationOnly = false): void
{
$this->oRunTimeEnvironment = $this->createMock(RunTimeEnvironment::class);
if (! $bStepComputationOnly) {
$this->oRunTimeEnvironment->expects($this->once())->method('EnterReadOnlyMode');
$this->oRunTimeEnvironment->expects($this->any())->method('GetBuildEnv')->willReturn('production-test');
}
}
private function GivenApplicationInstallSequencer(array $aAdditionalParams = [], bool $bStepComputationOnly = false): void
{
$this->GivenRunTimeEnvironment($bStepComputationOnly);
$this->oSequencer = new ApplicationInstallSequencer($this->GivenParams($aAdditionalParams), $this->oRunTimeEnvironment);
}
}