diff --git a/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php b/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php index 91fbdac017..36b4190417 100644 --- a/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php +++ b/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php @@ -13,6 +13,8 @@ class DeletionPlanService { private static DeletionPlanService $oInstance; + public int $iExecutionCount = 0; + protected function __construct() { } @@ -92,6 +94,7 @@ class DeletionPlanService * @since 3.3.0 * @param array $aClasses * @param int $iUnixTimeLimit : max execution time in seconds since Epoch before stopping deletion. by default: no limit (ie remove all without stop) + * @param int $iMaxExecutionCount : max execution count before stopping deletion. by default: no limit (ie remove all without stop) * * @return array<\Combodo\iTop\DataFeatureRemoval\Entity\DeletionPlanSummaryEntity> * @throws \Combodo\iTop\DataFeatureRemoval\Helper\DataFeatureRemovalException @@ -99,7 +102,7 @@ class DeletionPlanService * @throws \CoreUnexpectedValue * @throws \MySQLException */ - public function ExecuteDeletionPlan(array $aClasses, int $iUnixTimeLimit = 0): array + public function ExecuteDeletionPlan(array $aClasses, int $iUnixTimeLimit = 0, int $iMaxExecutionCount=0): array { $oDeletionPlan = $this->GetDeletionPlan($aClasses); @@ -107,12 +110,14 @@ class DeletionPlanService throw new DataFeatureRemovalException("Deletion Plan cannot be executed due to issues"); } + $this->iExecutionCount=0; + $aSummary = []; foreach ($oDeletionPlan->ListUpdates() as $sClass => $aToUpdate) { $oDeletionPlanSummaryEntity = $aSummary[$sClass] ?? new DeletionPlanSummaryEntity($sClass); foreach ($aToUpdate as $aData) { - if ($this->IsTimeLimitExceeded($iUnixTimeLimit)) { + if ($this->IsTimeLimitExceeded($iUnixTimeLimit, $iMaxExecutionCount)) { $aSummary[$sClass] = $oDeletionPlanSummaryEntity; return $aSummary; } @@ -133,7 +138,7 @@ class DeletionPlanService $oDeletionPlanSummaryEntity = $aSummary[$sClass] ?? new DeletionPlanSummaryEntity($sClass); foreach ($aDeletes as $sId => $aDelete) { - if ($this->IsTimeLimitExceeded($iUnixTimeLimit)) { + if ($this->IsTimeLimitExceeded($iUnixTimeLimit, $iMaxExecutionCount)) { $aSummary[$sClass] = $oDeletionPlanSummaryEntity; return $aSummary; } @@ -192,8 +197,15 @@ class DeletionPlanService return $oDeletionPlan; } - public function IsTimeLimitExceeded(int $iUnixTimeLimit): bool + public function IsTimeLimitExceeded(int $iUnixTimeLimit, int $iMaxExecutionCount): bool { + throw new \Exception(""); + $this->iExecutionCount++; + echo json_encode([$this->iExecutionCount, $iMaxExecutionCount]); + if ($iMaxExecutionCount === $this->iExecutionCount){ + return false; + } + if ($iUnixTimeLimit === 0) { return false; } diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php index 3ee6bc756c..6f4c8a1a8b 100644 --- a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php @@ -277,10 +277,11 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase public function testExecuteDeletionPlan_DeleteAllWithoutLimit() { - $iDFRToRemoveLeaf = $this->GivenObjectInDB('DFRToRemoveLeaf', ['name' => 'ga']); - $this->GivenObjectInDB('DFRToUpdate', ['name' => 'bu', 'dfrtoremove_id' => $iDFRToRemoveLeaf]); - $iDFRRemovedCollateral = $this->GivenObjectInDB('DFRRemovedCollateral', ['name' => 'zo', 'dfrtoremove_id' => $iDFRToRemoveLeaf]); - $this->GivenObjectInDB('DFRRemovedCollateralCascade', ['name' => 'meu', 'dfrremovedcollateral_id' => $iDFRRemovedCollateral]); + $this->GivenDFRTreeInDB(<<ExecuteDeletionPlan($aClasses); @@ -290,7 +291,46 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase ['DFRRemovedCollateral', 0, 1 ], ['DFRRemovedCollateralCascade', 0, 1 ], ]; - $this->AssertSummaryEquals($aExpected, $aRes, 'DeleteAllWithoutLimit'); + $this->AssertSummaryEquals($aExpected, $aRes); + } + + public function testExecuteDeletionPlan_DeleteManyObjPerClassWithoutLimit() + { + $this->GivenDFRTreeInDB(<<ExecuteDeletionPlan($aClasses); + $aExpected = [ + ['DFRToUpdate', 3, 0 ], + ['DFRToRemoveLeaf', 0, 3 ], + ['DFRRemovedCollateral', 0, 3 ], + ['DFRRemovedCollateralCascade', 0, 3 ], + ]; + $this->AssertSummaryEquals($aExpected, $aRes); + } + + public function testExecuteDeletionPlan_ManualDeleteShouldFail() + { + $this->GivenDFRTreeInDB(<<expectException(DataFeatureRemovalException::class); + $this->expectExceptionMessage('Deletion Plan cannot be executed due to issues'); + DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses); } private function AssertSummaryEquals(array $expected, $actual, $sMessage = '') @@ -311,16 +351,90 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase public function testExecuteDeletionPlan_StopInUpdates() { - self::markTestSkipped(); + $this->GivenDFRTreeInDB(<<ExecuteDeletionPlan($aClasses, 0, 2); + $aExpected = [ + ['DFRToUpdate', 2, 0 ], + ['DFRToRemoveLeaf', 0, 0 ], + ['DFRRemovedCollateral', 0, 0 ], + ['DFRRemovedCollateralCascade', 0, 0 ], + ]; + $this->AssertSummaryEquals($aExpected, $aRes); } public function testExecuteDeletionPlan_StopInDeletes() { - self::markTestSkipped(); + $this->GivenDFRTreeInDB(<<ExecuteDeletionPlan($aClasses, 0, 5); + $aExpected = [ + ['DFRToUpdate', 3, 0 ], + ['DFRToRemoveLeaf', 2, 0 ], + ['DFRRemovedCollateral', 0, 0 ], + ['DFRRemovedCollateralCascade', 0, 0 ], + ]; + $this->AssertSummaryEquals($aExpected, $aRes); } public function GetDatamodelDeltaAbsPath(): string { return __DIR__.'/deletionplan_delta.xml'; } + + private function GivenDFRTreeInDB(string $sTree) + { + $aTree = explode("\n", $sTree); + foreach ($aTree as $sLine) { + if (trim($sLine) === ""){ + continue; + } + $this->GivenDFRTreeLineInDB($sLine); + } + } + + private array $aIdByObjectName = []; + private function GivenDFRTreeLineInDB(string $sLine) + { + list($sLeft, $sRight) = explode('<-', $sLine); + $sLeft=trim($sLeft); + + $iLeftId = $this->aIdByObjectName[$sLeft] ?? 0; + if ($iLeftId===0){ + list($sChildClass, ) = explode('_', $sLeft, 2); + $iLeftId = $this->GivenObjectInDB($sChildClass, ['name' => $sLeft]); + $this->aIdByObjectName[$sLeft]=$iLeftId; + } + + $sRight=trim($sRight); + list($sChildClass, ) = explode('_', $sRight, 2); + $iRightId = $this->GivenObjectInDB($sChildClass, ['name' => $sRight, 'extkey_id' => $iLeftId]); + $this->aIdByObjectName[$sRight]=$iRightId; + } } diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/deletionplan_delta.xml b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/deletionplan_delta.xml index 1a77c9e38f..a338e1fbc5 100644 --- a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/deletionplan_delta.xml +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/deletionplan_delta.xml @@ -62,8 +62,8 @@ all - - dfrtoremove_id + + extkey_id true @@ -85,7 +85,7 @@ 10 - + 20 @@ -114,8 +114,8 @@ all - - dfrtoremove_id + + extkey_id false @@ -137,7 +137,7 @@ 10 - + 20 @@ -209,8 +209,8 @@ all - - dfrremovedcollateral_id + + extkey_id false @@ -232,7 +232,7 @@ 10 - + 20 @@ -261,8 +261,8 @@ all - - dfrtoremove_id + + extkey_id false @@ -284,7 +284,7 @@ 10 - + 20 @@ -308,16 +308,16 @@ - - + + - - + + @@ -330,16 +330,16 @@ - - + + - - + +