Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make now() testable #6753

Merged
merged 3 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions system/Helpers/date_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\I18n\Time;

// CodeIgniter Date Helpers

if (! function_exists('now')) {
Expand All @@ -18,20 +20,29 @@
* Returns time() based on the timezone parameter or on the
* app_timezone() setting
*
* @param string $timezone
*
* @throws Exception
*/
function now(?string $timezone = null): int
{
$timezone = empty($timezone) ? app_timezone() : $timezone;

if ($timezone === 'local' || $timezone === date_default_timezone_get()) {
return time();
$time = Time::now();

return $time->getTimestamp();
kenjis marked this conversation as resolved.
Show resolved Hide resolved
}

$datetime = new DateTime('now', new DateTimeZone($timezone));
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
$time = Time::now($timezone);
sscanf(
$time->format('j-n-Y G:i:s'),
'%d-%d-%d %d:%d:%d',
$day,
$month,
$year,
$hour,
$minute,
$second
);

return mktime($hour, $minute, $second, $month, $day, $year);
}
Expand Down
26 changes: 22 additions & 4 deletions tests/system/Helpers/DateHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Helpers;

use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;
use DateTimeZone;

Expand All @@ -27,13 +28,24 @@ protected function setUp(): void

public function testNowDefault()
{
$this->assertCloseEnough(now(), time()); // close enough
Time::setTestNow('June 20, 2022', 'America/Chicago');

$this->assertSame(now(), 1_655_701_200);

Time::setTestNow();
}

public function testNowSpecific()
{
Time::setTestNow('June 20, 2022', 'America/Chicago');

// Chicago should be two hours ahead of Vancouver
$this->assertCloseEnough(7200, now('America/Chicago') - now('America/Vancouver'));
$this->assertSame(
7200,
now('America/Chicago') - now('America/Vancouver')
);

Time::setTestNow();
}

public function testTimezoneSelectDefault()
Expand Down Expand Up @@ -66,7 +78,10 @@ public function testTimezoneSelectSpecific()

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

$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion));
$this->assertSame(
$expected,
timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion)
);
}

public function testTimezoneSelectSingle()
Expand All @@ -84,6 +99,9 @@ public function testTimezoneSelectSingle()

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

$this->assertSame($expected, timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion, $country));
$this->assertSame(
$expected,
timezone_select('custom-select', 'Asia/Jakarta', $spesificRegion, $country)
);
}
}