PhpSpreadsheet/unitTests/Classes/PHPExcel/Calculation/DateTimeTest.php

465 lines
16 KiB
PHP
Raw Normal View History

2012-07-31 20:56:11 +00:00
<?php
require_once 'testDataFileIterator.php';
class DateTimeTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
2015-05-17 16:34:30 +00:00
if (!defined('PHPEXCEL_ROOT')) {
2012-07-31 20:56:11 +00:00
define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
}
require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
PHPExcel_Calculation_Functions::setCompatibilityMode(PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL);
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerDATE
*/
2015-05-17 13:00:02 +00:00
public function testDATE()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DATE'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerDATE()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/DATE.data');
}
public function testDATEtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::DATE(2012, 1, 31);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(1327968000, $result, null, 1E-8);
}
public function testDATEtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::DATE(2012, 1, 31);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('d-M-Y'), '31-Jan-2012');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
2015-05-17 13:00:02 +00:00
public function testDATEwith1904Calendar()
{
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::DATE(1918, 11, 11);
2015-05-17 13:00:02 +00:00
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
2015-05-17 17:26:34 +00:00
$this->assertEquals($result, 5428);
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
2015-05-17 13:00:02 +00:00
public function testDATEwith1904CalendarError()
{
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::DATE(1901, 1, 31);
2015-05-17 13:00:02 +00:00
PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
2015-05-17 17:26:34 +00:00
$this->assertEquals($result, '#NUM!');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerDATEVALUE
*/
2015-05-17 13:00:02 +00:00
public function testDATEVALUE()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DATEVALUE'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerDATEVALUE()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/DATEVALUE.data');
}
public function testDATEVALUEtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
$result = PHPExcel_Calculation_DateTime::DATEVALUE('2012-1-31');
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(1327968000, $result, null, 1E-8);
}
public function testDATEVALUEtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
$result = PHPExcel_Calculation_DateTime::DATEVALUE('2012-1-31');
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('d-M-Y'), '31-Jan-2012');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerYEAR
*/
2015-05-17 13:00:02 +00:00
public function testYEAR()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'YEAR'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerYEAR()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/YEAR.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerMONTH
*/
2015-05-17 13:00:02 +00:00
public function testMONTH()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'MONTHOFYEAR'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerMONTH()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/MONTH.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerWEEKNUM
*/
2015-05-17 13:00:02 +00:00
public function testWEEKNUM()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'WEEKOFYEAR'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerWEEKNUM()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/WEEKNUM.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerWEEKDAY
*/
2015-05-17 13:00:02 +00:00
public function testWEEKDAY()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DAYOFWEEK'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerWEEKDAY()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/WEEKDAY.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerDAY
*/
2015-05-17 13:00:02 +00:00
public function testDAY()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DAYOFMONTH'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerDAY()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/DAY.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerTIME
*/
2015-05-17 13:00:02 +00:00
public function testTIME()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'TIME'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerTIME()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/TIME.data');
}
public function testTIMEtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::TIME(7, 30, 20);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(27020, $result, null, 1E-8);
}
public function testTIMEtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
2015-05-17 17:26:34 +00:00
$result = PHPExcel_Calculation_DateTime::TIME(7, 30, 20);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('H:i:s'), '07:30:20');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerTIMEVALUE
*/
2015-05-17 13:00:02 +00:00
public function testTIMEVALUE()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'TIMEVALUE'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerTIMEVALUE()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/TIMEVALUE.data');
}
public function testTIMEVALUEtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
$result = PHPExcel_Calculation_DateTime::TIMEVALUE('7:30:20');
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(23420, $result, null, 1E-8);
}
public function testTIMEVALUEtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
$result = PHPExcel_Calculation_DateTime::TIMEVALUE('7:30:20');
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('H:i:s'), '07:30:20');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerHOUR
*/
2015-05-17 13:00:02 +00:00
public function testHOUR()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'HOUROFDAY'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerHOUR()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/HOUR.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerMINUTE
*/
2015-05-17 13:00:02 +00:00
public function testMINUTE()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'MINUTEOFHOUR'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerMINUTE()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/MINUTE.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerSECOND
*/
2015-05-17 13:00:02 +00:00
public function testSECOND()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'SECONDOFMINUTE'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerSECOND()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/SECOND.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerNETWORKDAYS
*/
2015-05-17 13:00:02 +00:00
public function testNETWORKDAYS()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'NETWORKDAYS'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerNETWORKDAYS()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/NETWORKDAYS.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerWORKDAY
*/
2015-05-17 13:00:02 +00:00
public function testWORKDAY()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'WORKDAY'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerWORKDAY()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/WORKDAY.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerEDATE
*/
2015-05-17 13:00:02 +00:00
public function testEDATE()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'EDATE'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerEDATE()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/EDATE.data');
}
public function testEDATEtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
2015-05-17 17:33:14 +00:00
$result = PHPExcel_Calculation_DateTime::EDATE('2012-1-26', -1);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(1324857600, $result, null, 1E-8);
}
public function testEDATEtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
2015-05-17 17:33:14 +00:00
$result = PHPExcel_Calculation_DateTime::EDATE('2012-1-26', -1);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('d-M-Y'), '26-Dec-2011');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerEOMONTH
*/
2015-05-17 13:00:02 +00:00
public function testEOMONTH()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'EOMONTH'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerEOMONTH()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/EOMONTH.data');
}
public function testEOMONTHtoPHP()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
2015-05-17 17:33:14 +00:00
$result = PHPExcel_Calculation_DateTime::EOMONTH('2012-1-26', -1);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
$this->assertEquals(1325289600, $result, null, 1E-8);
}
public function testEOMONTHtoPHPObject()
{
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
2015-05-17 17:33:14 +00:00
$result = PHPExcel_Calculation_DateTime::EOMONTH('2012-1-26', -1);
2015-05-17 13:00:02 +00:00
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
2012-07-31 20:56:11 +00:00
// Must return an object...
$this->assertTrue(is_object($result));
// ... of the correct type
2015-05-17 17:26:34 +00:00
$this->assertTrue(is_a($result, 'DateTime'));
2012-07-31 20:56:11 +00:00
// ... with the correct value
2015-05-17 17:26:34 +00:00
$this->assertEquals($result->format('d-M-Y'), '31-Dec-2011');
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerDATEDIF
*/
2015-05-17 13:00:02 +00:00
public function testDATEDIF()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DATEDIF'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerDATEDIF()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/DATEDIF.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerDAYS360
*/
2015-05-17 13:00:02 +00:00
public function testDAYS360()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'DAYS360'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerDAYS360()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/DAYS360.data');
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerYEARFRAC
*/
2015-05-17 13:00:02 +00:00
public function testYEARFRAC()
{
$args = func_get_args();
$expectedResult = array_pop($args);
2015-05-17 17:26:34 +00:00
$result = call_user_func_array(array('PHPExcel_Calculation_DateTime', 'YEARFRAC'), $args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result, null, 1E-8);
}
2012-07-31 20:56:11 +00:00
public function providerYEARFRAC()
{
2015-05-17 13:00:02 +00:00
return new testDataFileIterator('rawTestData/Calculation/DateTime/YEARFRAC.data');
}
2012-07-31 20:56:11 +00:00
}