Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Google/Components/EventTicket/EventDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Chiiya\Passes\Common\Component;
use Chiiya\Passes\Google\Components\Common\LocalizedString;
use Chiiya\Passes\Google\Enumerators\DoorsOpenLabel;
use DateTimeInterface;
use Symfony\Component\Validator\Constraints\Choice;

class EventDateTime extends Component
Expand All @@ -19,21 +20,21 @@ public function __construct(
* ISO 8601 + optional offset.
*/
#[Cast(ISO8601DateCaster::class)]
public ?string $doorsOpen = null,
public DateTimeInterface|string|null $doorsOpen = null,
/**
* Optional.
* The date/time when the event starts.
* ISO 8601 + optional offset.
*/
#[Cast(ISO8601DateCaster::class)]
public ?string $start = null,
public DateTimeInterface|string|null $start = null,
/**
* Optional.
* The date/time when the event ends.
* ISO 8601 + optional offset.
*/
#[Cast(ISO8601DateCaster::class)]
public ?string $end = null,
public DateTimeInterface|string|null $end = null,
/**
* Optional.
* The label to use for the doors open value (doorsOpen) on the card detail view.
Expand Down
47 changes: 47 additions & 0 deletions tests/Google/Components/EventTicket/EventDateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace Chiiya\Passes\Tests\Google\Components\EventTicket;

use Chiiya\Passes\Google\Components\EventTicket\EventDateTime;
use Chiiya\Passes\Tests\TestCase;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Group;

class EventDateTimeTest extends TestCase
{
#[Group('google')]
public function test_decodes_iso8601_date_strings_into_dates(): void
{
// Regression for #41: the ISO8601DateCaster returns a DateTimeImmutable, which
// could not be assigned to the previously ?string-typed properties.
$component = EventDateTime::decode([
'doorsOpen' => '2023-01-01T12:00:00+00:00',
'start' => '2023-01-01T13:00:00+00:00',
'end' => '2023-01-01T15:00:00+00:00',
]);

$this->assertInstanceOf(DateTimeImmutable::class, $component->doorsOpen);
$this->assertInstanceOf(DateTimeImmutable::class, $component->start);
$this->assertInstanceOf(DateTimeImmutable::class, $component->end);
}

#[Group('google')]
public function test_serializes_dates_back_to_iso8601_strings(): void
{
$attributes = [
'doorsOpen' => '2023-01-01T12:00:00+00:00',
'start' => '2023-01-01T13:00:00+00:00',
'end' => '2023-01-01T15:00:00+00:00',
];

$this->assertSameArray($attributes, EventDateTime::decode($attributes)->jsonSerialize());
}

#[Group('google')]
public function test_accepts_plain_date_strings_on_construction(): void
{
$component = new EventDateTime(start: '2023-01-01T13:00:00+00:00');

$this->assertSame('2023-01-01T13:00:00+00:00', $component->encode()['start']);
}
}