From b7dfeb9bb7b3e4328fe0c1fcf43490a3e2c2113f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 24 Jul 2012 13:21:31 +0100 Subject: [PATCH] Unit Tests for Date helper functions --- unitTests/PHPExcel/Shared/DateTest.php | 69 +++++++++++++++++++ .../Shared/DateTimeExcelToPHP1900.data | 6 +- .../Shared/DateTimeExcelToPHP1904.data | 3 + .../DateTimeFormattedPHPToExcel1900.data | 12 ++++ .../Shared/DateTimePHPToExcel1900.data | 12 ++++ .../Shared/DateTimePHPToExcel1904.data | 7 ++ 6 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 unitTests/rawTestData/Shared/DateTimeFormattedPHPToExcel1900.data create mode 100644 unitTests/rawTestData/Shared/DateTimePHPToExcel1900.data create mode 100644 unitTests/rawTestData/Shared/DateTimePHPToExcel1904.data diff --git a/unitTests/PHPExcel/Shared/DateTest.php b/unitTests/PHPExcel/Shared/DateTest.php index 6ee3b95..ea6991e 100644 --- a/unitTests/PHPExcel/Shared/DateTest.php +++ b/unitTests/PHPExcel/Shared/DateTest.php @@ -46,6 +46,9 @@ class DateTest extends PHPUnit_Framework_TestCase $args = func_get_args(); $expectedResult = array_pop($args); + if ($args[0] < 1) { + $expectedResult += gmmktime(0,0,0); + } $result = call_user_func_array(array('PHPExcel_Shared_Date','ExcelToPHP'),$args); $this->assertEquals($expectedResult, $result); } @@ -55,6 +58,48 @@ class DateTest extends PHPUnit_Framework_TestCase return new testDataFileIterator('rawTestData/Shared/DateTimeExcelToPHP1900.data'); } + /** + * @dataProvider providerDateTimePHPToExcel1900 + */ + public function testDateTimePHPToExcel1900() + { + $result = call_user_func( + array('PHPExcel_Shared_Date','setExcelCalendar'), + PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900 + ); + + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Date','PHPToExcel'),$args); + $this->assertEquals($expectedResult, $result, NULL, 1E-5); + } + + public function providerDateTimePHPToExcel1900() + { + return new testDataFileIterator('rawTestData/Shared/DateTimePHPToExcel1900.data'); + } + + /** + * @dataProvider providerDateTimeFormattedPHPToExcel1900 + */ + public function testDateTimeFormattedPHPToExcel1900() + { + $result = call_user_func( + array('PHPExcel_Shared_Date','setExcelCalendar'), + PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900 + ); + + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Date','FormattedPHPToExcel'),$args); + $this->assertEquals($expectedResult, $result, NULL, 1E-5); + } + + public function providerDateTimeFormattedPHPToExcel1900() + { + return new testDataFileIterator('rawTestData/Shared/DateTimeFormattedPHPToExcel1900.data'); + } + /** * @dataProvider providerDateTimeExcelToPHP1904 */ @@ -67,6 +112,9 @@ class DateTest extends PHPUnit_Framework_TestCase $args = func_get_args(); $expectedResult = array_pop($args); + if ($args[0] < 1) { + $expectedResult += gmmktime(0,0,0); + } $result = call_user_func_array(array('PHPExcel_Shared_Date','ExcelToPHP'),$args); $this->assertEquals($expectedResult, $result); } @@ -76,6 +124,27 @@ class DateTest extends PHPUnit_Framework_TestCase return new testDataFileIterator('rawTestData/Shared/DateTimeExcelToPHP1904.data'); } + /** + * @dataProvider providerDateTimePHPToExcel1904 + */ + public function testDateTimePHPToExcel1904() + { + $result = call_user_func( + array('PHPExcel_Shared_Date','setExcelCalendar'), + PHPExcel_Shared_Date::CALENDAR_MAC_1904 + ); + + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Shared_Date','PHPToExcel'),$args); + $this->assertEquals($expectedResult, $result, NULL, 1E-5); + } + + public function providerDateTimePHPToExcel1904() + { + return new testDataFileIterator('rawTestData/Shared/DateTimePHPToExcel1904.data'); + } + /** * @dataProvider providerIsDateTimeFormatCode */ diff --git a/unitTests/rawTestData/Shared/DateTimeExcelToPHP1900.data b/unitTests/rawTestData/Shared/DateTimeExcelToPHP1900.data index 85497b8..d348b0a 100644 --- a/unitTests/rawTestData/Shared/DateTimeExcelToPHP1900.data +++ b/unitTests/rawTestData/Shared/DateTimeExcelToPHP1900.data @@ -10,6 +10,6 @@ 50424, 2147472000 // PHP 32-bit Latest Date 19-Jan-2038 1234.56789, -2102494934 // 18-May-1903 13:37:46 12345.6789, -1142494943 // 18-Oct-1933 16:17:37 -0.5, 1340539200 // 12:00:00 -0.75, 1340560800 // 18:00.00 -0.12345, 1340506666 // 02:57:46 +0.5, 43200 // 12:00:00 +0.75, 64800 // 18:00.00 +0.12345, 10666 // 02:57:46 diff --git a/unitTests/rawTestData/Shared/DateTimeExcelToPHP1904.data b/unitTests/rawTestData/Shared/DateTimeExcelToPHP1904.data index b30cff3..5501ad1 100644 --- a/unitTests/rawTestData/Shared/DateTimeExcelToPHP1904.data +++ b/unitTests/rawTestData/Shared/DateTimeExcelToPHP1904.data @@ -5,3 +5,6 @@ 25569, 126316800 30292, 534384000 39611, 1339545600 +0.25, 21600 // 06:00:00 +0.3333333333333333333, 28800 // 08:00.00 +0.54321, 46933 // 02:57:46 diff --git a/unitTests/rawTestData/Shared/DateTimeFormattedPHPToExcel1900.data b/unitTests/rawTestData/Shared/DateTimeFormattedPHPToExcel1900.data new file mode 100644 index 0000000..bb6ed3f --- /dev/null +++ b/unitTests/rawTestData/Shared/DateTimeFormattedPHPToExcel1900.data @@ -0,0 +1,12 @@ +#Year Month Day Hours Minutes Seconds Result Comments +1901, 12, 14, 714 // PHP 32-bit Earliest Date 14-Dec-1901 +1903, 12, 31, 1461 // 31-Dec-1903 +1904, 1, 1, 1462 // Excel 1904 Calendar Base Date 01-Jan-1904 +1904, 1, 2, 1463 // 02-Jan-1904 +1960, 12, 19, 22269 // 19-Dec-1960 +1970, 1, 1, 25569 // PHP Base Date 01-Jan-1970 +1982, 12, 7, 30292 // 07-Dec-1982 +2008, 6, 12, 39611 // 12-Jun-2008 +2038, 1, 19, 50424 // PHP 32-bit Latest Date 19-Jan-2038 +1903, 5, 18, 13, 37, 46, 1234.56789 // 18-May-1903 13:37:46 +1933, 10, 18, 16, 17, 37, 12345.6789 // 18-Oct-1933 16:17:37 diff --git a/unitTests/rawTestData/Shared/DateTimePHPToExcel1900.data b/unitTests/rawTestData/Shared/DateTimePHPToExcel1900.data new file mode 100644 index 0000000..dc6a9d9 --- /dev/null +++ b/unitTests/rawTestData/Shared/DateTimePHPToExcel1900.data @@ -0,0 +1,12 @@ +#Excel DateTimeStamp Result Comments +-2147472000, 714 // PHP 32-bit Earliest Date 14-Dec-1901 +-2082931200, 1461 // 31-Dec-1903 +-2082844800, 1462 // Excel 1904 Calendar Base Date 01-Jan-1904 +-2082758400, 1463 // 02-Jan-1904 +-285120000, 22269 // 19-Dec-1960 +0, 25569 // PHP Base Date 01-Jan-1970 +408067200, 30292 // 07-Dec-1982 +1213228800, 39611 // 12-Jun-2008 +2147472000, 50424 // PHP 32-bit Latest Date 19-Jan-2038 +-2102494934, 1234.56789 // 18-May-1903 13:37:46 +-1142494943, 12345.6789 // 18-Oct-1933 16:17:37 diff --git a/unitTests/rawTestData/Shared/DateTimePHPToExcel1904.data b/unitTests/rawTestData/Shared/DateTimePHPToExcel1904.data new file mode 100644 index 0000000..98fa495 --- /dev/null +++ b/unitTests/rawTestData/Shared/DateTimePHPToExcel1904.data @@ -0,0 +1,7 @@ +#Excel DateTimeStamp Result +-1956528000, 1462 +-1956441600, 1463 +-158803200, 22269 +126316800, 25569 +534384000, 30292 +1339545600, 39611