1 | <?php |
||||
2 | |||||
3 | // File: tests/Unit/DateConverterTest.php |
||||
4 | |||||
5 | use Pratiksh\Nepalidate\Tests\Unit\Doubles\DateConverterTestDouble; |
||||
6 | |||||
7 | test('date converter has correct bs month in english', function () { |
||||
8 | $converter = new DateConverterTestDouble(); |
||||
9 | |||||
10 | expect($converter->getBSMonthInEnglish(1))->toBe('Baisakh') |
||||
11 | ->and($converter->getBSMonthInEnglish(6))->toBe('Ashoj') |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
12 | ->and($converter->getBSMonthInEnglish(12))->toBe('Chaitra'); |
||||
13 | }); |
||||
14 | |||||
15 | test('date converter has correct bs month in nepali', function () { |
||||
16 | $converter = new DateConverterTestDouble(); |
||||
17 | |||||
18 | expect($converter->getBSMonthInNepali(1))->toBe('वैशाख') |
||||
19 | ->and($converter->getBSMonthInNepali(6))->toBe('असोज') |
||||
0 ignored issues
–
show
$converter->getBSMonthInNepali(6) of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
20 | ->and($converter->getBSMonthInNepali(12))->toBe('चैत'); |
||||
21 | }); |
||||
22 | |||||
23 | test('date converter has correct ad month in nepali', function () { |
||||
24 | $converter = new DateConverterTestDouble(); |
||||
25 | |||||
26 | expect($converter->getADMonthInNepali(1))->toBe('जनवरी') |
||||
27 | ->and($converter->getADMonthInNepali(6))->toBe('जून') |
||||
0 ignored issues
–
show
$converter->getADMonthInNepali(6) of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
28 | ->and($converter->getADMonthInNepali(12))->toBe('दिसेम्बर'); |
||||
29 | }); |
||||
30 | |||||
31 | test('date converter has correct day of week in nepali', function () { |
||||
32 | $converter = new DateConverterTestDouble(); |
||||
33 | |||||
34 | expect($converter->getDayOfWeekInNepali(1))->toBe('आइतवार') |
||||
35 | ->and($converter->getDayOfWeekInNepali(4))->toBe('बुधवार') |
||||
0 ignored issues
–
show
$converter->getDayOfWeekInNepali(4) of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
36 | ->and($converter->getDayOfWeekInNepali(7))->toBe('शनिवार'); |
||||
37 | }); |
||||
38 | |||||
39 | test('date converter has correct day of week in english', function () { |
||||
40 | $converter = new DateConverterTestDouble(); |
||||
41 | |||||
42 | expect($converter->getDayOfWeekInEnglish(1))->toBe('Sunday') |
||||
43 | ->and($converter->getDayOfWeekInEnglish(4))->toBe('Wednesday') |
||||
0 ignored issues
–
show
$converter->getDayOfWeekInEnglish(4) of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
44 | ->and($converter->getDayOfWeekInEnglish(7))->toBe('Saturday'); |
||||
45 | }); |
||||
46 | |||||
47 | test('date converter has correct numbers in nepali', function () { |
||||
48 | $converter = new DateConverterTestDouble(); |
||||
49 | |||||
50 | expect($converter->getNumbersInNepali(0))->toBe('०') |
||||
51 | ->and($converter->getNumbersInNepali(5))->toBe('५') |
||||
0 ignored issues
–
show
$converter->getNumbersInNepali(5) of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
52 | ->and($converter->getNumbersInNepali(9))->toBe('९'); |
||||
53 | }); |
||||
54 | |||||
55 | test('date converter formats nepali numbers correctly', function () { |
||||
56 | $converter = new DateConverterTestDouble(); |
||||
57 | |||||
58 | expect($converter->formattedNepaliNumber('123'))->toBe('१२३') |
||||
59 | ->and($converter->formattedNepaliNumber('2080'))->toBe('२०८०') |
||||
0 ignored issues
–
show
$converter->formattedNepaliNumber('2080') of type string is incompatible with the type Pest\TValue expected by parameter $value of Pest\Expectation::and() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
60 | ->and($converter->formattedNepaliNumber('9876543210'))->toBe('९८७६५४३२१०'); |
||||
61 | }); |
||||
62 | |||||
63 | test('date converter validates english date range correctly', function () { |
||||
64 | $converter = new DateConverterTestDouble(); |
||||
65 | |||||
66 | // Valid dates should pass should return true |
||||
67 | expect(fn () => $converter->isInRangeEng(1944, 1, 1))->not->toBeTrue(); |
||||
68 | expect(fn () => $converter->isInRangeEng(2033, 12, 31))->not->toBeTrue(); |
||||
69 | |||||
70 | // Invalid dates should return false |
||||
71 | expect($converter->isInRangeEng(1943, 12, 31))->toBeFalse(); |
||||
72 | expect($converter->isInRangeEng(2034, 1, 1))->toBeFalse(); |
||||
73 | expect($converter->isInRangeEng(2000, 0, 15))->toBeFalse(); |
||||
74 | expect($converter->isInRangeEng(2000, 13, 15))->toBeFalse(); |
||||
75 | expect($converter->isInRangeEng(2000, 6, 0))->toBeFalse(); |
||||
76 | expect($converter->isInRangeEng(2000, 6, 32))->toBeFalse(); |
||||
77 | }); |
||||
78 | |||||
79 | test('date converter validates nepali date range correctly', function () { |
||||
80 | $converter = new DateConverterTestDouble(); |
||||
81 | |||||
82 | // Valid dates |
||||
83 | expect($converter->isInRangeNep(2000, 1, 1))->toBeTrue(); |
||||
84 | expect($converter->isInRangeNep(2089, 12, 30))->toBeTrue(); |
||||
85 | |||||
86 | // Invalid dates |
||||
87 | expect($converter->isInRangeNep(1999, 12, 30))->toBeFalse(); |
||||
88 | expect($converter->isInRangeNep(2090, 1, 1))->toBeFalse(); |
||||
89 | expect($converter->isInRangeNep(2080, 0, 15))->toBeFalse(); |
||||
90 | expect($converter->isInRangeNep(2080, 13, 15))->toBeFalse(); |
||||
91 | expect($converter->isInRangeNep(2080, 6, 0))->toBeFalse(); |
||||
92 | expect($converter->isInRangeNep(2080, 6, 33))->toBeFalse(); |
||||
93 | }); |
||||
94 | |||||
95 | test('date converter correctly identifies leap years', function () { |
||||
96 | $converter = new DateConverterTestDouble(); |
||||
97 | |||||
98 | // Leap years |
||||
99 | expect($converter->isLeapYear(2000))->toBeTrue(); // Divisible by 400 |
||||
100 | expect($converter->isLeapYear(2004))->toBeTrue(); // Divisible by 4 |
||||
101 | expect($converter->isLeapYear(2020))->toBeTrue(); // Divisible by 4 |
||||
102 | |||||
103 | // Non-leap years |
||||
104 | expect($converter->isLeapYear(1900))->toBeFalse(); // Divisible by 100 but not by 400 |
||||
105 | expect($converter->isLeapYear(2100))->toBeFalse(); // Divisible by 100 but not by 400 |
||||
106 | expect($converter->isLeapYear(2021))->toBeFalse(); // Not divisible by 4 |
||||
107 | expect($converter->isLeapYear(2022))->toBeFalse(); // Not divisible by 4 |
||||
108 | }); |
||||
109 | |||||
110 | test('date converter calculates total english days correctly', function () { |
||||
111 | $converter = new DateConverterTestDouble(); |
||||
112 | |||||
113 | // Starting date used in calculations (1944-01-01 corresponds to day 1) |
||||
114 | $startDate = $converter->getTotalEnglishDays(1944, 1, 1); |
||||
115 | expect($startDate)->toBe(1); |
||||
116 | |||||
117 | // One day later |
||||
118 | expect($converter->getTotalEnglishDays(1944, 1, 2))->toBe(2); |
||||
119 | |||||
120 | // Next month (31 days after Jan 1) |
||||
121 | expect($converter->getTotalEnglishDays(1944, 2, 1))->toBe(32); |
||||
122 | |||||
123 | // Leap year Feb 29 |
||||
124 | expect($converter->getTotalEnglishDays(1944, 2, 29))->toBe(60); |
||||
125 | |||||
126 | // One year later (1945-01-01), 1944 is leap year so 366 days |
||||
127 | expect($converter->getTotalEnglishDays(1945, 1, 1))->toBe(367); |
||||
128 | |||||
129 | // Check a date far in the future |
||||
130 | $daysFrom1944To2022 = 0; |
||||
131 | for ($year = 1944; $year < 2022; $year++) { |
||||
132 | $daysInYear = $converter->isLeapYear($year) ? 366 : 365; |
||||
133 | $daysFrom1944To2022 += $daysInYear; |
||||
134 | } |
||||
135 | // 2022-01-01 |
||||
136 | expect($converter->getTotalEnglishDays(2022, 1, 1))->toBe($daysFrom1944To2022 + 1); |
||||
137 | }); |
||||
138 | |||||
139 | test('date converter calculates total nepali days correctly', function () { |
||||
140 | $converter = new DateConverterTestDouble(); |
||||
141 | $calendarData = $converter->getCalendarData(); |
||||
142 | |||||
143 | // Starting date used in calculations (2000-01-01 corresponds to day 1) |
||||
144 | $startDate = $converter->getTotalNepaliDays(2000, 1, 1); |
||||
145 | expect($startDate)->toBe(1); |
||||
146 | |||||
147 | // One day later |
||||
148 | expect($converter->getTotalNepaliDays(2000, 1, 2))->toBe(2); |
||||
149 | |||||
150 | // Next month |
||||
151 | $daysInMonth1 = $calendarData[0][1]; // Days in 2000/01 |
||||
152 | expect($converter->getTotalNepaliDays(2000, 2, 1))->toBe($daysInMonth1 + 1); |
||||
153 | |||||
154 | // One year later (2001-01-01) |
||||
155 | $daysIn2000 = 0; |
||||
156 | for ($m = 1; $m <= 12; $m++) { |
||||
157 | $daysIn2000 += $calendarData[0][$m]; |
||||
158 | } |
||||
159 | expect($converter->getTotalNepaliDays(2001, 1, 1))->toBe($daysIn2000 + 1); |
||||
160 | |||||
161 | // Manually calculate days for a date several years later |
||||
162 | $totalDays = 0; |
||||
163 | $targetYear = 2080; |
||||
164 | $targetMonth = 5; |
||||
165 | $targetDay = 15; |
||||
166 | |||||
167 | // Add complete years |
||||
168 | for ($y = 2000; $y < $targetYear; $y++) { |
||||
169 | $yearIndex = $y - 2000; |
||||
170 | for ($m = 1; $m <= 12; $m++) { |
||||
171 | $totalDays += $calendarData[$yearIndex][$m]; |
||||
172 | } |
||||
173 | } |
||||
174 | |||||
175 | // Add complete months in target year |
||||
176 | $yearIndex = $targetYear - 2000; |
||||
177 | for ($m = 1; $m < $targetMonth; $m++) { |
||||
178 | $totalDays += $calendarData[$yearIndex][$m]; |
||||
179 | } |
||||
180 | |||||
181 | // Add days in target month |
||||
182 | $totalDays += $targetDay; |
||||
183 | |||||
184 | expect($converter->getTotalNepaliDays($targetYear, $targetMonth, $targetDay))->toBe($totalDays); |
||||
185 | }); |
||||
186 | |||||
187 | test('calendar data is correctly structured', function () { |
||||
188 | $converter = new DateConverterTestDouble(); |
||||
189 | $calendarData = $converter->getCalendarData(); |
||||
190 | |||||
191 | // Check that years span from 2000 to 2090 |
||||
192 | expect(count($calendarData))->toBe(91); |
||||
193 | expect($calendarData[0][0])->toBe(2000); |
||||
194 | expect($calendarData[90][0])->toBe(2090); |
||||
195 | |||||
196 | // Check that each year has 13 elements (year + 12 months) |
||||
197 | foreach ($calendarData as $yearData) { |
||||
198 | expect(count($yearData))->toBe(13); |
||||
199 | } |
||||
200 | }); |
||||
201 |