Skip to content

Commit 1458519

Browse files
authored
Merge pull request #162 from packbackbooks/eng-4471-null-dates
ENG-4471: Allow DateTimeInterval to have `null` start and end
2 parents ac6c720 + f162a2b commit 1458519

File tree

3 files changed

+119
-11
lines changed

3 files changed

+119
-11
lines changed

src/DeepLinkResources/DateTimeInterval.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
class DateTimeInterval
1010
{
1111
use Arrayable;
12-
public const ERROR_NO_START_OR_END = 'Either a start or end time must be specified.';
1312
public const ERROR_START_GT_END = 'The start time cannot be greater than end time.';
1413

1514
public function __construct(
@@ -26,15 +25,11 @@ public static function new(): self
2625

2726
public function getArray(): array
2827
{
29-
if (!isset($this->start) && !isset($this->end)) {
30-
throw new LtiException(self::ERROR_NO_START_OR_END);
31-
}
32-
3328
$this->validateStartAndEnd();
3429

3530
return [
36-
'startDateTime' => $this->start?->format(DateTime::ATOM),
37-
'endDateTime' => $this->end?->format(DateTime::ATOM),
31+
'startDateTime' => $this->start?->format(DateTime::ATOM) ?? null,
32+
'endDateTime' => $this->end?->format(DateTime::ATOM) ?? null,
3833
];
3934
}
4035

tests/DeepLinkResources/DateTimeIntervalTest.php

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ public function test_it_creates_a_new_instance()
3232
$this->assertInstanceOf(DateTimeInterval::class, $DeepLinkResources);
3333
}
3434

35+
public function test_it_instantiates_with_null_start_end_values()
36+
{
37+
$dateTimeInterval = new DateTimeInterval(null, null);
38+
39+
$this->assertInstanceOf(DateTimeInterval::class, $dateTimeInterval);
40+
$this->assertNull($dateTimeInterval->getStart());
41+
$this->assertNull($dateTimeInterval->getEnd());
42+
}
43+
44+
public function test_it_instantiates_with_null_start()
45+
{
46+
$end = date_create('+1 day');
47+
$dateTimeInterval = new DateTimeInterval(null, $end);
48+
49+
$this->assertInstanceOf(DateTimeInterval::class, $dateTimeInterval);
50+
$this->assertNull($dateTimeInterval->getStart());
51+
$this->assertEquals($end, $dateTimeInterval->getEnd());
52+
}
53+
54+
public function test_it_instantiates_with_null_end()
55+
{
56+
$start = date_create();
57+
$dateTimeInterval = new DateTimeInterval($start, null);
58+
59+
$this->assertInstanceOf(DateTimeInterval::class, $dateTimeInterval);
60+
$this->assertEquals($start, $dateTimeInterval->getStart());
61+
$this->assertNull($dateTimeInterval->getEnd());
62+
}
63+
3564
public function test_it_gets_start()
3665
{
3766
$result = $this->dateTimeInterval->getStart();
@@ -66,15 +95,62 @@ public function test_it_sets_end()
6695
$this->assertEquals($expected, $this->dateTimeInterval->getEnd());
6796
}
6897

69-
public function test_it_throws_exception_when_creating_array_with_both_properties_null()
98+
public function test_it_sets_start_to_null()
99+
{
100+
$result = $this->dateTimeInterval->setStart(null);
101+
102+
$this->assertSame($this->dateTimeInterval, $result);
103+
$this->assertNull($this->dateTimeInterval->getStart());
104+
}
105+
106+
public function test_it_sets_end_to_null()
107+
{
108+
$result = $this->dateTimeInterval->setEnd(null);
109+
110+
$this->assertSame($this->dateTimeInterval, $result);
111+
$this->assertNull($this->dateTimeInterval->getEnd());
112+
}
113+
114+
public function test_it_creates_array_with_null_start()
115+
{
116+
$expectedEnd = date_create('+1 day');
117+
$expected = [
118+
'endDateTime' => $expectedEnd->format(DateTime::ATOM),
119+
];
120+
121+
$this->dateTimeInterval->setStart(null);
122+
$this->dateTimeInterval->setEnd($expectedEnd);
123+
124+
$result = $this->dateTimeInterval->toArray();
125+
126+
$this->assertEquals($expected, $result);
127+
}
128+
129+
public function test_it_creates_array_with_null_end()
70130
{
131+
$expectedStart = date_create('+1 day');
132+
$expected = [
133+
'startDateTime' => $expectedStart->format(DateTime::ATOM),
134+
];
135+
136+
$this->dateTimeInterval->setStart($expectedStart);
137+
$this->dateTimeInterval->setEnd(null);
138+
139+
$result = $this->dateTimeInterval->toArray();
140+
141+
$this->assertEquals($expected, $result);
142+
}
143+
144+
public function test_it_creates_array_with_both_null()
145+
{
146+
$expected = [];
147+
71148
$this->dateTimeInterval->setStart(null);
72149
$this->dateTimeInterval->setEnd(null);
73150

74-
$this->expectException(LtiException::class);
75-
$this->expectExceptionMessage(DateTimeInterval::ERROR_NO_START_OR_END);
151+
$result = $this->dateTimeInterval->toArray();
76152

77-
$this->dateTimeInterval->toArray();
153+
$this->assertEquals($expected, $result);
78154
}
79155

80156
public function test_it_throws_exception_when_creating_array_with_invalid_time_interval()

tests/DeepLinkResources/ResourceTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,43 @@ public function test_it_sets_submission_interval()
237237
$this->assertEquals($expected, $this->resource->getSubmissionInterval());
238238
}
239239

240+
public function test_it_creates_array_with_null_date_time_intervals()
241+
{
242+
$expected = [
243+
'type' => LtiConstants::DL_RESOURCE_LINK_TYPE,
244+
];
245+
246+
$this->resource->setAvailabilityInterval(null);
247+
$this->resource->setSubmissionInterval(null);
248+
249+
$result = $this->resource->toArray();
250+
251+
$this->assertEquals($expected, $result);
252+
}
253+
254+
public function test_it_creates_array_with_date_time_intervals_having_null_dates()
255+
{
256+
$availabilityInterval = new DateTimeInterval(date_create(), null);
257+
$submissionInterval = new DateTimeInterval(null, date_create());
258+
259+
$expected = [
260+
'type' => LtiConstants::DL_RESOURCE_LINK_TYPE,
261+
'available' => [
262+
'startDateTime' => $availabilityInterval->getStart()->format(\DateTime::ATOM),
263+
],
264+
'submission' => [
265+
'endDateTime' => $submissionInterval->getEnd()->format(\DateTime::ATOM),
266+
],
267+
];
268+
269+
$this->resource->setAvailabilityInterval($availabilityInterval);
270+
$this->resource->setSubmissionInterval($submissionInterval);
271+
272+
$result = $this->resource->toArray();
273+
274+
$this->assertEquals($expected, $result);
275+
}
276+
240277
public function test_it_creates_array_with_defined_optional_properties()
241278
{
242279
$icon = Icon::new('https://example.com/image.png', 100, 200);

0 commit comments

Comments
 (0)