Add integration tests for LastSubscribedAtListener

This commit adds basic integration tests for the class
LastSubscribedAtListener since it had none and it will be modified in a
subsequent commit. A method from TimestampListenerTest was moved to a
base class since it can be reused in LastSubscribedAtTest.

[MAILPOET-3946]
This commit is contained in:
Rodrigo Primo
2021-11-16 14:36:59 -03:00
committed by Veljko V
parent 2e050d7a32
commit 9e334a953a
4 changed files with 119 additions and 18 deletions

View File

@@ -127,7 +127,7 @@ class ContainerConfigurator implements IContainerConfigurator {
->setFactory([new Reference(\MailPoet\Doctrine\EntityManagerFactory::class), 'createEntityManager'])
->setPublic(true);
$container->autowire(\MailPoet\Doctrine\EventListeners\EmojiEncodingListener::class)->setPublic(true);
$container->autowire(\MailPoet\Doctrine\EventListeners\LastSubscribedAtListener::class);
$container->autowire(\MailPoet\Doctrine\EventListeners\LastSubscribedAtListener::class)->setPublic(true);
$container->autowire(\MailPoet\Doctrine\EventListeners\TimestampListener::class)->setPublic(true);
$container->autowire(\MailPoet\Doctrine\EventListeners\ValidationListener::class);
$container->autowire(\MailPoet\Doctrine\Validator\ValidatorFactory::class);

View File

@@ -0,0 +1,23 @@
<?php
namespace MailPoet\Test\Doctrine\EventListeners;
use MailPoetVendor\Doctrine\ORM\Events;
class EventListenersBaseTest extends \MailPoetTest {
/**
* Replaces event listeners. Needed to test them since EventManager
* is shared for all entity managers using same DB connection.
*/
protected function replaceListeners($original, $replacement) {
$this->entityManager->getEventManager()->removeEventListener(
[Events::prePersist, Events::preUpdate],
$original
);
$this->entityManager->getEventManager()->addEventListener(
[Events::prePersist, Events::preUpdate],
$replacement
);
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace MailPoet\Test\Doctrine\EventListeners;
use MailPoet\Doctrine\EventListeners\LastSubscribedAtListener;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
require_once __DIR__ . '/EventListenersBaseTest.php';
class LastSubscribedAtTest extends EventListenersBaseTest {
/** @var Carbon */
private $now;
/** @var WPFunctions */
private $wp;
public function _before() {
$timestamp = time();
$this->now = Carbon::createFromTimestamp($timestamp);
$this->wp = $this->make(WPFunctions::class, [
'currentTime' => $timestamp,
]);
$newTimestampListener = new LastSubscribedAtListener($this->wp);
$originalListener = $this->diContainer->get(LastSubscribedAtListener::class);
$this->replaceListeners($originalListener, $newTimestampListener);
}
public function testItSetsLastSubscribedAtOnCreate() {
$entity = new SubscriberEntity();
$entity->setEmail('test@test.com');
$entity->setStatus(SubscriberEntity::STATUS_SUBSCRIBED);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->assertEquals($this->now, $entity->getLastSubscribedAt());
}
public function testItDoesntSetLastSubscribedAtOnCreateWhenStatusIsNotSubscribed() {
$entity = new SubscriberEntity();
$entity->setEmail('test@test.com');
$entity->setStatus(SubscriberEntity::STATUS_INACTIVE);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->assertNull($entity->getLastSubscribedAt());
}
public function testItSetsLastSubscribedAtOnUpdate() {
$pastDate = new Carbon('2000-01-01 12:00:00');
$entity = new SubscriberEntity();
$entity->setEmail('test@test.com');
$entity->setStatus(SubscriberEntity::STATUS_INACTIVE);
$entity->setLastSubscribedAt($pastDate);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->assertEquals($pastDate, $entity->getLastSubscribedAt());
$entity->setStatus(SubscriberEntity::STATUS_SUBSCRIBED);
$this->entityManager->flush();
$this->assertEquals($this->now, $entity->getLastSubscribedAt());
}
public function testItDoesntChangeLastSubscribedAtOnUpdateIfStatusIsNotSubscribed() {
$pastDate = new Carbon('2000-01-01 12:00:00');
$entity = new SubscriberEntity();
$entity->setEmail('test@test.com');
$entity->setStatus(SubscriberEntity::STATUS_INACTIVE);
$entity->setLastSubscribedAt($pastDate);
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->assertEquals($pastDate, $entity->getLastSubscribedAt());
$entity->setEmail('test2@test.com');
$this->entityManager->flush();
$this->assertEquals($pastDate, $entity->getLastSubscribedAt());
}
public function _after() {
parent::_after();
$this->truncateEntity(SubscriberEntity::class);
}
}

View File

@@ -7,9 +7,10 @@ use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Carbon\Carbon;
use MailPoetVendor\Doctrine\ORM\Events;
require_once __DIR__ . '/EventListenersBaseTest.php';
require_once __DIR__ . '/TimestampEntity.php';
class TimestampListenerTest extends \MailPoetTest {
class TimestampListenerTest extends EventListenersBaseTest {
/** @var Carbon */
private $now;
@@ -96,20 +97,4 @@ class TimestampListenerTest extends \MailPoetTest {
parent::_after();
$this->connection->executeStatement("DROP TABLE IF EXISTS $this->tableName");
}
/**
* We have to replace event listeners since EventManager
* is shared for all entity managers using same DB connection
*/
private function replaceListeners($original, $replacement) {
$this->entityManager->getEventManager()->removeEventListener(
[Events::prePersist, Events::preUpdate],
$original
);
$this->entityManager->getEventManager()->addEventListener(
[Events::prePersist, Events::preUpdate],
$replacement
);
}
}