Refactor Forms::trash() to use Doctrine

[MAILPOET-3039]
This commit is contained in:
Rodrigo Primo
2021-04-01 15:03:56 -03:00
committed by Veljko V
parent 86fa574ce8
commit e054f05222
4 changed files with 53 additions and 8 deletions

View File

@ -289,14 +289,12 @@ class Forms extends APIEndpoint {
}
public function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
$form->trash();
$form = Form::findOne($form->id);
if(!$form instanceof Form) return $this->errorResponse();
$form = $this->getForm($data);
if ($form instanceof FormEntity) {
$this->formsRepository->trash($form);
return $this->successResponse(
$form->asArray(),
$form->toArray(),
['count' => 1]
);
} else {

View File

@ -4,6 +4,7 @@ namespace MailPoet\Form;
use MailPoet\Doctrine\Repository;
use MailPoet\Entities\FormEntity;
use MailPoetVendor\Carbon\Carbon;
/**
* @extends Repository<FormEntity>
@ -34,4 +35,10 @@ class FormsRepository extends Repository {
->getQuery()
->getSingleScalarResult();
}
public function trash(FormEntity $form) {
$form->setDeletedAt(Carbon::now());
$this->persist($form);
$this->flush();
}
}

View File

@ -212,12 +212,19 @@ class FormsTest extends \MailPoetTest {
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->equals(
$this->reloadForm((int)$this->form2->getId())->asArray()
$this->form2->toArray()
);
expect($response->data['deleted_at'])->notNull();
expect($response->meta['count'])->equals(1);
}
public function testErrorWhenTrashingNonExistentForm() {
$response = $this->endpoint->trash(['id' => 'Invalid ID']);
expect($response->errors[0]['error'])->equals('not_found');
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->meta)->isEmpty();
}
public function testItCanDeleteAForm() {
$response = $this->endpoint->delete(['id' => $this->form3->getId()]);
expect($response->data)->isEmpty();

View File

@ -0,0 +1,33 @@
<?php
namespace MailPoet\Form;
use MailPoet\Entities\FormEntity;
class FormsRepositoryTest extends \MailPoetTest {
/** @var FormsRepository */
private $repository;
public function _before() {
parent::_before();
$this->repository = $this->diContainer->get(FormsRepository::class);
}
public function testItCanTrashForm() {
$form = $this->createForm('Form 1');
expect($form->getDeletedAt())->null();
$this->repository->trash($form);
expect($form->getDeletedAt())->notNull();
}
public function _after() {
$this->truncateEntity(FormEntity::class);
}
private function createForm(string $name): FormEntity {
$form = new FormEntity($name);
$this->repository->persist($form);
$this->repository->flush();
return $form;
}
}