create api for social issue consistency

This commit is contained in:
2021-08-21 23:18:55 +02:00
parent 2a3f869882
commit 09e5cc1545
6 changed files with 341 additions and 1 deletions

View File

@@ -0,0 +1,140 @@
<?php
namespace Chill\PersonBundle\Tests\AccompanyingPeriod\SocialIssueConsistency;
use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodLinkedWithSocialIssuesEntityInterface;
use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodSocialIssueConsistencyEntityListener;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use PHPUnit\Framework\TestCase;
class AccompanyingPeriodSocialIssueConsistencyEntityListenerTest extends TestCase
{
public function testPrePersistAccompanyingPeriod()
{
$arraySocialIssues = [
$parent = new SocialIssue(),
$child = (new SocialIssue())->setParent($parent),
$grandChild = (new SocialIssue())->setParent($child),
$grandGrandChild = (new SocialIssue())->setParent($grandChild),
];
$period = new AccompanyingPeriod();
$consistency = new AccompanyingPeriodSocialIssueConsistencyEntityListener();
foreach ($arraySocialIssues as $issue) {
$period->addSocialIssue($issue);
}
$consistency->prePersistAccompanyingPeriod($period, $this->generateLifecycleArgs());
$this->assertCount(1, $period->getSocialIssues());
$this->assertSame($grandGrandChild, $period->getSocialIssues()->first());
}
public function testPreUpdate()
{
$socialIssues = new ArrayCollection([
$parent = new SocialIssue(),
$child = (new SocialIssue())->setParent($parent),
$grandChild = (new SocialIssue())->setParent($child),
$grandGrandChild = (new SocialIssue())->setParent($grandChild),
]);
$period = (new AccompanyingPeriod())->addSocialIssue($unrelated = new SocialIssue());
$entity = $this->generateClass($period, $socialIssues);
$consistency = new AccompanyingPeriodSocialIssueConsistencyEntityListener();
$consistency->preUpdate($entity, $this->generateLifecycleArgs());
$this->assertCount(2, $period->getSocialIssues());
$this->assertContains($grandGrandChild, $period->getSocialIssues());
$this->assertContains($unrelated, $period->getSocialIssues());
$this->assertCount(1, $entity->getSocialIssues());
$this->assertContains($grandGrandChild, $entity->getSocialIssues());
}
public function testPrePersist()
{
$socialIssues = new ArrayCollection([
$parent = new SocialIssue(),
$child = (new SocialIssue())->setParent($parent),
$grandChild = (new SocialIssue())->setParent($child),
$grandGrandChild = (new SocialIssue())->setParent($grandChild),
]);
$period = (new AccompanyingPeriod())->addSocialIssue($unrelated = new SocialIssue());
$entity = $this->generateClass($period, $socialIssues);
$consistency = new AccompanyingPeriodSocialIssueConsistencyEntityListener();
$consistency->prePersist($entity, $this->generateLifecycleArgs());
$this->assertCount(2, $period->getSocialIssues());
$this->assertContains($grandGrandChild, $period->getSocialIssues());
$this->assertContains($unrelated, $period->getSocialIssues());
$this->assertCount(1, $entity->getSocialIssues());
$this->assertContains($grandGrandChild, $entity->getSocialIssues());
}
public function testPreUpdateAccompanyingPeriod()
{
$arraySocialIssues = [
$parent = new SocialIssue(),
$child = (new SocialIssue())->setParent($parent),
$grandChild = (new SocialIssue())->setParent($child),
$grandGrandChild = (new SocialIssue())->setParent($grandChild),
];
$period = new AccompanyingPeriod();
$consistency = new AccompanyingPeriodSocialIssueConsistencyEntityListener();
foreach ($arraySocialIssues as $issue) {
$period->addSocialIssue($issue);
}
$consistency->prePersistAccompanyingPeriod($period, $this->generateLifecycleArgs());
$this->assertCount(1, $period->getSocialIssues());
$this->assertSame($grandGrandChild, $period->getSocialIssues()->first());
}
protected function generateLifecycleArgs(): LifecycleEventArgs
{
return $this->createMock(LifecycleEventArgs::class);
}
protected function generateClass(AccompanyingPeriod $period, Collection $socialIssues): AccompanyingPeriodLinkedWithSocialIssuesEntityInterface
{
return new class($period, $socialIssues) implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterface
{
public Collection $socialIssues;
public AccompanyingPeriod $period;
public function __construct($period, $socialIssues)
{
$this->period = $period;
$this->socialIssues = $socialIssues;
}
public function getAccompanyingPeriod(): AccompanyingPeriod
{
return $this->period;
}
public function getSocialIssues(): Collection
{
return $this->socialIssues;
}
public function removeSocialIssue(SocialIssue $issue): AccompanyingPeriodLinkedWithSocialIssuesEntityInterface
{
$this->socialIssues->removeElement($issue);
return $this;
}
};
}
}