diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index f799e56d93..211ee668cd 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -104,6 +104,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoetVendor\Doctrine\ORM\EntityManager::class) ->setFactory([new Reference(\MailPoet\Doctrine\EntityManagerFactory::class), 'createEntityManager']) ->setPublic(true); + $container->autowire(\MailPoet\Doctrine\EventListeners\TimestampListener::class); // Cron $container->autowire(\MailPoet\Cron\Daemon::class)->setPublic(true); $container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true); diff --git a/lib/Doctrine/EntityManagerFactory.php b/lib/Doctrine/EntityManagerFactory.php index fb4a4e3bf5..524549a072 100644 --- a/lib/Doctrine/EntityManagerFactory.php +++ b/lib/Doctrine/EntityManagerFactory.php @@ -16,9 +16,12 @@ class EntityManagerFactory { /** @var 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->configuration = $configuration; + $this->timestamp_listener = $timestamp_listener; } function createEntityManager() { @@ -30,7 +33,7 @@ class EntityManagerFactory { private function setupTimestampListener(EntityManager $entity_manager) { $entity_manager->getEventManager()->addEventListener( [Events::prePersist, Events::preUpdate], - new TimestampListener() + $this->timestamp_listener ); } } diff --git a/lib/Doctrine/EventListeners/TimestampListener.php b/lib/Doctrine/EventListeners/TimestampListener.php index e77aa2ad68..84ab43484e 100644 --- a/lib/Doctrine/EventListeners/TimestampListener.php +++ b/lib/Doctrine/EventListeners/TimestampListener.php @@ -5,6 +5,7 @@ namespace MailPoet\Doctrine\EventListeners; use Carbon\Carbon; use MailPoet\Doctrine\EntityTraits\CreatedAtTrait; use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait; +use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Doctrine\ORM\Event\LifecycleEventArgs; use ReflectionObject; @@ -12,8 +13,8 @@ class TimestampListener { /** @var Carbon */ private $now; - function __construct() { - $this->now = Carbon::now(); + function __construct(WPFunctions $wp) { + $this->now = Carbon::createFromTimestamp($wp->currentTime('timestamp')); } function prePersist(LifecycleEventArgs $event_args) { diff --git a/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php b/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php index efbb6de1c5..9268ebc432 100644 --- a/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php +++ b/tests/integration/Doctrine/EventListeners/TimestampListenerTest.php @@ -5,6 +5,8 @@ namespace MailPoet\Test\Doctrine\EventListeners; use Carbon\Carbon; use MailPoet\Doctrine\ConfigurationFactory; use MailPoet\Doctrine\EntityManagerFactory; +use MailPoet\Doctrine\EventListeners\TimestampListener; +use MailPoet\WP\Functions as WPFunctions; require_once __DIR__ . '/TimestampEntity.php'; @@ -12,12 +14,18 @@ class TimestampListenerTest extends \MailPoetTest { /** @var Carbon */ private $now; + /** @var WPFunctions */ + private $wp; + /** @var string */ private $table_name; function _before() { - $this->now = Carbon::now(); - Carbon::setTestNow($this->now); + $timestamp = time(); + $this->now = Carbon::createFromTimestamp($timestamp); + $this->wp = $this->make(WPFunctions::class, [ + 'currentTime' => $timestamp, + ]); $this->table_name = $this->entity_manager->getClassMetadata(TimestampEntity::class)->getTableName(); $this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name"); @@ -64,7 +72,6 @@ class TimestampListenerTest extends \MailPoetTest { function _after() { parent::_after(); - Carbon::setTestNow(); $this->connection->executeUpdate("DROP TABLE IF EXISTS $this->table_name"); } @@ -75,7 +82,8 @@ class TimestampListenerTest extends \MailPoetTest { $metadata_driver = $configuration->newDefaultAnnotationDriver([__DIR__]); $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(); } }