Refactor code that checks for list deletion with form

Move segment deletion to new function doTrash to avoid calling methods twice on individual segment deletion. Refactor acceptance test.

[MAILPOET-3661]
This commit is contained in:
Brezo Cordero
2021-07-30 17:32:41 -05:00
committed by Veljko V
parent 711830d44d
commit b2857cbf40
3 changed files with 21 additions and 27 deletions

View File

@ -209,7 +209,7 @@ class Segments extends APIEndpoint {
$this->subscribersRepository->bulkTrash($subscriberIds);
}
$this->segmentsRepository->bulkTrash([$segment->getId()], $segment->getType());
$this->segmentsRepository->doTrash([$segment->getId()], $segment->getType());
$this->segmentsRepository->refresh($segment);
return $this->successResponse(
$this->segmentsResponseBuilder->build($segment),

View File

@ -184,6 +184,10 @@ class SegmentsRepository extends Repository {
return $this->updateDeletedAt($ids, new Carbon(), $type);
}
public function doTrash(array $ids, string $type = SegmentEntity::TYPE_DEFAULT): int {
return $this->updateDeletedAt($ids, new Carbon(), $type);
}
public function bulkRestore(array $ids, string $type = SegmentEntity::TYPE_DEFAULT): int {
return $this->updateDeletedAt($ids, null, $type);
}

View File

@ -164,12 +164,8 @@ class SegmentsTest extends \MailPoetTest {
}
public function testItReturnsErrorWhenTrashingSegmentWithActiveForm() {
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => [(string)$this->segment3->getId()],
]);
$this->entityManager->persist($form);
$this->entityManager->flush();
$settings = ['segments' => [(string)$this->segment3->getId()]];
$this->createForm('My Form', $settings);
$response = $this->endpoint->trash(['id' => $this->segment3->getId()]);
$this->entityManager->refresh($this->segment3);
@ -178,19 +174,9 @@ class SegmentsTest extends \MailPoetTest {
}
public function testItReturnsPluralErrorWhenTrashingSegmentWithActiveForms() {
$form1 = new FormEntity('My Form');
$form1->setSettings([
'segments' => [(string)$this->segment3->getId()],
]);
$this->entityManager->persist($form1);
$this->entityManager->flush();
$form2 = new FormEntity('My other Form');
$form2->setSettings([
'segments' => [(string)$this->segment3->getId()],
]);
$this->entityManager->persist($form2);
$this->entityManager->flush();
$settings = ['segments' => [(string)$this->segment3->getId()]];
$this->createForm('My Form', $settings);
$this->createForm('My other Form', $settings);
$response = $this->endpoint->trash(['id' => $this->segment3->getId()]);
$this->entityManager->refresh($this->segment3);
@ -199,15 +185,11 @@ class SegmentsTest extends \MailPoetTest {
}
public function testItCanTrashSegmentWithoutActiveForm() {
$form1 = new FormEntity('My Form');
$form1->setSettings([
'segments' => [(string)$this->segment3->getId()],
]);
$this->entityManager->persist($form1);
$this->entityManager->flush();
$settings = ['segments' => [(string)$this->segment3->getId()]];
$this->createForm('My Form', $settings);
$response = $this->endpoint->trash(['id' => $this->segment2->getId()]);
$this->entityManager->clear();
$this->entityManager->refresh($this->segment2);
$segment = $this->segmentRepository->findOneById($this->segment2->getId());
assert($segment instanceof SegmentEntity);
@ -268,6 +250,14 @@ class SegmentsTest extends \MailPoetTest {
expect($subsribers)->count(0);
}
private function createForm(string $formName, array $settings ) {
$form = new FormEntity($formName);
$form->setSettings($settings);
$this->entityManager->persist($form);
$this->entityManager->flush();
return $form;
}
private function createSubscriberSegment(SubscriberEntity $subscriber, SegmentEntity $segment): SubscriberSegmentEntity {
$subscriberSegment = new SubscriberSegmentEntity($segment, $subscriber, SubscriberEntity::STATUS_SUBSCRIBED);
$this->entityManager->persist($subscriberSegment);