Skip to content

Commit f38def7

Browse files
committed
test: add base tests
1 parent e00f1b3 commit f38def7

7 files changed

Lines changed: 526 additions & 1 deletion

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
if (!function_exists('tick')) {
66
/**
77
* Initialize a new date instance
8-
* @param string|DateTime $userDate The date to initialize with
8+
* @param string|DateTime|\Leaf\Date $userDate The date to initialize with
99
* @param string|null $userTimeZone The timezone to initialize with
1010
* @return \Leaf\Date
1111
*/

tests/date-basics.test.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use Leaf\Date;
4+
5+
test('Date class can be instantiated', function () {
6+
$date = new Date();
7+
expect($date)->toBeInstanceOf(Date::class);
8+
});
9+
10+
test('now returns current date as string', function () {
11+
$date = new Date();
12+
expect($date->now())->toBeString();
13+
});
14+
15+
test('tick initializes with current date by default', function () {
16+
$date = tick();
17+
expect($date)->toBeInstanceOf(Date::class);
18+
});
19+
20+
test('tick accepts DateTime object', function () {
21+
$dateTime = new DateTime('2023-01-01');
22+
$date = tick($dateTime);
23+
expect($date->toDateString())->toBe('2023-01-01');
24+
});
25+
26+
test('tick accepts Date object', function () {
27+
$date1 = tick('2023-01-01');
28+
29+
$date2 = tick($date1);
30+
31+
expect($date2->toDateString())->toBe('2023-01-01');
32+
});
33+
34+
test('tick accepts string date', function () {
35+
$date = tick('2023-01-01');
36+
expect($date->toDateString())->toBe('2023-01-01');
37+
});
38+
39+
test('tick accepts date with slashes', function () {
40+
$date = tick('2023/01/01');
41+
expect($date->toDateString())->toBe('2023-01-01');
42+
});
43+
44+
test('tick accepts timezone', function () {
45+
$date = tick('2023-01-01', 'America/New_York');
46+
expect($date->toDateTime()->getTimezone()->getName())->toBe('America/New_York');
47+
});
48+
49+
test('setTimezone changes timezone', function () {
50+
$date = new Date();
51+
$date->setTimezone('America/New_York');
52+
expect($date->toDateTime()->getTimezone()->getName())->toBe('America/New_York');
53+
});
54+
55+
test('setTimezone throws exception for invalid timezone', function () {
56+
$date = new Date();
57+
expect(fn() => $date->setTimezone('Invalid/Timezone'))->toThrow(\Exception::class);
58+
});

tests/date-comparison.test.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
use Leaf\Date;
4+
5+
test('isBefore checks if date is before another date', function () {
6+
$date = new Date();
7+
$date->tick('2023-01-01');
8+
9+
expect($date->isBefore('2023-01-02'))->toBeTrue();
10+
expect($date->isBefore('2022-12-31'))->toBeFalse();
11+
12+
// Test with DateTime
13+
expect($date->isBefore(new DateTime('2023-01-02')))->toBeTrue();
14+
15+
// Test with Date
16+
$otherDate = new Date();
17+
$otherDate->tick('2023-01-02');
18+
expect($date->isBefore($otherDate))->toBeTrue();
19+
});
20+
21+
test('isAfter checks if date is after another date', function () {
22+
$date = new Date();
23+
$date->tick('2023-01-02');
24+
25+
expect($date->isAfter('2023-01-01'))->toBeTrue();
26+
expect($date->isAfter('2023-01-03'))->toBeFalse();
27+
28+
// Test with DateTime
29+
expect($date->isAfter(new DateTime('2023-01-01')))->toBeTrue();
30+
31+
// Test with Date
32+
$otherDate = new Date();
33+
$otherDate->tick('2023-01-01');
34+
expect($date->isAfter($otherDate))->toBeTrue();
35+
});
36+
37+
test('isBetween checks if date is between two dates', function () {
38+
$date = new Date();
39+
$date->tick('2023-01-02');
40+
41+
expect($date->isBetween('2023-01-01', '2023-01-03'))->toBeTrue();
42+
expect($date->isBetween('2023-01-02', '2023-01-03'))->toBeFalse(); // Equal to first date
43+
expect($date->isBetween('2023-01-01', '2023-01-02'))->toBeFalse(); // Equal to second date
44+
});
45+
46+
test('isBetweenOrEqual checks if date is between or equal to two dates', function () {
47+
$date = new Date();
48+
$date->tick('2023-01-02');
49+
50+
expect($date->isBetweenOrEqual('2023-01-01', '2023-01-03'))->toBeTrue();
51+
expect($date->isBetweenOrEqual('2023-01-02', '2023-01-03'))->toBeTrue(); // Equal to first date
52+
expect($date->isBetweenOrEqual('2023-01-01', '2023-01-02'))->toBeTrue(); // Equal to second date
53+
});
54+
55+
test('isSame checks if date is the same as another date', function () {
56+
$date = new Date();
57+
$date->tick('2023-01-01 12:00:00');
58+
59+
expect($date->isSame('2023-01-01 12:00:00'))->toBeTrue();
60+
expect($date->isSame('2023-01-01 12:00:01'))->toBeFalse();
61+
62+
// Test with DateTime
63+
expect($date->isSame(new DateTime('2023-01-01 12:00:00')))->toBeTrue();
64+
65+
// Test with Date
66+
$otherDate = new Date();
67+
$otherDate->tick('2023-01-01 12:00:00');
68+
expect($date->isSame($otherDate))->toBeTrue();
69+
});
70+
71+
test('isSameDay checks if date is the same day as another date', function () {
72+
$date = new Date();
73+
$date->tick('2023-01-01 12:00:00');
74+
75+
expect($date->isSameDay('2023-01-01 15:30:00'))->toBeTrue();
76+
expect($date->isSameDay('2023-01-02 12:00:00'))->toBeFalse();
77+
78+
// Test with DateTime
79+
expect($date->isSameDay(new DateTime('2023-01-01 15:30:00')))->toBeTrue();
80+
81+
// Test with Date
82+
$otherDate = new Date();
83+
$otherDate->tick('2023-01-01 15:30:00');
84+
expect($date->isSameDay($otherDate))->toBeTrue();
85+
});
86+
87+
test('isSameMonth checks if date is the same month as another date', function () {
88+
$date = new Date();
89+
$date->tick('2023-01-15');
90+
91+
expect($date->isSameMonth('2023-01-01'))->toBeTrue();
92+
expect($date->isSameMonth('2023-02-15'))->toBeFalse();
93+
94+
// Test with DateTime
95+
expect($date->isSameMonth(new DateTime('2023-01-01')))->toBeTrue();
96+
97+
// Test with Date
98+
$otherDate = new Date();
99+
$otherDate->tick('2023-01-01');
100+
expect($date->isSameMonth($otherDate))->toBeTrue();
101+
});
102+
103+
test('isSameYear checks if date is the same year as another date', function () {
104+
$date = new Date();
105+
$date->tick('2023-06-15');
106+
107+
expect($date->isSameYear('2023-01-01'))->toBeTrue();
108+
expect($date->isSameYear('2022-06-15'))->toBeFalse();
109+
110+
// Test with DateTime
111+
expect($date->isSameYear(new DateTime('2023-01-01')))->toBeTrue();
112+
113+
// Test with Date
114+
$otherDate = new Date();
115+
$otherDate->tick('2023-01-01');
116+
expect($date->isSameYear($otherDate))->toBeTrue();
117+
});
118+
119+
test('isLeapYear checks if year is a leap year', function () {
120+
$date = new Date();
121+
122+
$date->tick('2020-01-01'); // Leap year
123+
expect($date->isLeapYear())->toBeTrue();
124+
125+
$date->tick('2023-01-01'); // Not a leap year
126+
expect($date->isLeapYear())->toBeFalse();
127+
});
128+
129+
test('isDateTime checks if value is a DateTime object', function () {
130+
$date = new Date();
131+
132+
expect($date->isDateTime(new DateTime()))->toBeTrue();
133+
expect($date->isDateTime('2023-01-01'))->toBeFalse();
134+
expect($date->isDateTime($date))->toBeFalse();
135+
});

tests/date-formatting.test.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Leaf\Date;
4+
5+
test('format returns formatted date string', function () {
6+
$date = new Date();
7+
$date->tick('2023-05-15 12:30:45');
8+
9+
expect($date->format('YYYY-MM-DD'))->toBe('2023-05-15');
10+
expect($date->format('HH:mm:ss'))->toBe('12:30:45');
11+
expect($date->format('YYYY-MM-DD HH:mm:ss'))->toBe('2023-05-15 12:30:45');
12+
expect($date->format('MMM D, YYYY'))->toBe('May 15, 2023');
13+
});
14+
15+
test('toDateTimeString returns formatted datetime string', function () {
16+
$date = new Date();
17+
$date->tick('2023-05-15 12:30:45');
18+
19+
expect($date->toDateTimeString())->toBe('2023-05-15 12:30:45');
20+
});
21+
22+
test('toDateString returns formatted date string', function () {
23+
$date = new Date();
24+
$date->tick('2023-05-15 12:30:45');
25+
26+
expect($date->toDateString())->toBe('2023-05-15');
27+
});
28+
29+
test('toTimeString returns formatted time string', function () {
30+
$date = new Date();
31+
$date->tick('2023-05-15 12:30:45');
32+
33+
expect($date->toTimeString())->toBe('12:30:45');
34+
});
35+
36+
test('toIsoString returns ISO formatted string', function () {
37+
$date = new Date();
38+
$date->tick('2023-05-15 12:30:45');
39+
40+
// The timezone offset will depend on the server's timezone, so we'll just check the format
41+
expect($date->toIsoString())->toMatch('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}$/');
42+
});
43+
44+
test('from returns relative time string', function () {
45+
$date = new Date();
46+
$date->tick('2023-01-01');
47+
48+
$fromDate = new Date();
49+
$fromDate->tick('2022-01-01');
50+
51+
expect($date->from('2022-01-01'))->toContain('year');
52+
expect($date->from('2022-01-01', true))->not->toContain('ago');
53+
});
54+
55+
test('fromNow returns relative time from now', function () {
56+
$date = new Date();
57+
$date->tick('-1 day');
58+
59+
expect($date->fromNow())->toContain('ago');
60+
});
61+
62+
test('toNow is an alias for fromNow', function () {
63+
$date = new Date();
64+
$date->tick('-1 day');
65+
66+
expect($date->toNow())->toBe($date->fromNow());
67+
});
68+
69+
test('toDateTime returns DateTime object', function () {
70+
$date = new Date();
71+
expect($date->toDateTime())->toBeInstanceOf(DateTime::class);
72+
});
73+
74+
test('toTimestamp returns Unix timestamp', function () {
75+
$date = new Date();
76+
$date->tick('2023-01-01 00:00:00');
77+
78+
expect($date->toTimestamp())->toBe(strtotime('2023-01-01 00:00:00'));
79+
});

0 commit comments

Comments
 (0)