fix social action consistency

This commit is contained in:
2022-04-26 21:12:31 +02:00
parent db6c4f15f8
commit b2fb86111d
3 changed files with 93 additions and 31 deletions

View File

@@ -230,45 +230,39 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac
public function addSocialAction(SocialAction $socialAction): self
{
$descendants = $socialAction->getDescendantsWithThis();
$parent = $socialAction->getParent();
$parentKey = array_search($parent, $this->socialActions->toArray());
if (false !== $parentKey) {
$this->socialActions[$parentKey] = $socialAction;
// return $this;
if (!$this->socialActions->contains($socialAction)) {
$this->socialActions[] = $socialAction;
$this->ensureSocialActionConsistency();
}
if (count($descendants) > 0) {
foreach ($descendants as $d) {
$inCollection = $this->socialActions->contains($d);
if ($inCollection) {
return $this;
}
}
}
$this->socialActions[] = $socialAction;
return $this;
}
private function ensureSocialActionConsistency(): void
{
$ancestors = SocialAction::findAncestorSocialActions($this->getSocialActions());
foreach ($ancestors as $ancestor) {
$this->removeSocialAction($ancestor);
}
}
/**
* Add a social issue
*
* Note: the social issue consistency (the fact that only yougest social issues
* are kept) is processed by an entity listener:
* @see{\Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodSocialIssueConsistencyEntityListener}
*
* @param SocialIssue $socialIssue
* @return $this
*/
public function addSocialIssue(SocialIssue $socialIssue): self
{
$descendants = $socialIssue->getDescendantsWithThis();
if (count($descendants) > 0) {
foreach ($descendants as $d) {
$inCollection = $this->socialIssues->contains($d);
if ($inCollection) {
return $this;
}
}
if (!$this->socialIssues->contains($socialIssue)) {
$this->socialIssues[] = $socialIssue;
}
$this->socialIssues[] = $socialIssue;
return $this;
}

View File

@@ -12,12 +12,17 @@ declare(strict_types=1);
namespace Chill\ActivityBundle\Tests\Entity;
use Chill\ActivityBundle\Entity\Activity;
use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodSocialIssueConsistencyEntityListener;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Doctrine\ORM\Event\LifecycleEventArgs;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
final class ActivityTest extends TestCase
{
use ProphecyTrait;
public function testHierarchySocialActions(): void
{
@@ -59,6 +64,9 @@ final class ActivityTest extends TestCase
public function testHierarchySocialIssues(): void
{
$listener = new AccompanyingPeriodSocialIssueConsistencyEntityListener();
$event = $this->prophesize(LifecycleEventArgs::class)->reveal();
$parent = new SocialIssue();
$child = new SocialIssue();
@@ -67,19 +75,23 @@ final class ActivityTest extends TestCase
$child->addChild($grandChild);
$activity = new Activity();
$activity->setAccompanyingPeriod(new AccompanyingPeriod());
$activity->addSocialIssue($parent);
$listener->preUpdate($activity, $event);
$this->assertCount(1, $activity->getSocialIssues());
$this->assertContains($parent, $activity->getSocialIssues());
$activity->addSocialIssue($grandChild);
$listener->preUpdate($activity, $event);
$this->assertCount(1, $activity->getSocialIssues());
$this->assertContains($grandChild, $activity->getSocialIssues());
$this->assertNotContains($parent, $activity->getSocialIssues());
$activity->addSocialIssue($child);
$listener->preUpdate($activity, $event);
$this->assertCount(1, $activity->getSocialIssues());
$this->assertContains($grandChild, $activity->getSocialIssues());
@@ -87,6 +99,7 @@ final class ActivityTest extends TestCase
$this->assertNotContains($child, $activity->getSocialIssues());
$activity->addSocialIssue($another = new SocialIssue());
$listener->preUpdate($activity, $event);
$this->assertCount(2, $activity->getSocialIssues());
$this->assertContains($grandChild, $activity->getSocialIssues());
@@ -95,4 +108,4 @@ final class ActivityTest extends TestCase
$this->assertNotContains($child, $activity->getSocialIssues());
}
}
}