Skip to content

Commit 06fdf51

Browse files
committed
tests: added test for timezone_select function
1 parent 12e2a8d commit 06fdf51

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/system/Helpers/DateHelperTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Helpers;
1313

1414
use CodeIgniter\Test\CIUnitTestCase;
15+
use DateTimeZone;
1516

1617
/**
1718
* @internal
@@ -34,4 +35,55 @@ public function testNowSpecific()
3435
// Chicago should be two hours ahead of Vancouver
3536
$this->assertCloseEnough(7200, now('America/Chicago') - now('America/Vancouver'));
3637
}
38+
39+
public function testTimezoneSelectDefault()
40+
{
41+
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL, null);
42+
43+
$expected = "<select name='timezone' class='custom-select'>" . PHP_EOL;
44+
45+
foreach ($timezones as $timezone) {
46+
$selected = ($timezone === 'Asia/Jakarta') ? 'selected' : '';
47+
$expected .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
48+
}
49+
50+
$expected .= ('</select>' . PHP_EOL);
51+
52+
$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta'));
53+
}
54+
55+
public function testTimezoneSelectSpecific()
56+
{
57+
$spesificRegion = DateTimeZone::ASIA;
58+
$timezones = DateTimeZone::listIdentifiers($spesificRegion, null);
59+
60+
$expected = "<select name='timezone' class='custom-select'>" . PHP_EOL;
61+
62+
foreach ($timezones as $timezone) {
63+
$selected = ($timezone === 'Asia/Jakarta') ? 'selected' : '';
64+
$expected .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
65+
}
66+
67+
$expected .= ('</select>' . PHP_EOL);
68+
69+
$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion));
70+
}
71+
72+
public function testTimezoneSelectSingle()
73+
{
74+
$spesificRegion = DateTimeZone::PER_COUNTRY;
75+
$country = 'ID';
76+
$timezones = DateTimeZone::listIdentifiers($spesificRegion, $country);
77+
78+
$expected = "<select name='timezone' class='custom-select'>" . PHP_EOL;
79+
80+
foreach ($timezones as $timezone) {
81+
$selected = ($timezone === 'Asia/Jakarta') ? 'selected' : '';
82+
$expected .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
83+
}
84+
85+
$expected .= ('</select>' . PHP_EOL);
86+
87+
$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion, $country));
88+
}
3789
}

0 commit comments

Comments
 (0)