mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 15:22:17 +02:00
N°9144 - setup wizard sequencers test coverage + fixes
N°9144 - ci: fix hub setup test N°9144 - code style N°9144 - tests and fixes N°9144 - ci: fix tests and code style
This commit is contained in:
@@ -0,0 +1,508 @@
|
||||
<?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 \Config&\PHPUnit\Framework\MockObject\MockObject $oConfig;
|
||||
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');
|
||||
}
|
||||
|
||||
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')
|
||||
->with($this->oConfig, '/my_backup_file_path', '/my_config_file_path', null);
|
||||
|
||||
$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')
|
||||
->with('install', $this->oConfig);
|
||||
|
||||
$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')
|
||||
->with($this->oConfig, 'install', ["a" => "b"]);
|
||||
$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')
|
||||
->with('install', $this->oConfig);
|
||||
|
||||
$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')
|
||||
->with($this->oConfig, 'install', ["a" => "b"], $aAdminParams);
|
||||
|
||||
$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')
|
||||
->with($this->oConfig, true, ["a" => "b"]);
|
||||
|
||||
$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')
|
||||
->with($this->oConfig, "6.6.6", ["a" => "b"], ["c" => "d"], null);
|
||||
|
||||
$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('GetBuildEnv')
|
||||
->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')
|
||||
->with($this->oConfig);
|
||||
}
|
||||
}
|
||||
|
||||
private function GivenConfig(): void
|
||||
{
|
||||
$this->oConfig = $this->createMock(Config::class);
|
||||
}
|
||||
|
||||
private function GivenApplicationInstallSequencer(array $aAdditionalParams = [], bool $bStepComputationOnly = false): void
|
||||
{
|
||||
$this->GivenConfig();
|
||||
$this->GivenRunTimeEnvironment($bStepComputationOnly);
|
||||
$this->oSequencer = new ApplicationInstallSequencer($this->GivenParams($aAdditionalParams), $this->oRunTimeEnvironment);
|
||||
$this->SetNonPublicProperty($this->oSequencer, 'oConfig', $this->oConfig);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user