Skip to content

Commit

Permalink
Improve page fixtures factory date management
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Dec 3, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 258e062 commit 45abd13
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Fixture/Factory/PageFixtureFactory.php
Original file line number Diff line number Diff line change
@@ -86,15 +86,8 @@ public function create(array $options = []): PageInterface
$page->setEnabled($options['enabled']);
$page->setCode($options['code']);

$publishAt = $options['publish_at'] ?? null;
if ($publishAt) {
$page->setPublishAt(new DateTime($publishAt));
}

$unpublishAt = $options['unpublish_at'] ?? null;
if ($unpublishAt) {
$page->setUnpublishAt(new DateTime($unpublishAt));
}
$page->setPublishAt($options['publish_at']);
$page->setUnpublishAt($options['unpublish_at']);

foreach ($options['channels'] as $channel) {
$page->addChannel($channel);
@@ -124,6 +117,8 @@ private function createTranslations(PageInterface $page, array $options): void

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function configureOptions(OptionsResolver $resolver): void
{
@@ -142,9 +137,15 @@ protected function configureOptions(OptionsResolver $resolver): void
->setDefault('publish_at', function (Options $options) use ($publishAt, $hasPublishAt): ?string {
return $hasPublishAt ? $publishAt->format('Y-m-d H:i:s') : null;
})
->setNormalizer('publish_at', function (Options $options, $value): ?DateTime {
return null === $value ? null : new DateTime($value);
})
->setDefault('unpublish_at', function (Options $options) use ($publishAt): ?string {
return $this->faker->boolean(20) ? (clone $publishAt)->modify('+' . $this->faker->numberBetween(1, 20) . ' days')->format('Y-m-d H:i:s') : null;
})
->setNormalizer('unpublish_at', function (Options $options, $value): ?DateTime {
return null === $value ? null : new DateTime($value);
})
->setDefault('channels', LazyOption::all($this->channelRepository))
->setAllowedTypes('channels', 'array')
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))

0 comments on commit 45abd13

Please sign in to comment.