Refactor Forms::trash() to use Doctrine
[MAILPOET-3039]
This commit is contained in:
@ -289,14 +289,12 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function trash($data = []) {
|
public function trash($data = []) {
|
||||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
$form = $this->getForm($data);
|
||||||
$form = Form::findOne($id);
|
|
||||||
if ($form instanceof Form) {
|
if ($form instanceof FormEntity) {
|
||||||
$form->trash();
|
$this->formsRepository->trash($form);
|
||||||
$form = Form::findOne($form->id);
|
|
||||||
if(!$form instanceof Form) return $this->errorResponse();
|
|
||||||
return $this->successResponse(
|
return $this->successResponse(
|
||||||
$form->asArray(),
|
$form->toArray(),
|
||||||
['count' => 1]
|
['count' => 1]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,6 +4,7 @@ namespace MailPoet\Form;
|
|||||||
|
|
||||||
use MailPoet\Doctrine\Repository;
|
use MailPoet\Doctrine\Repository;
|
||||||
use MailPoet\Entities\FormEntity;
|
use MailPoet\Entities\FormEntity;
|
||||||
|
use MailPoetVendor\Carbon\Carbon;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends Repository<FormEntity>
|
* @extends Repository<FormEntity>
|
||||||
@ -34,4 +35,10 @@ class FormsRepository extends Repository {
|
|||||||
->getQuery()
|
->getQuery()
|
||||||
->getSingleScalarResult();
|
->getSingleScalarResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function trash(FormEntity $form) {
|
||||||
|
$form->setDeletedAt(Carbon::now());
|
||||||
|
$this->persist($form);
|
||||||
|
$this->flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,12 +212,19 @@ class FormsTest extends \MailPoetTest {
|
|||||||
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
|
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
|
||||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($response->data)->equals(
|
expect($response->data)->equals(
|
||||||
$this->reloadForm((int)$this->form2->getId())->asArray()
|
$this->form2->toArray()
|
||||||
);
|
);
|
||||||
expect($response->data['deleted_at'])->notNull();
|
expect($response->data['deleted_at'])->notNull();
|
||||||
expect($response->meta['count'])->equals(1);
|
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() {
|
public function testItCanDeleteAForm() {
|
||||||
$response = $this->endpoint->delete(['id' => $this->form3->getId()]);
|
$response = $this->endpoint->delete(['id' => $this->form3->getId()]);
|
||||||
expect($response->data)->isEmpty();
|
expect($response->data)->isEmpty();
|
||||||
|
33
tests/integration/Form/FormsRepositoryTest.php
Normal file
33
tests/integration/Form/FormsRepositoryTest.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user