| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group formatting |
| | 5 | * @ticket 38773 |
| | 6 | */ |
| | 7 | class Tests_Formatting_HumanTimeDiff extends WP_UnitTestCase { |
| | 8 | |
| | 9 | /** |
| | 10 | * @group formatting |
| | 11 | * @ticket 38773 |
| | 12 | * @dataProvider data_test_human_time_diff |
| | 13 | */ |
| | 14 | function test_human_time_diff( $expected, $stopdate, $message ) { |
| | 15 | $startdate = new DateTime( '2016-01-01 12:00:00' ); |
| | 16 | $this->assertEquals( $expected, human_time_diff( $startdate->format( 'U' ), $stopdate->format( 'U' ) ), $message ); |
| | 17 | } |
| | 18 | |
| | 19 | // Data for test_human_time_diff. |
| | 20 | function data_test_human_time_diff() { |
| | 21 | return array( |
| | 22 | array( |
| | 23 | '5 mins', |
| | 24 | new DateTime( '2016-01-01 12:05:00' ), |
| | 25 | 'Test a difference of 5 minutes.', |
| | 26 | ), |
| | 27 | array( |
| | 28 | '1 hour', |
| | 29 | new DateTime( '2016-01-01 13:00:00' ), |
| | 30 | 'Test a difference of 1 hour.', |
| | 31 | ), |
| | 32 | array( |
| | 33 | '2 days', |
| | 34 | new DateTime( '2016-01-03 12:00:00' ), |
| | 35 | 'Test a difference of 2 days.', |
| | 36 | ), |
| | 37 | array( |
| | 38 | '2 hours', |
| | 39 | new DateTime( '2016-01-01 14:29:59' ), |
| | 40 | 'Test a difference of 2 hours, 29 minutes and 59 seconds - should round down to 2 hours.', |
| | 41 | ), |
| | 42 | array( |
| | 43 | '3 hours', |
| | 44 | new DateTime( '2016-01-01 14:30:00' ), |
| | 45 | 'Test a difference of 2 hours and 30 minutes - should round up to 3 hours.', |
| | 46 | ), |
| | 47 | array( |
| | 48 | '2 months', |
| | 49 | new DateTime( '2016-02-15 12:00:00' ), |
| | 50 | 'Test a difference of 1 month and 15 days - should round up to 2 months.', |
| | 51 | ), |
| | 52 | array( |
| | 53 | '1 month', |
| | 54 | new DateTime( '2016-02-14 12:00:00' ), |
| | 55 | 'Test a difference of 1 month and 14 days - should round down to 1 month.', |
| | 56 | ), |
| | 57 | array( |
| | 58 | '3 years', |
| | 59 | new DateTime( '2018-07-02 12:00:00' ), |
| | 60 | 'Test a difference of 2 years 6 months and 1 day, should round up to 3 years.', |
| | 61 | ), |
| | 62 | array( |
| | 63 | '2 years', |
| | 64 | new DateTime( '2018-07-01 12:00:00' ), |
| | 65 | 'Test a difference of 2 years 6 months, should round down to 2 years.', |
| | 66 | ), |
| | 67 | ); |
| | 68 | } |
| | 69 | } |