1 <?php
defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
8 * @group kohana.core.date
13 * @author BRMatt <matthew@sigswitch.com>
14 * @copyright (c) 2008-2012 Kohana Team
15 * @license http://kohanaframework.org/license
17 class Kohana_DateTest
extends Unittest_TestCase
19 protected $_original_timezone = NULL;
20 protected $default_locale;
23 * Ensures we have a consistant timezone for testing.
25 // @codingStandardsIgnoreStart
26 public function setUp()
27 // @codingStandardsIgnoreEnd
31 $this->_original_timezone
= date_default_timezone_get();
32 $this->default_locale
= setlocale(LC_ALL
, 0);
34 date_default_timezone_set('America/Chicago');
35 setlocale(LC_ALL
, 'en_US.utf8');
39 * Restores original timezone after testing.
41 // @codingStandardsIgnoreStart
42 public function tearDown()
43 // @codingStandardsIgnoreEnd
45 date_default_timezone_set($this->_original_timezone
);
46 setlocale(LC_ALL
, $this->default_locale
);
52 * Provides test data for test_offset()
56 public function provider_offset()
59 array(30600, 'Asia/Calcutta', 'America/Argentina/Buenos_Aires'),
64 * Tests Date::offset()
67 * @dataProvider provider_offset
68 * @covers Date::offset
69 * @param integer $expected Expected offset
70 * @param string $remote Remote TZ
71 * @param string $local Local TZ
72 * @param integer $now Current timestamp
74 public function test_offset($expected, $remote, $local, $now = NULL)
76 $this->assertSame($expected, Date
::offset($remote, $local, $now));
80 * Provides test data for test_date()
84 public function provider_am_pm()
87 // All possible values
113 // ampm doesn't validate the hour, so I don't think we should test it..
114 // test strings are converted
125 * @dataProvider provider_am_pm
126 * @param <type> $hour
127 * @param <type> $expected
129 public function test_am_pm($hour, $expected)
138 * Provides test data for test_adjust()
142 public function provider_adjust()
145 // Might as well test all possibilities
146 array(1, 'am', '01'),
147 array(2, 'am', '02'),
148 array(3, 'am', '03'),
149 array(4, 'am', '04'),
150 array(5, 'am', '05'),
151 array(6, 'am', '06'),
152 array(7, 'am', '07'),
153 array(8, 'am', '08'),
154 array(9, 'am', '09'),
155 array(10, 'am', '10'),
156 array(11, 'am', '11'),
157 array(12, 'am', '00'),
158 array(1, 'pm', '13'),
159 array(2, 'pm', '14'),
160 array(3, 'pm', '15'),
161 array(4, 'pm', '16'),
162 array(5, 'pm', '17'),
163 array(6, 'pm', '18'),
164 array(7, 'pm', '19'),
165 array(8, 'pm', '20'),
166 array(9, 'pm', '21'),
167 array(10, 'pm', '22'),
168 array(11, 'pm', '23'),
169 array(12, 'pm', '12'),
170 // It should also work with strings instead of ints
171 array('10', 'pm', '22'),
172 array('10', 'am', '10'),
180 * @dataProvider provider_adjust
181 * @param integer $hour Hour in 12 hour format
182 * @param string $ampm Either am or pm
183 * @param string $expected Expected result
185 public function test_adjust($hour, $ampm, $expected)
189 Date
::adjust($hour, $ampm)
194 * Provides test data for test_days()
198 public function provider_days()
201 // According to "the rhyme" these should be the same every year
205 array(11, FALSE, 30),
211 array(10, FALSE, 31),
212 // February is such a pain
224 * @dataProvider provider_days
225 * @param integer $month
226 * @param integer $year
227 * @param integer $expected
229 public function test_days($month, $year, $expected)
231 $days = Date
::days($month, $year);
238 // This should be a mirrored array, days => days
239 for ($i = 1; $i <= $expected; ++
$i)
241 $this->assertArrayHasKey($i, $days);
242 // Combining the type check into this saves about 400-500 assertions!
243 $this->assertSame( (string) $i, $days[$i]);
248 * Provides test data for test_formatted_time()
252 public function provider_formatted_time()
255 // Test the default format
256 array('2010-04-16 17:00:00', '5:00PM 16th April 2010'),
257 // Now we use our own format
259 array('01/01/2010 01:00', '1AM 1st January 2010', 'd/m/Y H:i'),
260 // Timezones (see #3902)
261 array('2011-04-01 01:23:45 Antarctica/South_Pole', '2011-04-01 01:23:45', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
262 array('2011-04-01 01:23:45 Antarctica/South_Pole', '2011-03-31 14:23:45 Europe/Paris', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
263 array('2011-04-01 01:23:45 Antarctica/South_Pole', '@1301574225', 'Y-m-d H:i:s e', 'Antarctica/South_Pole'),
268 * Tests Date::formatted_time()
271 * @dataProvider provider_formatted_time
272 * @covers Date::formatted_time
274 * @param string $expected Expected output
275 * @param string|integer $datetime_str The datetime timestamp / string
276 * @param string|null $timestamp_format The output format
277 * @param string|null $timezone The timezone identifier
279 public function test_formatted_time($expected, $datetime_str, $timestamp_format = NULL, $timezone = NULL)
281 $timestamp = Date
::formatted_time($datetime_str, $timestamp_format, $timezone);
283 $this->assertSame($expected, $timestamp);
287 * Provider for test_months()
289 * @return array Test data
291 public function provider_months()
367 * Date::months() should allow the user to specify different format types, defaulting
368 * to a mirrored month number => month number array if format is NULL or unrecognised
371 * @dataProvider provider_months
372 * @covers Date::months
374 public function test_months($expected, $format)
376 $months = Date
::months($format);
378 $this->assertSame($expected, $months);
382 * Provides test data for test_span()
386 public function provider_span()
390 // Test that it must specify an output format
397 // Test that providing only one output just returns that output
408 'years,months,weeks,days,hours,minutes,seconds',
409 array('years' => 0, 'months' => 0, 'weeks' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 30),
412 $time - (60 * 60 * 24 * 782) +
(60 * 25),
414 'years,months,weeks,days,hours,minutes,seconds',
415 array('years' => 2, 'months' => 1, 'weeks' => 3, 'days' => 0, 'hours' => 1, 'minutes' => 28, 'seconds' => 24),
417 // Should be able to compare with the future & that it only uses formats specified
419 $time +
(60 * 60 * 24 * 15) +
(60 * 5),
421 'weeks,days,hours,minutes,seconds',
422 array('weeks' => 2, 'days' => 1, 'hours' => 0, 'minutes' => 5, 'seconds' => 0),
425 // Add a bit of extra time to account for phpunit processing
426 $time +
(14 * 31 * 24* 60 * 60) +
(79 * 80),
429 array('months' => 2, 'years' => 1),
439 * @dataProvider provider_span
440 * @param integer $time1 Time in the past
441 * @param integer $time2 Time to compare against
442 * @param string $output Units to output
443 * @param array $expected Array of $outputs => values
445 public function test_span($time1, $time2, $output, $expected)
449 Date
::span($time1, $time2, $output)
454 * Provides test data to test_fuzzy_span
456 * This test data is provided on the assumption that it
457 * won't take phpunit more than 30 seconds to get the
458 * data from this provider to the test... ;)
460 * @return array Test Data
462 public function provider_fuzzy_span()
467 array('moments ago', $now - 30, $now),
468 array('in moments', $now +
30, $now),
470 array('a few minutes ago', $now - 10*60, $now),
471 array('in a few minutes', $now +
10*60, $now),
473 array('less than an hour ago', $now - 45*60, $now),
474 array('in less than an hour', $now +
45*60, $now),
476 array('a couple of hours ago', $now - 2*60*60, $now),
477 array('in a couple of hours', $now +
2*60*60, $now),
479 array('less than a day ago', $now - 12*60*60, $now),
480 array('in less than a day', $now +
12*60*60, $now),
482 array('about a day ago', $now - 30*60*60, $now),
483 array('in about a day', $now +
30*60*60, $now),
485 array('a couple of days ago', $now - 3*24*60*60, $now),
486 array('in a couple of days', $now +
3*24*60*60, $now),
488 array('less than a week ago', $now - 5*24*60*60, $now),
489 array('in less than a week', $now +
5*24*60*60, $now),
491 array('about a week ago', $now - 9*24*60*60, $now),
492 array('in about a week', $now +
9*24*60*60, $now),
494 array('less than a month ago', $now - 20*24*60*60, $now),
495 array('in less than a month', $now +
20*24*60*60, $now),
497 array('about a month ago', $now - 40*24*60*60, $now),
498 array('in about a month', $now +
40*24*60*60, $now),
500 array('a couple of months ago', $now - 3*30*24*60*60, $now),
501 array('in a couple of months', $now +
3*30*24*60*60, $now),
503 array('less than a year ago', $now - 7*31*24*60*60, $now),
504 array('in less than a year', $now +
7*31*24*60*60, $now),
506 array('about a year ago', $now - 18*31*24*60*60, $now),
507 array('in about a year', $now +
18*31*24*60*60, $now),
509 array('a couple of years ago', $now - 3*12*31*24*60*60, $now),
510 array('in a couple of years', $now +
3*12*31*24*60*60, $now),
512 array('a few years ago', $now - 5*12*31*24*60*60, $now),
513 array('in a few years', $now +
5*12*31*24*60*60, $now),
515 array('about a decade ago', $now - 11*12*31*24*60*60, $now),
516 array('in about a decade', $now +
11*12*31*24*60*60, $now),
518 array('a couple of decades ago', $now - 20*12*31*24*60*60, $now),
519 array('in a couple of decades', $now +
20*12*31*24*60*60, $now),
521 array('several decades ago', $now - 50*12*31*24*60*60, $now),
522 array('in several decades', $now +
50*12*31*24*60*60, $now),
524 array('a long time ago', $now - pow(10,10), $now),
525 array('in a long time', $now +
pow(10,10), $now),
530 * Test of Date::fuzy_span()
533 * @dataProvider provider_fuzzy_span
534 * @param string $expected Expected output
535 * @param integer $timestamp Timestamp to use
536 * @param integer $local_timestamp The local timestamp to use
538 public function test_fuzzy_span($expected, $timestamp, $local_timestamp)
542 Date
::fuzzy_span($timestamp, $local_timestamp)
547 * Provides test data for test_years()
549 * @return array Test Data
551 public function provider_years()
575 * Tests Data::years()
578 * @dataProvider provider_years
580 public function test_years($expected, $start = FALSE, $end = FALSE)
584 Date
::years($start, $end)
588 public function provider_hours()
611 * Test for Date::hours
614 * @dataProvider provider_hours
616 public function test_hours($expected, $step = 1, $long = FALSE, $start = NULL)
620 Date
::hours($step, $long, $start)
625 * Provides test data for test_seconds
627 * @return array Test data
629 public function provider_seconds()
633 // Thank god for var_export()
635 0 => '00', 1 => '01', 2 => '02', 3 => '03', 4 => '04',
636 5 => '05', 6 => '06', 7 => '07', 8 => '08', 9 => '09',
637 10 => '10', 11 => '11', 12 => '12', 13 => '13', 14 => '14',
638 15 => '15', 16 => '16', 17 => '17', 18 => '18', 19 => '19',
639 20 => '20', 21 => '21', 22 => '22', 23 => '23', 24 => '24',
640 25 => '25', 26 => '26', 27 => '27', 28 => '28', 29 => '29',
641 30 => '30', 31 => '31', 32 => '32', 33 => '33', 34 => '34',
642 35 => '35', 36 => '36', 37 => '37', 38 => '38', 39 => '39',
643 40 => '40', 41 => '41', 42 => '42', 43 => '43', 44 => '44',
644 45 => '45', 46 => '46', 47 => '47', 48 => '48', 49 => '49',
645 50 => '50', 51 => '51', 52 => '52', 53 => '53', 54 => '54',
646 55 => '55', 56 => '56', 57 => '57', 58 => '58', 59 => '59',
658 * @dataProvider provider_seconds
659 * @covers Date::seconds
661 public function test_seconds($expected, $step = 1, $start = 0, $end = 60)
665 Date
::seconds($step, $start, $end)
670 * Provides test data for test_minutes
672 * @return array Test data
674 public function provider_minutes()
679 0 => '00', 5 => '05', 10 => '10',
680 15 => '15', 20 => '20', 25 => '25',
681 30 => '30', 35 => '35', 40 => '40',
682 45 => '45', 50 => '50', 55 => '55',
692 * @dataProvider provider_minutes
694 public function test_minutes($expected, $step)
703 * This tests that the minutes helper defaults to using a $step of 5
704 * and thus returns an array of 5 minute itervals
707 * @covers Date::minutes
709 public function test_minutes_defaults_to_using_step_of5()
712 0 => '00', 5 => '05', 10 => '10',
713 15 => '15', 20 => '20', 25 => '25',
714 30 => '30', 35 => '35', 40 => '40',
715 45 => '45', 50 => '50', 55 => '55',
725 * Provids for test_unix2dos
727 * @return array Test Data
729 public function provider_unix2dos()
744 * Test Date::unix2dos()
746 * You should always pass a timestamp as otherwise the current
747 * date/time would be used and that's oviously variable
749 * Geert seems to be the only person who knows how unix2dos() works
750 * so we just throw in some random values and see what happens
753 * @dataProvider provider_unix2dos
754 * @covers Date::unix2dos
755 * @param integer $expected Expected output
756 * @param integer $timestamp Input timestamp
758 public function test_unix2dos($expected, $timestamp)
760 $this->assertSame($expected, Date
::unix2dos($timestamp));
764 * Provides test data for test_dos2unix
766 * @return array Test data
768 public function provider_dos2unix()
783 * Tests Date::dos2unix
786 * @dataProvider provider_dos2unix
787 * @param integer $expected Expected output
788 * @param integer $timestamp Input timestamp
790 public function test_dos2unix($expected, $timestamp)
792 $this->assertEquals($expected, Date
::dos2unix($timestamp));