From 33755709e4acde4d430eb70059adbb91443f7444 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 7 Jul 2017 17:07:40 +0000 Subject: [PATCH] Date/Time: Add unit tests for the `human_time_diff` function. Verify that `human_time_diff` works as expected for minute, hour and day intervals. Test that rounding works as expected when the time difference is near the rounding margin. Props SGr33n. Fixes #38773. git-svn-id: https://develop.svn.wordpress.org/trunk@41018 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/formatting/HumanTimeDiff.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/phpunit/tests/formatting/HumanTimeDiff.php diff --git a/tests/phpunit/tests/formatting/HumanTimeDiff.php b/tests/phpunit/tests/formatting/HumanTimeDiff.php new file mode 100644 index 0000000000..da1ad2dcfc --- /dev/null +++ b/tests/phpunit/tests/formatting/HumanTimeDiff.php @@ -0,0 +1,69 @@ +assertEquals( $expected, human_time_diff( $startdate->format( 'U' ), $stopdate->format( 'U' ) ), $message ); + } + + // Data for test_human_time_diff. + function data_test_human_time_diff() { + return array( + array( + '5 mins', + new DateTime( '2016-01-01 12:05:00' ), + 'Test a difference of 5 minutes.', + ), + array( + '1 hour', + new DateTime( '2016-01-01 13:00:00' ), + 'Test a difference of 1 hour.', + ), + array( + '2 days', + new DateTime( '2016-01-03 12:00:00' ), + 'Test a difference of 2 days.', + ), + array( + '2 hours', + new DateTime( '2016-01-01 14:29:59' ), + 'Test a difference of 2 hours, 29 minutes and 59 seconds - should round down to 2 hours.', + ), + array( + '3 hours', + new DateTime( '2016-01-01 14:30:00' ), + 'Test a difference of 2 hours and 30 minutes - should round up to 3 hours.', + ), + array( + '2 months', + new DateTime( '2016-02-15 12:00:00' ), + 'Test a difference of 1 month and 15 days - should round up to 2 months.', + ), + array( + '1 month', + new DateTime( '2016-02-14 12:00:00' ), + 'Test a difference of 1 month and 14 days - should round down to 1 month.', + ), + array( + '3 years', + new DateTime( '2018-07-02 12:00:00' ), + 'Test a difference of 2 years 6 months and 1 day, should round up to 3 years.', + ), + array( + '2 years', + new DateTime( '2018-07-01 12:00:00' ), + 'Test a difference of 2 years 6 months, should round down to 2 years.', + ), + ); + } +}