PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/DateTimeTest.php

477 lines
14 KiB
PHP
Raw Normal View History

2012-07-31 20:56:11 +00:00
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
2012-07-31 20:56:11 +00:00
use PhpOffice\PhpSpreadsheet\Calculation\DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
/**
* Class DateTimeTest
*/
2016-03-22 14:35:50 +00:00
class DateTimeTest extends \PHPUnit_Framework_TestCase
2012-07-31 20:56:11 +00:00
{
public function setUp()
{
Functions::setCompatibilityMode(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/DATE.php';
2015-05-17 13:00:02 +00:00
}
public function testDATEtoPHP()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::DATE(2012, 1, 31);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(1327968000, $result, null, 1E-8);
}
public function testDATEtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::DATE(2012, 1, 31);
Functions::setReturnDateType(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()
{
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
$result = DateTime::DATE(1918, 11, 11);
Date::setExcelCalendar(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()
{
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
$result = DateTime::DATE(1901, 1, 31);
Date::setExcelCalendar(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/DATEVALUE.php';
2015-05-17 13:00:02 +00:00
}
public function testDATEVALUEtoPHP()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::DATEVALUE('2012-1-31');
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(1327968000, $result, null, 1E-8);
}
public function testDATEVALUEtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::DATEVALUE('2012-1-31');
Functions::setReturnDateType(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/YEAR.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/MONTH.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, 'WEEKNUM'], $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()
{
return require 'data/Calculation/DateTime/WEEKNUM.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/WEEKDAY.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/DAY.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/TIME.php';
2015-05-17 13:00:02 +00:00
}
public function testTIMEtoPHP()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::TIME(7, 30, 20);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(27020, $result, null, 1E-8);
}
public function testTIMEtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::TIME(7, 30, 20);
Functions::setReturnDateType(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/TIMEVALUE.php';
2015-05-17 13:00:02 +00:00
}
public function testTIMEVALUEtoPHP()
{
$this->markTestIncomplete('TODO: This test should be fixed');
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::TIMEVALUE('7:30:20');
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(23420, $result, null, 1E-8);
}
public function testTIMEVALUEtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::TIMEVALUE('7:30:20');
Functions::setReturnDateType(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/HOUR.php';
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerMINUTE
*/
2015-05-17 13:00:02 +00:00
public function testMINUTE()
{
$this->markTestIncomplete('TODO: This test should be fixed');
2015-05-17 13:00:02 +00:00
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/MINUTE.php';
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerSECOND
*/
2015-05-17 13:00:02 +00:00
public function testSECOND()
{
$this->markTestIncomplete('TODO: This test should be fixed');
2015-05-17 13:00:02 +00:00
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/SECOND.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/NETWORKDAYS.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/WORKDAY.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/EDATE.php';
2015-05-17 13:00:02 +00:00
}
public function testEDATEtoPHP()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::EDATE('2012-1-26', -1);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(1324857600, $result, null, 1E-8);
}
public function testEDATEtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::EDATE('2012-1-26', -1);
Functions::setReturnDateType(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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/EOMONTH.php';
2015-05-17 13:00:02 +00:00
}
public function testEOMONTHtoPHP()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_NUMERIC);
$result = DateTime::EOMONTH('2012-1-26', -1);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
2015-05-17 13:00:02 +00:00
$this->assertEquals(1325289600, $result, null, 1E-8);
}
public function testEOMONTHtoPHPObject()
{
Functions::setReturnDateType(Functions::RETURNDATE_PHP_OBJECT);
$result = DateTime::EOMONTH('2012-1-26', -1);
Functions::setReturnDateType(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
* @group fail19
2012-07-31 20:56:11 +00:00
*/
2015-05-17 13:00:02 +00:00
public function testDATEDIF()
{
$this->markTestIncomplete('TODO: This test should be fixed');
2015-05-17 13:00:02 +00:00
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/DATEDIF.php';
2015-05-17 13:00:02 +00:00
}
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);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/DAYS360.php';
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
/**
* @dataProvider providerYEARFRAC
* @group fail19
2012-07-31 20:56:11 +00:00
*/
2015-05-17 13:00:02 +00:00
public function testYEARFRAC()
{
$this->markTestIncomplete('TODO: This test should be fixed');
2015-05-17 13:00:02 +00:00
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array([DateTime::class, '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()
{
return require 'data/Calculation/DateTime/YEARFRAC.php';
2015-05-17 13:00:02 +00:00
}
2012-07-31 20:56:11 +00:00
}