From 3ba181a75348f1b7f8cd4919cd6a8972de3426a9 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 15 Jun 2025 09:58:14 +0100 Subject: [PATCH] Add `Events::fromDateStrings` --- modules/opd_presentations/src/Events.php | 21 +++++++++++++++ .../Functional/PresentationCounterTest.php | 27 +++---------------- .../tests/src/Functional/PresentationTest.php | 24 ++--------------- .../src/Traits/PresentationCreationTrait.php | 6 ++--- 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/modules/opd_presentations/src/Events.php b/modules/opd_presentations/src/Events.php index 4e60a9ad1..a33142298 100644 --- a/modules/opd_presentations/src/Events.php +++ b/modules/opd_presentations/src/Events.php @@ -35,6 +35,27 @@ readonly final class Events implements \Countable, \IteratorAggregate { ->filter(fn (Event $event): bool => $event->isPast()); } + /** + * @param non-empty-string[] $events + */ + public static function fromDateStrings(string ...$dates): self { + $events = array_map( + array: $dates, + callback: fn (string $date): Event => Event::create([ + 'field_date' => strtotime($date), + ]), + ); + + return new self($events); + } + + /** + * @return Event[] + */ + public function toEvents(): array { + return $this->events; + } + /** * @param Event[] $events */ diff --git a/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php b/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php index 27e1e28d4..649c62050 100644 --- a/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php +++ b/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\opd_presentations; use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait; use Drupal\opd_presentations\Date; +use Drupal\opd_presentations\Events; use Drupal\opd_presentations\PresentationCounter; use weitzman\DrupalTestTraits\ExistingSiteBase; @@ -20,12 +21,7 @@ final class PresentationCounterTest extends ExistingSiteBase { assert($counter instanceof PresentationCounter); $this->createPresentation( - events: [ - $this->createEvent( - eventDate: Date::fromString('yesterday'), - eventName: $this->randomString(), - ), - ], + Events::fromDateStrings('yesterday'), ); $this->assertGreaterThanOrEqual( @@ -41,12 +37,7 @@ final class PresentationCounterTest extends ExistingSiteBase { $count = $counter->getPastCount(); $this->createPresentation( - events: [ - $this->createEvent( - eventDate: Date::fromString('yesterday'), - eventName: $this->randomString(), - ), - ], + events: Events::fromDateStrings('yesterday'), isPublished: FALSE, ); @@ -69,17 +60,7 @@ final class PresentationCounterTest extends ExistingSiteBase { ); $this->createPresentation( - events: [ - $this->createEvent( - eventDate: Date::fromString('tomorrow'), - eventName: $this->randomString(), - ), - - $this->createEvent( - eventDate: Date::fromString('yesterday'), - eventName: $this->randomString(), - ), - ], + Events::fromDateStrings('tomorrow', 'yesterday'), ); $counter = $this->container->get(PresentationCounter::class); diff --git a/modules/opd_presentations/tests/src/Functional/PresentationTest.php b/modules/opd_presentations/tests/src/Functional/PresentationTest.php index 16f87150f..290d14bd2 100644 --- a/modules/opd_presentations/tests/src/Functional/PresentationTest.php +++ b/modules/opd_presentations/tests/src/Functional/PresentationTest.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace Drupal\opd_presentations\Functional; use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait; -use Drupal\opd_presentations\Date; +use Drupal\opd_presentations\Events; use weitzman\DrupalTestTraits\ExistingSiteBase; final class PresentationTest extends ExistingSiteBase { @@ -14,22 +14,7 @@ final class PresentationTest extends ExistingSiteBase { public function test_only_past_events_are_returned(): void { $presentation = $this->createPresentation( - events: [ - $this->createEvent( - eventDate: Date::fromString('now'), - eventName: 'PHP South West', - ), - - $this->createEvent( - eventDate: Date::fromString('yesterday'), - eventName: 'DrupalCon Lille', - ), - - $this->createEvent( - eventDate: Date::fromString('tomorrow'), - eventName: 'PHP Oxford', - ), - ], + events: Events::fromDateStrings('now', 'yesterday', 'tomorrow'), ); $events = $presentation->getEvents()->getPast(); @@ -38,11 +23,6 @@ final class PresentationTest extends ExistingSiteBase { expectedCount: 1, haystack: $events, ); - - $this->assertSame( - actual: $events->first()->getEventName(), - expected: 'DrupalCon Lille', - ); } } diff --git a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php index d165fe6e5..f425bc956 100644 --- a/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php +++ b/modules/opd_presentations/tests/src/Traits/PresentationCreationTrait.php @@ -4,11 +4,11 @@ declare(strict_types=1); 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\Events; use Drupal\opd_presentations\Presentation; trait PresentationCreationTrait { @@ -19,9 +19,9 @@ trait PresentationCreationTrait { /** * @param Event[] $events */ - private function createPresentation(array $events, bool $isPublished = TRUE): Presentation { + private function createPresentation(Events $events, bool $isPublished = TRUE): Presentation { $presentation = $this->createNode([ - 'field_events' => $events, + 'field_events' => $events->toEvents(), 'status' => $isPublished, 'type' => Presentation::NODE_TYPE, ]);