- Refactor WP DateTime helper

- Add a unit test helper to stub out Wordpress functions
This commit is contained in:
Tautvidas Sipavičius
2016-05-16 17:44:26 +03:00
parent f8b1e153be
commit 61df4899cd
5 changed files with 134 additions and 16 deletions

View File

@ -404,7 +404,7 @@ class Menu {
$data['roles']['mailpoet_all'] = __('In any WordPress role'); $data['roles']['mailpoet_all'] = __('In any WordPress role');
$date_time = new DateTime(); $date_time = new DateTime();
$data['current_date'] = $date_time->getCurrentDate(DateTime::INTERNAL_DATE_FORMAT); $data['current_date'] = $date_time->getCurrentDate(DateTime::DEFAULT_DATE_FORMAT);
$data['current_time'] = $date_time->getCurrentTime(); $data['current_time'] = $date_time->getCurrentTime();
$data['schedule_time_of_day'] = $date_time->getTimeInterval( $data['schedule_time_of_day'] = $date_time->getTimeInterval(
'00:00:00', '00:00:00',

View File

@ -3,22 +3,22 @@ namespace MailPoet\WP;
class DateTime { class DateTime {
const INTERNAL_DATE_FORMAT = 'Y-m-d'; const DEFAULT_DATE_FORMAT = 'Y-m-d';
const INTERNAL_TIME_FORMAT = 'H:i:s'; const DEFAULT_TIME_FORMAT = 'H:i:s';
const INTERNAL_DATE_TIME_FORMAT = 'Y-m-d H:i:s'; const DEFAULT_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
function __construct() { function __construct() {
} }
function getTimeFormat() { function getTimeFormat() {
$time_format = get_option('time_format'); $time_format = get_option('time_format');
if (empty($time_format)) $time_format = self::INTERNAL_TIME_FORMAT; if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT;
return $time_format; return $time_format;
} }
function getDateFormat() { function getDateFormat() {
$date_format = get_option('date_format'); $date_format = get_option('date_format');
if (empty($date_format)) $date_format = self::INTERNAL_DATE_FORMAT; if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT;
return $date_format; return $date_format;
} }
@ -32,15 +32,15 @@ class DateTime {
return $this->getCurrentTime($format); return $this->getCurrentTime($format);
} }
function getTime($time, $format=false) { function formatTime($timestamp, $format=false) {
if (empty($format)) $format = $this->getTimeFormat(); if (empty($format)) $format = $this->getTimeFormat();
return date($format, $time); return date($format, $timestamp);
} }
/** /**
* Generates a list of time strings within an interval, * Generates a list of time strings within an interval,
* formatted and mapped from INTERNAL_TIME_FORMAT to Wordpress time strings. * formatted and mapped from DEFAULT_TIME_FORMAT to Wordpress time strings.
*/ */
function getTimeInterval( function getTimeInterval(
$start_time='00:00:00', $start_time='00:00:00',
@ -49,15 +49,15 @@ class DateTime {
) { ) {
$steps = array(); $steps = array();
$internal_time = $start_time; $formatted_time = $start_time;
$timestamp = strtotime($internal_time); $timestamp = strtotime($formatted_time);
for ($step = 0; $step < $total_steps; $step += 1) { for ($step = 0; $step < $total_steps; $step += 1) {
$wordpress_time = $this->getTime($timestamp); $formatted_time = $this->formatTime($timestamp, self::DEFAULT_TIME_FORMAT);
$steps[$internal_time] = $wordpress_time; $label_time = $this->formatTime($timestamp);
$steps[$formatted_time] = $label_time;
$timestamp = strtotime($time_step, $timestamp); $timestamp = strtotime($time_step, $timestamp);
$internal_time = $this->getTime($timestamp, self::INTERNAL_TIME_FORMAT);
} }
return $steps; return $steps;

View File

@ -0,0 +1,35 @@
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class WordPress extends \Codeception\Module
{
private static $functions_to_intercept = array();
static function interceptFunction($function_name, $callback) {
self::$functions_to_intercept[$function_name] = $callback;
}
static function releaseFunction($function_name) {
unset(self::$functions_to_intercept[$function_name]);
}
static function releaseAllFunctions() {
self::$functions_to_intercept = array();
}
static function getInterceptor($function_name) {
if (isset(self::$functions_to_intercept[$function_name]))
return self::$functions_to_intercept[$function_name];
}
}
// WP function overrides for \MailPoet namespace go here
namespace MailPoet\WP;
function get_option($key) {
if ($callback = \Helper\Wordpress::getInterceptor('get_option'))
return $callback($key);
return \get_option($key);
}

View File

@ -7,3 +7,4 @@ modules:
enabled: enabled:
- Asserts - Asserts
- \Helper\Unit - \Helper\Unit
- \Helper\WordPress

View File

@ -0,0 +1,82 @@
<?php
use \Helper\WordPress as WordPressHelper;
use \MailPoet\WP\DateTime;
class DateTimeTest extends MailPoetTest {
function _before() {
$this->date_time = new DateTime();
}
function testGetTimeFormat() {
WordPressHelper::interceptFunction('get_option', function($key) {
return 'H:i';
});
expect($this->date_time->getTimeFormat())->equals('H:i');
WordPressHelper::interceptFunction('get_option', function($key) {
return '';
});
expect($this->date_time->getTimeFormat())->equals('H:i:s');
}
function testGetDateFormat() {
WordPressHelper::interceptFunction('get_option', function($key) {
return 'm-d';
});
expect($this->date_time->getDateFormat())->equals('m-d');
WordPressHelper::interceptFunction('get_option', function($key) {
return '';
});
expect($this->date_time->getDateFormat())->equals('Y-m-d');
}
function testGetCurrentDate() {
expect($this->date_time->getCurrentDate("Y-m"))->equals(date("Y-m"));
}
function testGetCurrentTime() {
expect($this->date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/');
}
function testFormatTime() {
$timestamp = 1234567;
$format = "H:i:s";
expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
}
function testTimeInterval() {
$one_hour_interval = array_keys($this->date_time->getTimeInterval(
'00:00:00',
'+1 hour',
$total_steps=5
));
$one_hour_expected = array(
'00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00');
expect($one_hour_interval)->equals($one_hour_expected);
$quarter_hour_interval = array_keys($this->date_time->getTimeInterval(
'00:00:00',
'+15 minute',
$total_steps=5
));
$quarter_hour_expected = array(
'00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00',
);
expect($quarter_hour_interval)->equals($quarter_hour_expected);
$offset_start_time_interval = array_keys($this->date_time->getTimeInterval(
'03:00:00',
'+1 hour',
$total_steps=5
));
$offset_start_time_expected = array(
'03:00:00', '04:00:00', '05:00:00', '06:00:00', '07:00:00',
);
expect($offset_start_time_interval)->equals($offset_start_time_expected);
}
function _afterStep() {
Wordpress::releaseAllFunctions();
}
}