From b1ec1b95005385f15f8d0309fcf37a88cb2b7aa0 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Tue, 21 Feb 2023 11:44:55 +0100 Subject: [PATCH] Add helper method for updating updatedAt of an entity in integration tests We can't simply set updatedAt via setter on the entity. The value gets overwritten in the TimeStampListener. This commit adds a simple method that saves updatedAt via dbal connection and refreshes the entity. [MAILPOET-4828] --- mailpoet/tests/integration/_bootstrap.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mailpoet/tests/integration/_bootstrap.php b/mailpoet/tests/integration/_bootstrap.php index 87ee88bf07..10dafab23d 100644 --- a/mailpoet/tests/integration/_bootstrap.php +++ b/mailpoet/tests/integration/_bootstrap.php @@ -9,6 +9,7 @@ use MailPoet\Features\FeaturesController; use MailPoet\Settings\SettingsController; use MailPoetVendor\Doctrine\DBAL\Connection; use MailPoetVendor\Doctrine\ORM\EntityManager; +use MailPoetVendor\Doctrine\Persistence\Mapping\ClassMetadata; if ((boolean)getenv('MULTISITE') === true) { // REQUEST_URI needs to be set for WP to load the proper subsite where MailPoet is activated @@ -163,6 +164,27 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore $connection->executeQuery('SET FOREIGN_KEY_CHECKS=1'); } + /** + * This is a helper function to update the updated_at column of an entity. + * The updatedAt column is automatically updated in MailPoet\Doctrine\EventListeners\TimestampListener + * so it is not possible to set it manually using a setter. + */ + public function setUpdatedAtForEntity($entity, DateTimeInterface $updatedAt) { + $className = (string)get_class($entity); + $classMetadata = $this->entityManager->getClassMetadata($className); + if (!$classMetadata instanceof ClassMetadata) { + throw new \Exception("Entity $className not found"); + } + $tableName = $classMetadata->getTableName(); + $connection = $this->entityManager->getConnection(); + $connection->executeQuery(" + UPDATE $tableName + SET updated_at = '{$updatedAt->format('Y-m-d H:i:s')}' + WHERE id = {$entity->getId()} + "); + $this->entityManager->refresh($entity); + } + public function clearSubscribersCountCache() { $cache = $this->diContainer->get(TransientCache::class); $cache->invalidateItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY);