Skip to content

Commit d2ad6fc

Browse files
authored
Merge pull request #6753 from kenjis/refactor-date-now
refactor: make now() testable
2 parents f38806b + ff0b36c commit d2ad6fc

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

system/Helpers/date_helper.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12+
use CodeIgniter\I18n\Time;
13+
1214
// CodeIgniter Date Helpers
1315

1416
if (! function_exists('now')) {
@@ -18,20 +20,29 @@
1820
* Returns time() based on the timezone parameter or on the
1921
* app_timezone() setting
2022
*
21-
* @param string $timezone
22-
*
2323
* @throws Exception
2424
*/
2525
function now(?string $timezone = null): int
2626
{
2727
$timezone = empty($timezone) ? app_timezone() : $timezone;
2828

2929
if ($timezone === 'local' || $timezone === date_default_timezone_get()) {
30-
return time();
30+
$time = Time::now();
31+
32+
return $time->getTimestamp();
3133
}
3234

33-
$datetime = new DateTime('now', new DateTimeZone($timezone));
34-
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
35+
$time = Time::now($timezone);
36+
sscanf(
37+
$time->format('j-n-Y G:i:s'),
38+
'%d-%d-%d %d:%d:%d',
39+
$day,
40+
$month,
41+
$year,
42+
$hour,
43+
$minute,
44+
$second
45+
);
3546

3647
return mktime($hour, $minute, $second, $month, $day, $year);
3748
}

tests/system/Helpers/DateHelperTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Helpers;
1313

14+
use CodeIgniter\I18n\Time;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use DateTimeZone;
1617

@@ -29,13 +30,24 @@ protected function setUp(): void
2930

3031
public function testNowDefault()
3132
{
32-
$this->assertCloseEnough(now(), time()); // close enough
33+
Time::setTestNow('June 20, 2022', 'America/Chicago');
34+
35+
$this->assertSame(now(), 1_655_701_200);
36+
37+
Time::setTestNow();
3338
}
3439

3540
public function testNowSpecific()
3641
{
42+
Time::setTestNow('June 20, 2022', 'America/Chicago');
43+
3744
// Chicago should be two hours ahead of Vancouver
38-
$this->assertCloseEnough(7200, now('America/Chicago') - now('America/Vancouver'));
45+
$this->assertSame(
46+
7200,
47+
now('America/Chicago') - now('America/Vancouver')
48+
);
49+
50+
Time::setTestNow();
3951
}
4052

4153
public function testTimezoneSelectDefault()
@@ -68,7 +80,10 @@ public function testTimezoneSelectSpecific()
6880

6981
$expected .= ("</select>\n");
7082

71-
$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion));
83+
$this->assertSame(
84+
$expected,
85+
timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion)
86+
);
7287
}
7388

7489
public function testTimezoneSelectSingle()
@@ -86,6 +101,9 @@ public function testTimezoneSelectSingle()
86101

87102
$expected .= ("</select>\n");
88103

89-
$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion, $country));
104+
$this->assertSame(
105+
$expected,
106+
timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion, $country)
107+
);
90108
}
91109
}

0 commit comments

Comments
 (0)