Add Events::fromDateStrings

This commit is contained in:
Oliver Davies 2025-06-15 09:58:14 +01:00
parent 678263c75c
commit 3ba181a753
4 changed files with 30 additions and 48 deletions

View file

@ -35,6 +35,27 @@ readonly final class Events implements \Countable, \IteratorAggregate {
->filter(fn (Event $event): bool => $event->isPast()); ->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 * @param Event[] $events
*/ */

View file

@ -7,6 +7,7 @@ namespace Drupal\Tests\opd_presentations;
use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait; use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date; use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\Events;
use Drupal\opd_presentations\PresentationCounter; use Drupal\opd_presentations\PresentationCounter;
use weitzman\DrupalTestTraits\ExistingSiteBase; use weitzman\DrupalTestTraits\ExistingSiteBase;
@ -20,12 +21,7 @@ final class PresentationCounterTest extends ExistingSiteBase {
assert($counter instanceof PresentationCounter); assert($counter instanceof PresentationCounter);
$this->createPresentation( $this->createPresentation(
events: [ Events::fromDateStrings('yesterday'),
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
],
); );
$this->assertGreaterThanOrEqual( $this->assertGreaterThanOrEqual(
@ -41,12 +37,7 @@ final class PresentationCounterTest extends ExistingSiteBase {
$count = $counter->getPastCount(); $count = $counter->getPastCount();
$this->createPresentation( $this->createPresentation(
events: [ events: Events::fromDateStrings('yesterday'),
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
],
isPublished: FALSE, isPublished: FALSE,
); );
@ -69,17 +60,7 @@ final class PresentationCounterTest extends ExistingSiteBase {
); );
$this->createPresentation( $this->createPresentation(
events: [ Events::fromDateStrings('tomorrow', 'yesterday'),
$this->createEvent(
eventDate: Date::fromString('tomorrow'),
eventName: $this->randomString(),
),
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
],
); );
$counter = $this->container->get(PresentationCounter::class); $counter = $this->container->get(PresentationCounter::class);

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Drupal\opd_presentations\Functional; namespace Drupal\opd_presentations\Functional;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait; use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date; use Drupal\opd_presentations\Events;
use weitzman\DrupalTestTraits\ExistingSiteBase; use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationTest extends ExistingSiteBase { final class PresentationTest extends ExistingSiteBase {
@ -14,22 +14,7 @@ final class PresentationTest extends ExistingSiteBase {
public function test_only_past_events_are_returned(): void { public function test_only_past_events_are_returned(): void {
$presentation = $this->createPresentation( $presentation = $this->createPresentation(
events: [ events: Events::fromDateStrings('now', 'yesterday', 'tomorrow'),
$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 = $presentation->getEvents()->getPast(); $events = $presentation->getEvents()->getPast();
@ -38,11 +23,6 @@ final class PresentationTest extends ExistingSiteBase {
expectedCount: 1, expectedCount: 1,
haystack: $events, haystack: $events,
); );
$this->assertSame(
actual: $events->first()->getEventName(),
expected: 'DrupalCon Lille',
);
} }
} }

View file

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace Drupal\Tests\opd_presentations\Traits; namespace Drupal\Tests\opd_presentations\Traits;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\ctools\Testing\EntityCreationTrait; use Drupal\ctools\Testing\EntityCreationTrait;
use Drupal\opd_presentations\Date; use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\Event; use Drupal\opd_presentations\Event;
use Drupal\opd_presentations\Events;
use Drupal\opd_presentations\Presentation; use Drupal\opd_presentations\Presentation;
trait PresentationCreationTrait { trait PresentationCreationTrait {
@ -19,9 +19,9 @@ trait PresentationCreationTrait {
/** /**
* @param Event[] $events * @param Event[] $events
*/ */
private function createPresentation(array $events, bool $isPublished = TRUE): Presentation { private function createPresentation(Events $events, bool $isPublished = TRUE): Presentation {
$presentation = $this->createNode([ $presentation = $this->createNode([
'field_events' => $events, 'field_events' => $events->toEvents(),
'status' => $isPublished, 'status' => $isPublished,
'type' => Presentation::NODE_TYPE, 'type' => Presentation::NODE_TYPE,
]); ]);