Extract a Date class for event dates

This commit is contained in:
Oliver Davies 2025-06-12 23:27:58 +01:00
parent c2dd8a1a07
commit c2cd2164f5
3 changed files with 27 additions and 5 deletions

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
readonly final class Date {
public function toTimestamp(): int {
return $this->date->getTimestamp();
}
public static function fromString(string $date): self {
return new self(new \DateTimeImmutable($date));
}
private function __construct(private \DateTimeImmutable $date) {
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\opd_presentations\Functional;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationTest extends ExistingSiteBase {
@ -15,17 +16,17 @@ final class PresentationTest extends ExistingSiteBase {
$presentation = $this->createPresentation(
events: [
$this->createEvent(
eventDate: 'now',
eventDate: Date::fromString('now'),
eventName: 'PHP South West',
),
$this->createEvent(
eventDate: 'yesterday',
eventDate: Date::fromString('yesterday'),
eventName: 'DrupalCon Lille',
),
$this->createEvent(
eventDate: 'tomorrow',
eventDate: Date::fromString('tomorrow'),
eventName: 'PHP Oxford',
),
],

View file

@ -7,6 +7,7 @@ namespace Drupal\Tests\opd_presentations\Traits;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\ctools\Testing\EntityCreationTrait;
use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\Event;
use Drupal\opd_presentations\Presentation;
@ -29,11 +30,11 @@ trait PresentationCreationTrait {
return $presentation;
}
private function createEvent(string $eventName, string $eventDate): Event {
private function createEvent(string $eventName, Date $eventDate): Event {
$event = $this->createEntity(
entity_type: 'paragraph',
values: [
'field_date' => (new DrupalDateTime($eventDate))->getTimestamp(),
'field_date' => $eventDate->toTimestamp(),
'field_event_name' => $eventName,
'type' => Event::PARAGRAPH_TYPE,
],