Skip to content

Commit 1b9af1c

Browse files
author
joseph
committed
functionality to force the timestamp fields
1 parent 3652ceb commit 1b9af1c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Entity/Fields/Traits/TimeStamp/CreationTimestampFieldTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*
2121
* Notice the use of a lifecyle event to handle setting the pre persist creation timestamp
2222
*
23+
* In test fixtures you will want to use something like
24+
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator::forceTimestamp
25+
* to provide consistent fixtures
26+
*
2327
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\DateTime
2428
*/
2529
trait CreationTimestampFieldTrait

src/Entity/Fields/Traits/TimeStamp/UpdatedAtTimestampFieldTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
/**
1717
* Trait TimestampFieldTrait
1818
*
19-
* An Immutable creation timestamp. It is null until it is saved (and reloaded)
19+
* An Immutable updated timestamp. It is null until it is saved (and reloaded)
2020
*
2121
* Notice the use of a lifecyle event to handle setting the pre persist creation timestamp
2222
*
23+
* In test fixtures you will want to use something like
24+
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGenerator::forceTimestamp
25+
* to provide consistent fixtures
26+
*
2327
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\DateTime
2428
*/
2529
trait UpdatedAtTimestampFieldTrait

src/Entity/Testing/EntityGenerator/TestEntityGenerator.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator;
66

7+
use DateTimeImmutable;
78
use Doctrine\Common\Collections\Collection;
89
use Doctrine\ORM\EntityManagerInterface;
10+
use Edmonds\MarketingEntities\Entity\Fields\Traits\Website\Platform\Search\WebsiteEngineHitLog\TimestampFieldTrait;
911
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta;
1012
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory;
1113
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface;
14+
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\TimeStamp\CreationTimestampFieldTrait;
1215
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface;
1316
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
1417
use EdmondsCommerce\DoctrineStaticMeta\RelationshipHelper;
@@ -159,6 +162,23 @@ public function generateDto(): DataTransferObjectInterface
159162
return $dto;
160163
}
161164

165+
/**
166+
* Timestamp columns need to be forcibly updated with dates or they will always reflect the time of test run
167+
*
168+
* @param EntityInterface $entity
169+
* @param DateTimeImmutable $date
170+
* @param string $propertyName
171+
*
172+
* @see CreationTimestampFieldTrait
173+
*/
174+
public function forceTimestamp(EntityInterface $entity, DateTimeImmutable $date, string $propertyName): void
175+
{
176+
$property = $entity::getDoctrineStaticMeta()
177+
->getReflectionClass()
178+
->getProperty($propertyName);
179+
$property->setValue($entity, $date);
180+
}
181+
162182
public function fakerUpdateDto(DataTransferObjectInterface $dto): void
163183
{
164184
$this->fakerDataFiller->updateDtoWithFakeData($dto);
@@ -181,8 +201,8 @@ public function addAssociationEntities(
181201
if (empty($mappings)) {
182202
return;
183203
}
184-
$methods = array_map('strtolower', get_class_methods($generated));
185-
$relationHelper = $this->relationshipHelper;
204+
$methods = array_map('strtolower', get_class_methods($generated));
205+
$relationHelper = $this->relationshipHelper;
186206
foreach ($mappings as $mapping) {
187207
$getter = $relationHelper->getGetterFromDoctrineMapping($mapping);
188208
$isPlural = $relationHelper->isPlural($mapping);

0 commit comments

Comments
 (0)