No Description

DateTimeTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. require_once 'testDataFileIterator.php';
  3. class DateTimeTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function setUp()
  6. {
  7. if (!defined('PHPEXCEL_ROOT'))
  8. {
  9. define('PHPEXCEL_ROOT', APPLICATION_PATH . '/');
  10. }
  11. require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  12. PHPExcel_Calculation_Functions::setCompatibilityMode(PHPExcel_Calculation_Functions::COMPATIBILITY_EXCEL);
  13. }
  14. /**
  15. * @dataProvider providerDATE
  16. */
  17. public function testDATE()
  18. {
  19. $args = func_get_args();
  20. $expectedResult = array_pop($args);
  21. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DATE'),$args);
  22. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  23. }
  24. public function providerDATE()
  25. {
  26. return new testDataFileIterator('rawTestData/Calculation/DateTime/DATE.data');
  27. }
  28. public function testDATEtoPHP()
  29. {
  30. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  31. $result = PHPExcel_Calculation_DateTime::DATE(2012,1,31);
  32. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  33. $this->assertEquals(1327968000, $result, NULL, 1E-8);
  34. }
  35. public function testDATEtoPHPObject()
  36. {
  37. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  38. $result = PHPExcel_Calculation_DateTime::DATE(2012,1,31);
  39. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  40. // Must return an object...
  41. $this->assertTrue(is_object($result));
  42. // ... of the correct type
  43. $this->assertTrue(is_a($result,'DateTime'));
  44. // ... with the correct value
  45. $this->assertEquals($result->format('d-M-Y'),'31-Jan-2012');
  46. }
  47. public function testDATEwith1904Calendar()
  48. {
  49. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  50. $result = PHPExcel_Calculation_DateTime::DATE(1918,11,11);
  51. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  52. $this->assertEquals($result,5428);
  53. }
  54. public function testDATEwith1904CalendarError()
  55. {
  56. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  57. $result = PHPExcel_Calculation_DateTime::DATE(1901,1,31);
  58. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  59. $this->assertEquals($result,'#NUM!');
  60. }
  61. /**
  62. * @dataProvider providerDATEVALUE
  63. */
  64. public function testDATEVALUE()
  65. {
  66. $args = func_get_args();
  67. $expectedResult = array_pop($args);
  68. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DATEVALUE'),$args);
  69. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  70. }
  71. public function providerDATEVALUE()
  72. {
  73. return new testDataFileIterator('rawTestData/Calculation/DateTime/DATEVALUE.data');
  74. }
  75. public function testDATEVALUEtoPHP()
  76. {
  77. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  78. $result = PHPExcel_Calculation_DateTime::DATEVALUE('2012-1-31');
  79. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  80. $this->assertEquals(1327968000, $result, NULL, 1E-8);
  81. }
  82. public function testDATEVALUEtoPHPObject()
  83. {
  84. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  85. $result = PHPExcel_Calculation_DateTime::DATEVALUE('2012-1-31');
  86. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  87. // Must return an object...
  88. $this->assertTrue(is_object($result));
  89. // ... of the correct type
  90. $this->assertTrue(is_a($result,'DateTime'));
  91. // ... with the correct value
  92. $this->assertEquals($result->format('d-M-Y'),'31-Jan-2012');
  93. }
  94. /**
  95. * @dataProvider providerYEAR
  96. */
  97. public function testYEAR()
  98. {
  99. $args = func_get_args();
  100. $expectedResult = array_pop($args);
  101. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','YEAR'),$args);
  102. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  103. }
  104. public function providerYEAR()
  105. {
  106. return new testDataFileIterator('rawTestData/Calculation/DateTime/YEAR.data');
  107. }
  108. /**
  109. * @dataProvider providerMONTH
  110. */
  111. public function testMONTH()
  112. {
  113. $args = func_get_args();
  114. $expectedResult = array_pop($args);
  115. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','MONTHOFYEAR'),$args);
  116. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  117. }
  118. public function providerMONTH()
  119. {
  120. return new testDataFileIterator('rawTestData/Calculation/DateTime/MONTH.data');
  121. }
  122. /**
  123. * @dataProvider providerWEEKNUM
  124. */
  125. public function testWEEKNUM()
  126. {
  127. $args = func_get_args();
  128. $expectedResult = array_pop($args);
  129. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','WEEKOFYEAR'),$args);
  130. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  131. }
  132. public function providerWEEKNUM()
  133. {
  134. return new testDataFileIterator('rawTestData/Calculation/DateTime/WEEKNUM.data');
  135. }
  136. /**
  137. * @dataProvider providerWEEKDAY
  138. */
  139. public function testWEEKDAY()
  140. {
  141. $args = func_get_args();
  142. $expectedResult = array_pop($args);
  143. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DAYOFWEEK'),$args);
  144. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  145. }
  146. public function providerWEEKDAY()
  147. {
  148. return new testDataFileIterator('rawTestData/Calculation/DateTime/WEEKDAY.data');
  149. }
  150. /**
  151. * @dataProvider providerDAY
  152. */
  153. public function testDAY()
  154. {
  155. $args = func_get_args();
  156. $expectedResult = array_pop($args);
  157. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DAYOFMONTH'),$args);
  158. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  159. }
  160. public function providerDAY()
  161. {
  162. return new testDataFileIterator('rawTestData/Calculation/DateTime/DAY.data');
  163. }
  164. /**
  165. * @dataProvider providerTIME
  166. */
  167. public function testTIME()
  168. {
  169. $args = func_get_args();
  170. $expectedResult = array_pop($args);
  171. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','TIME'),$args);
  172. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  173. }
  174. public function providerTIME()
  175. {
  176. return new testDataFileIterator('rawTestData/Calculation/DateTime/TIME.data');
  177. }
  178. public function testTIMEtoPHP()
  179. {
  180. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  181. $result = PHPExcel_Calculation_DateTime::TIME(7,30,20);
  182. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  183. $this->assertEquals(27020, $result, NULL, 1E-8);
  184. }
  185. public function testTIMEtoPHPObject()
  186. {
  187. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  188. $result = PHPExcel_Calculation_DateTime::TIME(7,30,20);
  189. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  190. // Must return an object...
  191. $this->assertTrue(is_object($result));
  192. // ... of the correct type
  193. $this->assertTrue(is_a($result,'DateTime'));
  194. // ... with the correct value
  195. $this->assertEquals($result->format('H:i:s'),'07:30:20');
  196. }
  197. /**
  198. * @dataProvider providerTIMEVALUE
  199. */
  200. public function testTIMEVALUE()
  201. {
  202. $args = func_get_args();
  203. $expectedResult = array_pop($args);
  204. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','TIMEVALUE'),$args);
  205. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  206. }
  207. public function providerTIMEVALUE()
  208. {
  209. return new testDataFileIterator('rawTestData/Calculation/DateTime/TIMEVALUE.data');
  210. }
  211. public function testTIMEVALUEtoPHP()
  212. {
  213. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  214. $result = PHPExcel_Calculation_DateTime::TIMEVALUE('7:30:20');
  215. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  216. $this->assertEquals(23420, $result, NULL, 1E-8);
  217. }
  218. public function testTIMEVALUEtoPHPObject()
  219. {
  220. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  221. $result = PHPExcel_Calculation_DateTime::TIMEVALUE('7:30:20');
  222. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  223. // Must return an object...
  224. $this->assertTrue(is_object($result));
  225. // ... of the correct type
  226. $this->assertTrue(is_a($result,'DateTime'));
  227. // ... with the correct value
  228. $this->assertEquals($result->format('H:i:s'),'07:30:20');
  229. }
  230. /**
  231. * @dataProvider providerHOUR
  232. */
  233. public function testHOUR()
  234. {
  235. $args = func_get_args();
  236. $expectedResult = array_pop($args);
  237. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','HOUROFDAY'),$args);
  238. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  239. }
  240. public function providerHOUR()
  241. {
  242. return new testDataFileIterator('rawTestData/Calculation/DateTime/HOUR.data');
  243. }
  244. /**
  245. * @dataProvider providerMINUTE
  246. */
  247. public function testMINUTE()
  248. {
  249. $args = func_get_args();
  250. $expectedResult = array_pop($args);
  251. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','MINUTEOFHOUR'),$args);
  252. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  253. }
  254. public function providerMINUTE()
  255. {
  256. return new testDataFileIterator('rawTestData/Calculation/DateTime/MINUTE.data');
  257. }
  258. /**
  259. * @dataProvider providerSECOND
  260. */
  261. public function testSECOND()
  262. {
  263. $args = func_get_args();
  264. $expectedResult = array_pop($args);
  265. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','SECONDOFMINUTE'),$args);
  266. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  267. }
  268. public function providerSECOND()
  269. {
  270. return new testDataFileIterator('rawTestData/Calculation/DateTime/SECOND.data');
  271. }
  272. /**
  273. * @dataProvider providerNETWORKDAYS
  274. */
  275. public function testNETWORKDAYS()
  276. {
  277. $args = func_get_args();
  278. $expectedResult = array_pop($args);
  279. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','NETWORKDAYS'),$args);
  280. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  281. }
  282. public function providerNETWORKDAYS()
  283. {
  284. return new testDataFileIterator('rawTestData/Calculation/DateTime/NETWORKDAYS.data');
  285. }
  286. /**
  287. * @dataProvider providerWORKDAY
  288. */
  289. public function testWORKDAY()
  290. {
  291. $args = func_get_args();
  292. $expectedResult = array_pop($args);
  293. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','WORKDAY'),$args);
  294. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  295. }
  296. public function providerWORKDAY()
  297. {
  298. return new testDataFileIterator('rawTestData/Calculation/DateTime/WORKDAY.data');
  299. }
  300. /**
  301. * @dataProvider providerEDATE
  302. */
  303. public function testEDATE()
  304. {
  305. $args = func_get_args();
  306. $expectedResult = array_pop($args);
  307. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','EDATE'),$args);
  308. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  309. }
  310. public function providerEDATE()
  311. {
  312. return new testDataFileIterator('rawTestData/Calculation/DateTime/EDATE.data');
  313. }
  314. public function testEDATEtoPHP()
  315. {
  316. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  317. $result = PHPExcel_Calculation_DateTime::EDATE('2012-1-26',-1);
  318. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  319. $this->assertEquals(1324857600, $result, NULL, 1E-8);
  320. }
  321. public function testEDATEtoPHPObject()
  322. {
  323. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  324. $result = PHPExcel_Calculation_DateTime::EDATE('2012-1-26',-1);
  325. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  326. // Must return an object...
  327. $this->assertTrue(is_object($result));
  328. // ... of the correct type
  329. $this->assertTrue(is_a($result,'DateTime'));
  330. // ... with the correct value
  331. $this->assertEquals($result->format('d-M-Y'),'26-Dec-2011');
  332. }
  333. /**
  334. * @dataProvider providerEOMONTH
  335. */
  336. public function testEOMONTH()
  337. {
  338. $args = func_get_args();
  339. $expectedResult = array_pop($args);
  340. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','EOMONTH'),$args);
  341. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  342. }
  343. public function providerEOMONTH()
  344. {
  345. return new testDataFileIterator('rawTestData/Calculation/DateTime/EOMONTH.data');
  346. }
  347. public function testEOMONTHtoPHP()
  348. {
  349. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_NUMERIC);
  350. $result = PHPExcel_Calculation_DateTime::EOMONTH('2012-1-26',-1);
  351. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  352. $this->assertEquals(1325289600, $result, NULL, 1E-8);
  353. }
  354. public function testEOMONTHtoPHPObject()
  355. {
  356. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_PHP_OBJECT);
  357. $result = PHPExcel_Calculation_DateTime::EOMONTH('2012-1-26',-1);
  358. PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);
  359. // Must return an object...
  360. $this->assertTrue(is_object($result));
  361. // ... of the correct type
  362. $this->assertTrue(is_a($result,'DateTime'));
  363. // ... with the correct value
  364. $this->assertEquals($result->format('d-M-Y'),'31-Dec-2011');
  365. }
  366. /**
  367. * @dataProvider providerDATEDIF
  368. */
  369. public function testDATEDIF()
  370. {
  371. $args = func_get_args();
  372. $expectedResult = array_pop($args);
  373. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DATEDIF'),$args);
  374. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  375. }
  376. public function providerDATEDIF()
  377. {
  378. return new testDataFileIterator('rawTestData/Calculation/DateTime/DATEDIF.data');
  379. }
  380. /**
  381. * @dataProvider providerDAYS360
  382. */
  383. public function testDAYS360()
  384. {
  385. $args = func_get_args();
  386. $expectedResult = array_pop($args);
  387. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','DAYS360'),$args);
  388. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  389. }
  390. public function providerDAYS360()
  391. {
  392. return new testDataFileIterator('rawTestData/Calculation/DateTime/DAYS360.data');
  393. }
  394. /**
  395. * @dataProvider providerYEARFRAC
  396. */
  397. public function testYEARFRAC()
  398. {
  399. $args = func_get_args();
  400. $expectedResult = array_pop($args);
  401. $result = call_user_func_array(array('PHPExcel_Calculation_DateTime','YEARFRAC'),$args);
  402. $this->assertEquals($expectedResult, $result, NULL, 1E-8);
  403. }
  404. public function providerYEARFRAC()
  405. {
  406. return new testDataFileIterator('rawTestData/Calculation/DateTime/YEARFRAC.data');
  407. }
  408. }