Updates parts of FormsTest to use Doctrine entities

This commit simply updates parts FormsTest to use Doctrine entities
instead of Paris objects.

[MAILPOET-3036]
This commit is contained in:
Rodrigo Primo
2021-03-20 12:46:09 -03:00
committed by Veljko V
parent 4546b5be68
commit 298b8730fe

View File

@ -6,6 +6,8 @@ use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\v1\Forms; use MailPoet\API\JSON\v1\Forms;
use MailPoet\DI\ContainerWrapper; use MailPoet\DI\ContainerWrapper;
use MailPoet\Entities\FormEntity; use MailPoet\Entities\FormEntity;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Form\PreviewPage; use MailPoet\Form\PreviewPage;
use MailPoet\Models\Form; use MailPoet\Models\Form;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
@ -19,16 +21,20 @@ class FormsTest extends \MailPoetTest {
/** @var Forms */ /** @var Forms */
private $endpoint; private $endpoint;
/** @var FormsRepository */
private $formsRepository;
/** @var WPFunctions */ /** @var WPFunctions */
private $wp; private $wp;
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->endpoint = ContainerWrapper::getInstance()->get(Forms::class); $this->endpoint = ContainerWrapper::getInstance()->get(Forms::class);
$this->formsRepository = ContainerWrapper::getInstance()->get(FormsRepository::class);
$this->wp = WPFunctions::get(); $this->wp = WPFunctions::get();
$this->form1 = Form::createOrUpdate(['name' => 'Form 1']); $this->form1 = $this->createForm('Form 1');
$this->form2 = Form::createOrUpdate(['name' => 'Form 2']); $this->form2 = $this->createForm('Form 2');
$this->form3 = Form::createOrUpdate(['name' => 'Form 3']); $this->form3 = $this->createForm('Form 3');
Segment::createOrUpdate(['name' => 'Segment 1']); Segment::createOrUpdate(['name' => 'Segment 1']);
Segment::createOrUpdate(['name' => 'Segment 2']); Segment::createOrUpdate(['name' => 'Segment 2']);
} }
@ -42,10 +48,10 @@ class FormsTest extends \MailPoetTest {
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND); expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->equals('This form does not exist.'); expect($response->errors[0]['message'])->equals('This form does not exist.');
$response = $this->endpoint->get(['id' => $this->form1->id]); $response = $this->endpoint->get(['id' => $this->form1->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->form1->id)->asArray() $this->reloadForm((int)$this->form1->getId())->asArray()
); );
} }
@ -186,40 +192,41 @@ class FormsTest extends \MailPoetTest {
} }
public function testItCanRestoreAForm() { public function testItCanRestoreAForm() {
$this->form1->trash(); $this->form1->setDeletedAt(new \DateTime());
$this->formsRepository->flush();
$trashedForm = Form::findOne($this->form1->id); $trashedForm = Form::findOne($this->form1->getId());
assert($trashedForm instanceof Form); assert($trashedForm instanceof Form);
expect($trashedForm->deletedAt)->notNull(); expect($trashedForm->deletedAt)->notNull();
$response = $this->endpoint->restore(['id' => $this->form1->id]); $response = $this->endpoint->restore(['id' => $this->form1->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->form1->id)->asArray() $this->reloadForm((int)$this->form1->getId())->asArray()
); );
expect($response->data['deleted_at'])->null(); expect($response->data['deleted_at'])->null();
expect($response->meta['count'])->equals(1); expect($response->meta['count'])->equals(1);
} }
public function testItCanTrashAForm() { public function testItCanTrashAForm() {
$response = $this->endpoint->trash(['id' => $this->form2->id]); $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->id)->asArray() $this->reloadForm((int)$this->form2->getId())->asArray()
); );
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 testItCanDeleteAForm() { public function testItCanDeleteAForm() {
$response = $this->endpoint->delete(['id' => $this->form3->id]); $response = $this->endpoint->delete(['id' => $this->form3->getId()]);
expect($response->data)->isEmpty(); expect($response->data)->isEmpty();
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->meta['count'])->equals(1); expect($response->meta['count'])->equals(1);
} }
public function testItCanDuplicateAForm() { public function testItCanDuplicateAForm() {
$response = $this->endpoint->duplicate(['id' => $this->form1->id]); $response = $this->endpoint->duplicate(['id' => $this->form1->getId()]);
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
$form = Form::where('name', 'Copy of Form 1')->findOne(); $form = Form::where('name', 'Copy of Form 1')->findOne();
assert($form instanceof Form); assert($form instanceof Form);
@ -255,18 +262,18 @@ class FormsTest extends \MailPoetTest {
public function testItCanUpdateFormStatus() { public function testItCanUpdateFormStatus() {
$response = $this->endpoint->setStatus([ $response = $this->endpoint->setStatus([
'status' => FormEntity::STATUS_ENABLED, 'status' => FormEntity::STATUS_ENABLED,
'id' => $this->form1->id, 'id' => $this->form1->getId(),
]); ]);
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
$form = $this->reloadForm((int)$this->form1->id); $form = $this->reloadForm((int)$this->form1->getId());
expect($form->status)->equals(FormEntity::STATUS_ENABLED); expect($form->status)->equals(FormEntity::STATUS_ENABLED);
$response = $this->endpoint->setStatus([ $response = $this->endpoint->setStatus([
'status' => FormEntity::STATUS_DISABLED, 'status' => FormEntity::STATUS_DISABLED,
'id' => $this->form1->id, 'id' => $this->form1->getId(),
]); ]);
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
$form = $this->reloadForm((int)$this->form1->id); $form = $this->reloadForm((int)$this->form1->getId());
expect($form->status)->equals(FormEntity::STATUS_DISABLED); expect($form->status)->equals(FormEntity::STATUS_DISABLED);
$response = $this->endpoint->setStatus([ $response = $this->endpoint->setStatus([
@ -276,17 +283,24 @@ class FormsTest extends \MailPoetTest {
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND); expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
$response = $this->endpoint->setStatus([ $response = $this->endpoint->setStatus([
'id' => $this->form1->id, 'id' => $this->form1->getId(),
]); ]);
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
$response = $this->endpoint->setStatus([ $response = $this->endpoint->setStatus([
'status' => 'invalid status', 'status' => 'invalid status',
'id' => $this->form1->id, 'id' => $this->form1->getId(),
]); ]);
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
} }
private function createForm(string $name): FormEntity {
$form = new FormEntity($name);
$this->formsRepository->persist($form);
$this->formsRepository->flush();
return $form;
}
private function reloadForm(int $id): Form { private function reloadForm(int $id): Form {
$reloaded = Form::findOne($id); $reloaded = Form::findOne($id);
assert($reloaded instanceof Form); assert($reloaded instanceof Form);
@ -294,7 +308,7 @@ class FormsTest extends \MailPoetTest {
} }
public function _after() { public function _after() {
Form::deleteMany(); $this->truncateEntity(FormEntity::class);
Segment::deleteMany(); $this->truncateEntity(SegmentEntity::class);
} }
} }