Skip to content

Commit 808837c

Browse files
committed
Fixed date time formats, more unit tests
Signed-off-by: Mua N. Laurent <[email protected]>
1 parent bc32809 commit 808837c

File tree

2 files changed

+187
-35
lines changed

2 files changed

+187
-35
lines changed

src/DataTypes/Calendar.php

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use BaconQrCode\Exception\InvalidArgumentException;
7+
use Exception;
78

89
class Calendar implements DataTypeInterface
910
{
@@ -12,7 +13,7 @@ class Calendar implements DataTypeInterface
1213
*
1314
* @var string
1415
*/
15-
protected $prefix = "BEGIN:VEVENT\n";
16+
protected $prefix = "BEGIN:VEVENT";
1617

1718
/**
1819
* The suffix of the QrCode.
@@ -26,7 +27,7 @@ class Calendar implements DataTypeInterface
2627
*
2728
* @var string
2829
*/
29-
protected $separator = "\n";
30+
protected $separator = "\r\n";
3031

3132
/**
3233
* The summary of the event.
@@ -51,20 +52,23 @@ class Calendar implements DataTypeInterface
5152

5253
/**
5354
* The start time of the event.
55+
* e.g 2020-05-23 15:00
5456
*
55-
* @var DateTime
57+
* @var string
5658
*/
5759
protected $startDateTime;
5860

5961
/**
6062
* The end time of the event.
63+
* e.g 2020-05-24 15:00
6164
*
62-
* @var DateTime
65+
* @var string
6366
*/
6467
protected $endDateTime;
6568

6669
/**
6770
* The standard format for the [$statDateTime] and [$endDateTime].
71+
* Gets overridden if user specifies their own formats [$dateTimeFormat].
6872
*
6973
* @var string
7074
*/
@@ -97,7 +101,7 @@ public function __toString()
97101
*/
98102
protected function buildCalendarString()
99103
{
100-
$calendar = $this->prefix;
104+
$calendar = $this->prefix.$this->separator;
101105

102106
if (isset($this->summary)) {
103107
$calendar .= "SUMMARY:".$this->summary.$this->separator;
@@ -109,10 +113,10 @@ protected function buildCalendarString()
109113
$calendar .= "URL:".$this->url.$this->separator;
110114
}
111115
if (isset($this->startDateTime)) {
112-
$calendar .= "DTSTART:".$this->convertEventDateTimeToString($this->startDateTime).$this->separator;
116+
$calendar .= "DTSTART:".$this->startDateTime.$this->separator;
113117
}
114118
if (isset($this->endDateTime)) {
115-
$calendar .= "DTEND:".$this->convertEventDateTimeToString($this->endDateTime).$this->separator;
119+
$calendar .= "DTEND:".$this->endDateTime.$this->separator;
116120
}
117121

118122
$calendar .= $this->suffix;
@@ -128,20 +132,28 @@ protected function buildCalendarString()
128132
protected function setProperties(array $arguments)
129133
{
130134
$arguments = $arguments[0];
131-
if (isset($arguments['summary'])) {
135+
if (! isset($arguments['summary'])) {
136+
throw new InvalidArgumentException('Please provide an event summary.');
137+
} elseif (! isset($arguments['startDateTime'])) {
138+
throw new InvalidArgumentException('Please provide a start date for the event.');
139+
} else {
140+
141+
if (isset($arguments['dateTimeFormat'])) {
142+
$this->dateTimeFormat = $arguments['dateTimeFormat'];
143+
}
144+
132145
$this->summary = $arguments['summary'];
133-
}
134-
if (isset($arguments['location'])) {
135-
$this->location = $arguments['location'];
136-
}
137-
if (isset($arguments['url'])) {
138-
$this->setUrl($arguments['url']);
139-
}
140-
if (isset($arguments['start_date_time'])) {
141-
$this->setEventDateTime($arguments['start_date_time'], "start");
142-
}
143-
if (isset($arguments['end_date_time'])) {
144-
$this->setEventDateTime($arguments['end_date_time'], "end");
146+
$this->setEventDateTime($arguments['startDateTime'], "start");
147+
148+
if (isset($arguments['location'])) {
149+
$this->location = $arguments['location'];
150+
}
151+
if (isset($arguments['url'])) {
152+
$this->setUrl($arguments['url']);
153+
}
154+
if (isset($arguments['endDateTime'])) {
155+
$this->setEventDateTime($arguments['endDateTime'], "end");
156+
}
145157
}
146158
}
147159

@@ -160,18 +172,18 @@ protected function setUrl($url)
160172
/**
161173
* Sets the datetime property.
162174
*
163-
* @param DateTime $eventDateTime
175+
* @param string $eventDateTime
164176
* @param string $type
165177
*
166178
*/
167179
protected function setEventDateTime($eventDateTime, $type = "")
168180
{
169-
if ($this->isValidDateTime($eventDateTime)) {
181+
if ($this->isValidDateTime($eventDateTime, $type)) {
170182
if ($type == "start") {
171-
$this->startDateTime = $eventDateTime->format("yyyyMMddTHHmmss");
183+
$this->startDateTime = $this->convertEventDateTimeToString($eventDateTime);
172184
}
173185
if ($type == "end") {
174-
$this->endDateTime = $eventDateTime->format("yyyyMMddTHHmmss");
186+
$this->endDateTime = $this->convertEventDateTimeToString($eventDateTime);
175187
}
176188
}
177189
}
@@ -185,7 +197,7 @@ protected function setEventDateTime($eventDateTime, $type = "")
185197
*/
186198
protected function isValidUrl($url)
187199
{
188-
if (! filter_var($url, FILTER_VALIDATE_EMAIL)) {
200+
if (! filter_var($url, FILTER_VALIDATE_URL)) {
189201
throw new InvalidArgumentException('Invalid url provided');
190202
}
191203

@@ -195,36 +207,45 @@ protected function isValidUrl($url)
195207
/**
196208
* Ensures datetime is valid.
197209
*
198-
* @param DateTime $dateTime
210+
* @param string $dateTime
199211
* @param string $type
200212
*
201213
* @return bool
202214
*/
203215
protected function isValidDateTime($dateTime, $type = "")
204216
{
205217
$newDate = DateTime::createFromFormat($this->dateTimeFormat, $dateTime);
206-
if (! $newDate && $newDate->format($this->dateTimeFormat) == $dateTime) {
218+
if (! ($newDate && $newDate->format($this->dateTimeFormat) == $dateTime)) {
207219
if ($type == "start") {
208-
throw new InvalidArgumentException('Invalid start date provided');
220+
throw new InvalidArgumentException('Invalid start date provided. Date must be of format '.
221+
$this->dateTimeFormat);
209222
}
210223
if ($type == "end") {
211-
throw new InvalidArgumentException('Invalid end date provided');
224+
throw new InvalidArgumentException('Invalid end date provided. Date must be of format '.
225+
$this->dateTimeFormat);
212226
}
213-
throw new InvalidArgumentException('Invalid date provided');
227+
throw new InvalidArgumentException('Invalid date provided. Date must be of format '.
228+
$this->dateTimeFormat);
214229
}
215230

216231
return true;
217232
}
218233

234+
219235
/**
220-
* Returns date time to string
236+
* Returns correct date time to string
221237
*
222-
* @param DateTime $dateTime
238+
* @param string $dateTime
223239
*
224240
* @return string
225241
*/
226242
protected function convertEventDateTimeToString($dateTime)
227243
{
228-
return $dateTime->format($this->dateTimeFormat);
244+
try {
245+
$date = new DateTime($dateTime);
246+
} catch (Exception $e) {
247+
throw new InvalidArgumentException('Invalid date provided');
248+
}
249+
return $date->format('yymdTHms');
229250
}
230251
}

tests/DataTypes/CalendarTest.php

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,157 @@
44
namespace DataTypes;
55

66

7+
use BaconQrCode\Exception\InvalidArgumentException;
8+
use DateTime;
79
use PHPUnit\Framework\TestCase;
810
use SimpleSoftwareIO\QrCode\DataTypes\Calendar;
911

1012
class CalendarTest extends TestCase
1113
{
14+
/**
15+
* The separator between the variables.
16+
*
17+
* @var string
18+
*/
19+
private $separator;
1220

1321
public function setUp(): void
1422
{
23+
$this->separator = "\r\n";
1524
$this->calendar = new Calendar();
1625
}
1726

18-
public function test_it_generates_a_proper_format_with_just_the_summary()
27+
public function test_summary_is_mandatory()
28+
{
29+
$this->expectException(InvalidArgumentException::class);
30+
$this->calendar->create([
31+
0 => [
32+
'startDateTime' => '2020-04-15 14:00',
33+
],
34+
]);
35+
}
36+
37+
public function test_start_date_time_is_mandatory()
38+
{
39+
$exceptionOccurred = false;
40+
try {
41+
$this->calendar->create([
42+
0 => [
43+
'summary' => 'My FooBar Event',
44+
],
45+
]);
46+
} catch (InvalidArgumentException $e) {
47+
$exceptionOccurred = true;
48+
$this->assertEquals($e->getMessage(), "Please provide a start date for the event.");
49+
}
50+
51+
$this->assertTrue($exceptionOccurred);
52+
53+
}
54+
55+
public function test_it_generates_a_proper_format_with_just_the_summary_and_start_date_time()
56+
{
57+
$this->calendar->create([
58+
0 => [
59+
'summary' => 'My FooBar Event',
60+
'startDateTime' => '2020-10-08 16:00'
61+
],
62+
]);
63+
64+
$properFormat = "BEGIN:VEVENT".$this->separator.
65+
"SUMMARY:My FooBar Event".$this->separator.
66+
"DTSTART:20201008UTC161000".$this->separator.
67+
"END:VEVENT";
68+
69+
$this->assertEquals($properFormat, strval($this->calendar));
70+
}
71+
72+
public function test_it_generates_a_proper_format_with_summary_start_date_time_and_end_date_time()
73+
{
74+
$this->calendar->create([
75+
0 => [
76+
'summary' => 'My FooBar Event',
77+
'startDateTime' => '2020-10-08 16:00',
78+
'endDateTime' => '2020-10-08 18:00'
79+
],
80+
]);
81+
82+
$properFormat = "BEGIN:VEVENT".$this->separator.
83+
"SUMMARY:My FooBar Event".$this->separator.
84+
"DTSTART:20201008UTC161000".$this->separator.
85+
"DTEND:20201008UTC181000".$this->separator.
86+
"END:VEVENT";
87+
88+
$this->assertEquals($properFormat, strval($this->calendar));
89+
}
90+
91+
public function test_it_generates_a_proper_format_with_specified_date_time_format()
92+
{
93+
$this->calendar->create([
94+
0 => [
95+
'summary' => 'My FooBar Event',
96+
'startDateTime' => '2020-10-08 16:00',
97+
'endDateTime' => '2020-10-08 18:00',
98+
'dateTimeFormat' => 'Y-m-d H:i'
99+
],
100+
]);
101+
102+
$properFormat = "BEGIN:VEVENT".$this->separator.
103+
"SUMMARY:My FooBar Event".$this->separator.
104+
"DTSTART:20201008UTC161000".$this->separator.
105+
"DTEND:20201008UTC181000".$this->separator.
106+
"END:VEVENT";
107+
108+
$this->assertEquals($properFormat, strval($this->calendar));
109+
}
110+
111+
public function test_date_time_format_validation()
112+
{
113+
$this->expectException(InvalidArgumentException::class);
114+
$this->calendar->create([
115+
0 => [
116+
'summary' => 'My FooBar Event',
117+
'startDateTime' => '2020-10-08 16:00',
118+
'endDateTime' => '2020-10-08 18:00',
119+
'dateTimeFormat' => 'Y/m/d H:i'
120+
],
121+
]);
122+
}
123+
124+
public function test_it_generates_a_proper_format_with_summary_start_date_time_and_just_location()
125+
{
126+
$this->calendar->create([
127+
0 => [
128+
'summary' => 'My FooBar Event',
129+
'startDateTime' => '2020-10-08 16:00',
130+
'location' => 'Foo Location'
131+
],
132+
]);
133+
134+
$properFormat = "BEGIN:VEVENT".$this->separator.
135+
"SUMMARY:My FooBar Event".$this->separator.
136+
"DTSTART:20201008UTC161000".$this->separator.
137+
"LOCATION:Foo Location".$this->separator.
138+
"END:VEVENT";
139+
140+
$this->assertEquals($properFormat, strval($this->calendar));
141+
}
142+
143+
public function test_it_generates_a_proper_format_with_summary_start_date_time_and_just_url()
19144
{
20145
$this->calendar->create([
21146
0 => [
22147
'summary' => 'My FooBar Event',
148+
'startDateTime' => '2020-10-08 16:00',
149+
'url' => 'https://www.google.com'
23150
],
24151
]);
25152

26-
$properFormat = "BEGIN:VEVENT\nSUMMARY:My FooBar Event\nEND:VEVENT";
153+
$properFormat = "BEGIN:VEVENT".$this->separator.
154+
"SUMMARY:My FooBar Event".$this->separator.
155+
"DTSTART:20201008UTC161000".$this->separator.
156+
"URL:https://www.google.com".$this->separator.
157+
"END:VEVENT";
27158

28159
$this->assertEquals($properFormat, strval($this->calendar));
29160
}

0 commit comments

Comments
 (0)