Use WP timestamp in TimestampListener

[MAILPOET-2014]
This commit is contained in:
Jan Jakeš
2019-07-23 15:36:38 +02:00
committed by M. Shull
parent 0045683d92
commit 11b166d018
4 changed files with 21 additions and 8 deletions

View File

@ -104,6 +104,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoetVendor\Doctrine\ORM\EntityManager::class) $container->autowire(\MailPoetVendor\Doctrine\ORM\EntityManager::class)
->setFactory([new Reference(\MailPoet\Doctrine\EntityManagerFactory::class), 'createEntityManager']) ->setFactory([new Reference(\MailPoet\Doctrine\EntityManagerFactory::class), 'createEntityManager'])
->setPublic(true); ->setPublic(true);
$container->autowire(\MailPoet\Doctrine\EventListeners\TimestampListener::class);
// Cron // Cron
$container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true); $container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true);
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true); $container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);

View File

@ -16,9 +16,12 @@ class EntityManagerFactory {
/** @var Configuration */ /** @var Configuration */
private $configuration; private $configuration;
function __construct(Connection $connection, Configuration $configuration) { private $timestamp_listener;
function __construct(Connection $connection, Configuration $configuration, TimestampListener $timestamp_listener) {
$this->connection = $connection; $this->connection = $connection;
$this->configuration = $configuration; $this->configuration = $configuration;
$this->timestamp_listener = $timestamp_listener;
} }
function createEntityManager() { function createEntityManager() {
@ -30,7 +33,7 @@ class EntityManagerFactory {
private function setupTimestampListener(EntityManager $entity_manager) { private function setupTimestampListener(EntityManager $entity_manager) {
$entity_manager->getEventManager()->addEventListener( $entity_manager->getEventManager()->addEventListener(
[Events::prePersist, Events::preUpdate], [Events::prePersist, Events::preUpdate],
new TimestampListener() $this->timestamp_listener
); );
} }
} }

View File

@ -5,6 +5,7 @@ namespace MailPoet\Doctrine\EventListeners;
use Carbon\Carbon; use Carbon\Carbon;
use MailPoet\Doctrine\EntityTraits\CreatedAtTrait; use MailPoet\Doctrine\EntityTraits\CreatedAtTrait;
use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait; use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
use MailPoet\WP\Functions as WPFunctions;
use MailPoetVendor\Doctrine\ORM\Event\LifecycleEventArgs; use MailPoetVendor\Doctrine\ORM\Event\LifecycleEventArgs;
use ReflectionObject; use ReflectionObject;
@ -12,8 +13,8 @@ class TimestampListener {
/** @var Carbon */ /** @var Carbon */
private $now; private $now;
function __construct() { function __construct(WPFunctions $wp) {
$this->now = Carbon::now(); $this->now = Carbon::createFromTimestamp($wp->currentTime('timestamp'));
} }
function prePersist(LifecycleEventArgs $event_args) { function prePersist(LifecycleEventArgs $event_args) {

View File

@ -5,6 +5,8 @@ namespace MailPoet\Test\Doctrine\EventListeners;
use Carbon\Carbon; use Carbon\Carbon;
use MailPoet\Doctrine\ConfigurationFactory; use MailPoet\Doctrine\ConfigurationFactory;
use MailPoet\Doctrine\EntityManagerFactory; use MailPoet\Doctrine\EntityManagerFactory;
use MailPoet\Doctrine\EventListeners\TimestampListener;
use MailPoet\WP\Functions as WPFunctions;
require_once __DIR__ . '/TimestampEntity.php'; require_once __DIR__ . '/TimestampEntity.php';
@ -12,12 +14,18 @@ class TimestampListenerTest extends \MailPoetTest {
/** @var Carbon */ /** @var Carbon */
private $now; private $now;
/** @var WPFunctions */
private $wp;
/** @var string */ /** @var string */
private $table_name; private $table_name;
function _before() { function _before() {
$this->now = Carbon::now(); $timestamp = time();
Carbon::setTestNow($this->now); $this->now = Carbon::createFromTimestamp($timestamp);
$this->wp = $this->make(WPFunctions::class, [
'currentTime' => $timestamp,
]);
$this->table_name = $this->entity_manager->getClassMetadata(TimestampEntity::class)->getTableName(); $this->table_name = $this->entity_manager->getClassMetadata(TimestampEntity::class)->getTableName();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name"); $this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name");
@ -64,7 +72,6 @@ class TimestampListenerTest extends \MailPoetTest {
function _after() { function _after() {
parent::_after(); parent::_after();
Carbon::setTestNow();
$this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name"); $this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name");
} }
@ -75,7 +82,8 @@ class TimestampListenerTest extends \MailPoetTest {
$metadata_driver = $configuration->newDefaultAnnotationDriver([__DIR__]); $metadata_driver = $configuration->newDefaultAnnotationDriver([__DIR__]);
$configuration->setMetadataDriverImpl($metadata_driver); $configuration->setMetadataDriverImpl($metadata_driver);
$entity_manager_factory = new EntityManagerFactory($this->connection, $configuration); $timestamp_listener = new TimestampListener($this->wp);
$entity_manager_factory = new EntityManagerFactory($this->connection, $configuration, $timestamp_listener);
return $entity_manager_factory->createEntityManager(); return $entity_manager_factory->createEntityManager();
} }
} }