N°931: Add Delta management in ormTagSet (with unit tests)

This commit is contained in:
Eric
2018-09-04 11:06:53 +02:00
parent 7d502fae23
commit 98e5eaa4e0
3 changed files with 9230 additions and 9092 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -52,7 +52,7 @@ final class ormTagSet
private $aAdded = array();
/**
* @var int[] Removed items
* @var DBObject[] Removed items
*/
private $aRemoved = array();
@@ -146,15 +146,17 @@ final class ormTagSet
return $aValues;
}
public function GetLabel()
/**
* @return array of tag labels indexed by code
*/
public function GetTags()
{
$aLabels = array();
$aValues = array();
$aTags = array();
foreach ($this->aPreserved as $oTag)
{
try
{
$aValues[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
} catch (CoreException $e)
{
IssueLog::Error($e->getMessage());
@@ -164,18 +166,115 @@ final class ormTagSet
{
try
{
$aValues[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
} catch (CoreException $e)
{
IssueLog::Error($e->getMessage());
}
}
ksort($aValues);
foreach($aValues as $sLabel)
{
$aLabels[] = $sLabel;
}
return $aLabels;
ksort($aTags);
return $aTags;
}
/**
* @return array of tag labels indexed by code for only the added tags
*/
public function GetAddedTags()
{
$aTags = array();
foreach ($this->aAdded as $oTag)
{
try
{
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
} catch (CoreException $e)
{
IssueLog::Error($e->getMessage());
}
}
ksort($aTags);
return $aTags;
}
/**
* @return array of tag labels indexed by code for only the removed tags
*/
public function GetRemovedTags()
{
$aTags = array();
foreach ($this->aRemoved as $oTag)
{
try
{
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
} catch (CoreException $e)
{
IssueLog::Error($e->getMessage());
}
}
ksort($aTags);
return $aTags;
}
/** Get the delta with another TagSet
*
* $aDelta['added] = array of tag labels indexed by code for only the added tags
* $aDelta['removed'] = array of tag labels indexed by code for only the removed tags
*
* @param \ormTagSet $oOtherTagSet
*
* @return array
*
* @throws \CoreException
* @throws \CoreUnexpectedValue
*/
public function GetDelta(ormTagSet $oOtherTagSet)
{
$oTag = new ormTagSet($this->sClass, $this->sAttCode);
// Set the initial value
$aOrigTagCodes = $this->GetValue();
$oTag->SetValue($aOrigTagCodes);
// now remove everything
foreach($aOrigTagCodes as $sTagCode)
{
$oTag->RemoveTag($sTagCode);
}
// now add the tags of the other TagSet
foreach($oOtherTagSet->GetValue() as $sTagCode)
{
$oTag->AddTag($sTagCode);
}
$aDelta = array();
$aDelta['added'] = $oTag->GetAddedTags();
$aDelta['removed'] = $oTag->GetRemovedTags();
return $aDelta;
}
/**
* Apply a delta to the current TagSet
*
* @param $aDelta
*
* @throws \CoreException
* @throws \CoreUnexpectedValue
*/
public function ApplyDelta($aDelta)
{
if (isset($aDelta['removed']))
{
foreach($aDelta['removed'] as $sTagCode => $aTagLabel)
{
$this->RemoveTag($sTagCode);
}
}
if (isset($aDelta['added']))
{
foreach($aDelta['added'] as $sTagCode => $aTagLabel)
{
$this->AddTag($sTagCode);
}
}
}
/**

View File

@@ -152,4 +152,43 @@ use ormTagSet;
$oTagSet->RemoveTag('tag2');
static::assertEquals($oTagSet->GetValue(), array());
}
public function testGetDelta()
{
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
$this->CreateTagData('Ticket', 'tagfield', 'tag3', 'Third');
$this->CreateTagData('Ticket', 'tagfield', 'tag4', 'Fourth');
$oTagSet1 = new ormTagSet('Ticket', 'tagfield');
$oTagSet1->SetValue(array('tag1', 'tag2'));
$oTagSet2 = new ormTagSet('Ticket', 'tagfield');
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
$aDelta = $oTagSet1->GetDelta($oTagSet2);
static::assertCount(2, $aDelta);
static::assertCount(2, $aDelta['added']);
static::assertCount(1, $aDelta['removed']);
}
public function testApplyDelta()
{
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
$this->CreateTagData('Ticket', 'tagfield', 'tag3', 'Third');
$this->CreateTagData('Ticket', 'tagfield', 'tag4', 'Fourth');
$oTagSet1 = new ormTagSet('Ticket', 'tagfield');
$oTagSet1->SetValue(array('tag1', 'tag2'));
$oTagSet2 = new ormTagSet('Ticket', 'tagfield');
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
$aDelta = $oTagSet1->GetDelta($oTagSet2);
$oTagSet1->ApplyDelta($aDelta);
static::assertTrue($oTagSet1->Equals($oTagSet2));
}
}