PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/PHPUnit/Core/Period/RangeTest.php

https://github.com/CodeYellowBV/piwik
PHP | 1317 lines | 1000 code | 137 blank | 180 comment | 13 complexity | 4397998d919da865864377998a9fef5e MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. use Piwik\Date;
  9. use Piwik\Period\Month;
  10. use Piwik\Period\Range;
  11. use Piwik\Period\Week;
  12. use Piwik\Period\Year;
  13. use Piwik\Translate;
  14. class Period_RangeTest extends PHPUnit_Framework_TestCase
  15. {
  16. // test range 1
  17. /**
  18. * @group Core
  19. */
  20. public function testRangeToday()
  21. {
  22. $range = new Range('day', 'last1');
  23. $today = Date::today();
  24. $correct = array(
  25. $today->toString(),
  26. );
  27. $correct = array_reverse($correct);
  28. $this->assertEquals(1, $range->getNumberOfSubperiods());
  29. $this->assertEquals($correct, $range->toString());
  30. }
  31. /**
  32. * @group Core
  33. */
  34. public function testRangeTodayUtcPlus12()
  35. {
  36. // rather ugly test, UTC+23 doesn't exist, but it's a way to test that last1 in UTC+23 will be "our" UTC tomorrow
  37. $range = new Range('day', 'last1', 'UTC+23');
  38. $today = Date::now()->addHour(23);
  39. $correct = array(
  40. $today->toString(),
  41. );
  42. $correct = array_reverse($correct);
  43. $this->assertEquals(1, $range->getNumberOfSubperiods());
  44. $this->assertEquals($correct, $range->toString());
  45. }
  46. // test range 2
  47. /**
  48. * @group Core
  49. */
  50. public function testRange2days()
  51. {
  52. $range = new Range('day', 'last2');
  53. $today = Date::today();
  54. $correct = array(
  55. $today->toString(),
  56. $today->subDay(1)->toString()
  57. );
  58. $correct = array_reverse($correct);
  59. $this->assertEquals(2, $range->getNumberOfSubperiods());
  60. $this->assertEquals($correct, $range->toString());
  61. }
  62. // test range 3
  63. /**
  64. * @group Core
  65. */
  66. public function testRange5days()
  67. {
  68. $range = new Range('day', 'last50');
  69. $today = Date::today();
  70. $correct = array();
  71. for ($i = 0; $i < 50; $i++) {
  72. $correct[] = $today->subDay($i)->toString();
  73. }
  74. $correct = array_reverse($correct);
  75. $this->assertEquals(50, $range->getNumberOfSubperiods());
  76. $this->assertEquals($correct, $range->toString());
  77. }
  78. // test range 4
  79. /**
  80. * @group Core
  81. */
  82. public function testRangePrevious3days()
  83. {
  84. $range = new Range('day', 'previous3');
  85. $yesterday = Date::yesterday();
  86. $correct = array();
  87. for ($i = 0; $i < 3; $i++) {
  88. $correct[] = $yesterday->subDay($i)->toString();
  89. }
  90. $correct = array_reverse($correct);
  91. $this->assertEquals(3, $range->getNumberOfSubperiods());
  92. $this->assertEquals($correct, $range->toString());
  93. }
  94. // test range date1,date2
  95. /**
  96. * @group Core
  97. */
  98. public function testRangeComma1()
  99. {
  100. $range = new Range('day', '2008-01-01,2008-01-03');
  101. $correct = array(
  102. '2008-01-01',
  103. '2008-01-02',
  104. '2008-01-03',
  105. );
  106. $this->assertEquals(3, $range->getNumberOfSubperiods());
  107. $this->assertEquals($correct, $range->toString());
  108. }
  109. // test range date1,date2
  110. /**
  111. * @group Core
  112. */
  113. public function testRangeComma2()
  114. {
  115. $range = new Range('day', '2007-12-22,2008-01-03');
  116. $correct = array(
  117. '2007-12-22',
  118. '2007-12-23',
  119. '2007-12-24',
  120. '2007-12-25',
  121. '2007-12-26',
  122. '2007-12-27',
  123. '2007-12-28',
  124. '2007-12-29',
  125. '2007-12-30',
  126. '2007-12-31',
  127. '2008-01-01',
  128. '2008-01-02',
  129. '2008-01-03',
  130. );
  131. $this->assertEquals(13, $range->getNumberOfSubperiods());
  132. $this->assertEquals($correct, $range->toString());
  133. }
  134. // test range date1,date2
  135. /**
  136. * @group Core
  137. */
  138. public function testRangeWeekcomma1()
  139. {
  140. $range = new Range('week', '2007-12-22,2008-01-03');
  141. $range2 = new Range('week', '2007-12-19,2008-01-03');
  142. $correct = array(
  143. array(
  144. '2007-12-17',
  145. '2007-12-18',
  146. '2007-12-19',
  147. '2007-12-20',
  148. '2007-12-21',
  149. '2007-12-22',
  150. '2007-12-23',
  151. ),
  152. array(
  153. '2007-12-24',
  154. '2007-12-25',
  155. '2007-12-26',
  156. '2007-12-27',
  157. '2007-12-28',
  158. '2007-12-29',
  159. '2007-12-30',
  160. ),
  161. array(
  162. '2007-12-31',
  163. '2008-01-01',
  164. '2008-01-02',
  165. '2008-01-03',
  166. '2008-01-04',
  167. '2008-01-05',
  168. '2008-01-06',
  169. )
  170. );
  171. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  172. $this->assertEquals(count($correct), $range2->getNumberOfSubperiods());
  173. $this->assertEquals($correct, $range->toString());
  174. $this->assertEquals($correct, $range2->toString());
  175. }
  176. // test range date1,date2
  177. /**
  178. * @group Core
  179. */
  180. public function testRangeYearcomma1()
  181. {
  182. $range = new Range('year', '2006-12-22,2007-01-03');
  183. $correct = array(
  184. array(
  185. 0 => '2006-01-01',
  186. 1 => '2006-02-01',
  187. 2 => '2006-03-01',
  188. 3 => '2006-04-01',
  189. 4 => '2006-05-01',
  190. 5 => '2006-06-01',
  191. 6 => '2006-07-01',
  192. 7 => '2006-08-01',
  193. 8 => '2006-09-01',
  194. 9 => '2006-10-01',
  195. 10 => '2006-11-01',
  196. 11 => '2006-12-01',
  197. ),
  198. 1 =>
  199. array(
  200. 0 => '2007-01-01',
  201. 1 => '2007-02-01',
  202. 2 => '2007-03-01',
  203. 3 => '2007-04-01',
  204. 4 => '2007-05-01',
  205. 5 => '2007-06-01',
  206. 6 => '2007-07-01',
  207. 7 => '2007-08-01',
  208. 8 => '2007-09-01',
  209. 9 => '2007-10-01',
  210. 10 => '2007-11-01',
  211. 11 => '2007-12-01',
  212. ),
  213. );
  214. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  215. $this->assertEquals($correct, $range->toString());
  216. }
  217. // test range date1,date2
  218. /**
  219. * @group Core
  220. */
  221. public function testRangeMonthcomma1()
  222. {
  223. $range = new Range('month', '2006-12-22,2007-01-03');
  224. $correct = array(
  225. array(
  226. '2006-12-01',
  227. '2006-12-02',
  228. '2006-12-03',
  229. '2006-12-04',
  230. '2006-12-05',
  231. '2006-12-06',
  232. '2006-12-07',
  233. '2006-12-08',
  234. '2006-12-09',
  235. '2006-12-10',
  236. '2006-12-11',
  237. '2006-12-12',
  238. '2006-12-13',
  239. '2006-12-14',
  240. '2006-12-15',
  241. '2006-12-16',
  242. '2006-12-17',
  243. '2006-12-18',
  244. '2006-12-19',
  245. '2006-12-20',
  246. '2006-12-21',
  247. '2006-12-22',
  248. '2006-12-23',
  249. '2006-12-24',
  250. '2006-12-25',
  251. '2006-12-26',
  252. '2006-12-27',
  253. '2006-12-28',
  254. '2006-12-29',
  255. '2006-12-30',
  256. '2006-12-31',
  257. ),
  258. array(
  259. '2007-01-01',
  260. '2007-01-02',
  261. '2007-01-03',
  262. '2007-01-04',
  263. '2007-01-05',
  264. '2007-01-06',
  265. '2007-01-07',
  266. '2007-01-08',
  267. '2007-01-09',
  268. '2007-01-10',
  269. '2007-01-11',
  270. '2007-01-12',
  271. '2007-01-13',
  272. '2007-01-14',
  273. '2007-01-15',
  274. '2007-01-16',
  275. '2007-01-17',
  276. '2007-01-18',
  277. '2007-01-19',
  278. '2007-01-20',
  279. '2007-01-21',
  280. '2007-01-22',
  281. '2007-01-23',
  282. '2007-01-24',
  283. '2007-01-25',
  284. '2007-01-26',
  285. '2007-01-27',
  286. '2007-01-28',
  287. '2007-01-29',
  288. '2007-01-30',
  289. '2007-01-31',
  290. ),
  291. );
  292. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  293. $this->assertEquals($correct, $range->toString());
  294. }
  295. // test range WEEK
  296. /**
  297. * @group Core
  298. */
  299. public function testRangeWeek()
  300. {
  301. $range = new Range('week', 'last50');
  302. $today = Date::today();
  303. $correct = array();
  304. for ($i = 0; $i < 50; $i++) {
  305. $date = $today->subDay($i * 7);
  306. $week = new Week($date);
  307. $correct[] = $week->toString();
  308. }
  309. $correct = array_reverse($correct);
  310. $this->assertEquals(50, $range->getNumberOfSubperiods());
  311. $this->assertEquals($correct, $range->toString());
  312. }
  313. // test range WEEK last1
  314. /**
  315. * @group Core
  316. */
  317. public function testRangeWeekLast1()
  318. {
  319. $range = new Range('week', 'last1');
  320. $currentWeek = new Week(Date::today());
  321. $this->assertEquals(1, $range->getNumberOfSubperiods());
  322. $this->assertEquals(array($currentWeek->toString()), $range->toString());
  323. }
  324. // test range MONTH
  325. /**
  326. * @group Core
  327. */
  328. public function testRangeMonth()
  329. {
  330. $range = new Range('month', 'last20');
  331. $today = Date::today();
  332. $correct = array();
  333. for ($i = 0; $i < 20; $i++) {
  334. $date = $today->subMonth($i);
  335. $week = new Month($date);
  336. $correct[] = $week->toString();
  337. }
  338. $correct = array_reverse($correct);
  339. $this->assertEquals(20, $range->getNumberOfSubperiods());
  340. $this->assertEquals($correct, $range->toString());
  341. }
  342. // test range MONTH last1
  343. /**
  344. * @group Core
  345. */
  346. public function testRangeMonthLast1()
  347. {
  348. $range = new Range('month', 'last1');
  349. $month = new Month(Date::today());
  350. $this->assertEquals(1, $range->getNumberOfSubperiods());
  351. $this->assertEquals(array($month->toString()), $range->toString());
  352. }
  353. // test range PREVIOUS MONTH
  354. /**
  355. * @group Core
  356. */
  357. public function testRangePreviousmonth()
  358. {
  359. $range = new Range('month', 'previous10');
  360. $end = Date::today();
  361. $end = $end->subMonth(1);
  362. $correct = array();
  363. for ($i = 0; $i < 10; $i++) {
  364. $date = $end->subMonth($i);
  365. $week = new Month($date);
  366. $correct[] = $week->toString();
  367. }
  368. $correct = array_reverse($correct);
  369. $this->assertEquals(10, $range->getNumberOfSubperiods());
  370. $this->assertEquals($correct, $range->toString());
  371. }
  372. /**
  373. * @group Core
  374. */
  375. public function testRangePreviousmonth_onLastDayOfMonth()
  376. {
  377. $end = Date::factory('2013-10-31');
  378. $range = new Range('month', 'previous10', 'UTC', $end);
  379. $end = $end->subMonth(1);
  380. $correct = array();
  381. for ($i = 0; $i < 10; $i++) {
  382. $date = $end->subMonth($i);
  383. $week = new Month($date);
  384. $correct[] = $week->toString();
  385. }
  386. $correct = array_reverse($correct);
  387. $this->assertEquals(10, $range->getNumberOfSubperiods());
  388. $this->assertEquals($correct, $range->toString());
  389. }
  390. /**
  391. * @group Core
  392. */
  393. public function testRangePreviousweek_onLastDayOfWeek()
  394. {
  395. $end = Date::factory('2013-11-03');
  396. $range = new Range('week', 'previous2', 'UTC', $end);
  397. $end = $end->subWeek(1);
  398. $correct = array();
  399. for ($i = 0; $i < 2; $i++) {
  400. $date = $end->subWeek($i);
  401. $week = new Week($date);
  402. $correct[] = $week->toString();
  403. }
  404. $correct = array_reverse($correct);
  405. $this->assertEquals($correct, $range->toString());
  406. }
  407. /**
  408. * @group Core
  409. */
  410. public function testRangePreviousweek_onFirstDayOfWeek()
  411. {
  412. $end = Date::factory('2013-11-04');
  413. $range = new Range('week', 'previous2', 'UTC', $end);
  414. $end = $end->subWeek(1);
  415. $correct = array();
  416. for ($i = 0; $i < 2; $i++) {
  417. $date = $end->subWeek($i);
  418. $week = new Week($date);
  419. $correct[] = $week->toString();
  420. }
  421. $correct = array_reverse($correct);
  422. $this->assertEquals($correct, $range->toString());
  423. }
  424. /**
  425. * @group Core
  426. */
  427. public function testRangeLastweek_onFirstDayOfWeek()
  428. {
  429. $end = Date::factory('2013-11-04');
  430. $range = new Range('week', 'last2', 'UTC', $end);
  431. $correct = array();
  432. for ($i = 0; $i < 2; $i++) {
  433. $date = $end->subWeek($i);
  434. $week = new Week($date);
  435. $correct[] = $week->toString();
  436. }
  437. $correct = array_reverse($correct);
  438. $this->assertEquals($correct, $range->toString());
  439. }
  440. /**
  441. * @group Core
  442. */
  443. public function testRangeLastmonth_onLastDayOfMonth()
  444. {
  445. $end = Date::factory('2013-10-31');
  446. $range = new Range('month', 'last10', 'UTC', $end);
  447. $correct = array();
  448. for ($i = 0; $i < 10; $i++) {
  449. $date = $end->subMonth($i);
  450. $week = new Month($date);
  451. $correct[] = $week->toString();
  452. }
  453. $correct = array_reverse($correct);
  454. $this->assertEquals(10, $range->getNumberOfSubperiods());
  455. $this->assertEquals($correct, $range->toString());
  456. }
  457. /**
  458. * @group Core
  459. */
  460. public function _testRangePreviousmonth_onFirstOfMonth()
  461. {
  462. $end = Date::factory('2013-11-01');
  463. $range = new Range('month', 'previous10', 'UTC', $end);
  464. $end = $end->subMonth(1);
  465. $correct = array();
  466. for ($i = 0; $i < 10; $i++) {
  467. $date = $end->subMonth($i);
  468. $week = new Month($date);
  469. $correct[] = $week->toString();
  470. }
  471. $correct = array_reverse($correct);
  472. $this->assertEquals(10, $range->getNumberOfSubperiods());
  473. $this->assertEquals($correct, $range->toString());
  474. }
  475. /**
  476. * @group Core
  477. */
  478. public function _testRangeLastmonth_onFirstOfMonth()
  479. {
  480. $end = Date::factory('2013-11-01');
  481. $range = new Range('month', 'last10', 'UTC', $end);
  482. $correct = array();
  483. for ($i = 0; $i < 10; $i++) {
  484. $date = $end->subMonth($i);
  485. $week = new Month($date);
  486. $correct[] = $week->toString();
  487. }
  488. $correct = array_reverse($correct);
  489. $this->assertEquals(10, $range->getNumberOfSubperiods());
  490. $this->assertEquals($correct, $range->toString());
  491. }
  492. // test range YEAR
  493. /**
  494. * @group Core
  495. */
  496. public function testRangeYear()
  497. {
  498. $range = new Range('year', 'last10');
  499. $today = Date::today();
  500. $correct = array();
  501. for ($i = 0; $i < 10; $i++) {
  502. $date = $today->subMonth(12 * $i);
  503. $week = new Year($date);
  504. $correct[] = $week->toString();
  505. }
  506. $correct = array_reverse($correct);
  507. $this->assertEquals(10, $range->getNumberOfSubperiods());
  508. $this->assertEquals($correct, $range->toString());
  509. }
  510. // test range YEAR last1
  511. /**
  512. * @group Core
  513. */
  514. public function testRangeYearLast1()
  515. {
  516. $range = new Range('year', 'last1');
  517. $currentYear = new Year(Date::today());
  518. $this->assertEquals(1, $range->getNumberOfSubperiods());
  519. $this->assertEquals(array($currentYear->toString()), $range->toString());
  520. }
  521. /**
  522. * @group Core
  523. */
  524. public function testCustomRangeYearUsesYearIfPossible()
  525. {
  526. $range = new Range('range', '2005-12-17,2008-01-03', 'UTC', Date::factory('2008-01-03'));
  527. $year2006 = new Year(Date::factory('2006-02-02'));
  528. $year2007 = new Year(Date::factory('2007-02-02'));
  529. $year2008 = new Year(Date::factory('2008-02-02'));
  530. $correct = array(
  531. '2005-12-17',
  532. '2005-12-18',
  533. array (
  534. "2005-12-19",
  535. "2005-12-20",
  536. "2005-12-21",
  537. "2005-12-22",
  538. "2005-12-23",
  539. "2005-12-24",
  540. "2005-12-25"
  541. ),
  542. "2005-12-26",
  543. "2005-12-27",
  544. "2005-12-28",
  545. "2005-12-29",
  546. "2005-12-30",
  547. "2005-12-31",
  548. $year2006->toString(),
  549. $year2007->toString(),
  550. $year2008->toString()
  551. );
  552. $this->assertEquals(12, $range->getNumberOfSubperiods());
  553. $this->assertEquals($correct, $range->toString());
  554. }
  555. /**
  556. * @group Core
  557. */
  558. public function testCustomRangeIsYear_UsesFullYear()
  559. {
  560. $range = new Range('range', '2011-01-01,2011-12-31', 'UTC', Date::factory('2012-01-03'));
  561. $year2011 = new Year(Date::factory('2011-02-02'));
  562. $correct = array(
  563. $year2011->toString()
  564. );
  565. $this->assertEquals(1, $range->getNumberOfSubperiods());
  566. $this->assertEquals($correct, $range->toString());
  567. }
  568. /**
  569. * @group Core
  570. */
  571. public function testCustomRangeYear_UsesCurrentYear()
  572. {
  573. $range = new Range('range', '2013-01-01,2013-11-01', 'UTC', Date::factory('2013-11-01'));
  574. $year2013 = new Year(Date::factory('2013-02-02'));
  575. $correct = array(
  576. $year2013->toString()
  577. );
  578. $this->assertEquals(1, $range->getNumberOfSubperiods());
  579. $this->assertEquals($correct, $range->toString());
  580. }
  581. /**
  582. * @group Core
  583. */
  584. public function testCustomRangeYearUsesCurrentYear_onLastDayOfYear()
  585. {
  586. $range = new Range('range', '2013-01-01,2013-12-31', 'UTC', Date::factory('2013-12-31'));
  587. $year2013 = new Year(Date::factory('2013-01-01'));
  588. $correct = array(
  589. $year2013->toString()
  590. );
  591. $this->assertEquals(1, $range->getNumberOfSubperiods());
  592. $this->assertEquals($correct, $range->toString());
  593. }
  594. /**
  595. * @group Core
  596. */
  597. public function testCustomRangeWeekInsideEndingToday()
  598. {
  599. $range = new Range('range', '2007-12-22,2008-01-03', 'UTC', Date::factory('2008-01-03'));
  600. $correct = array(
  601. '2007-12-22',
  602. '2007-12-23',
  603. array(
  604. '2007-12-24',
  605. '2007-12-25',
  606. '2007-12-26',
  607. '2007-12-27',
  608. '2007-12-28',
  609. '2007-12-29',
  610. '2007-12-30',
  611. ),
  612. array(
  613. '2007-12-31',
  614. '2008-01-01',
  615. '2008-01-02',
  616. '2008-01-03',
  617. '2008-01-04',
  618. '2008-01-05',
  619. '2008-01-06',
  620. )
  621. );
  622. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  623. $this->assertEquals($correct, $range->toString());
  624. }
  625. /**
  626. * @group Core
  627. */
  628. public function testRangeEndDateIsTodayAndStartDateNotStartOfTheWeek()
  629. {
  630. $range = new Range('range', '2013-10-29,2013-10-30', 'UTC', Date::factory('2013-10-30'));
  631. $correct = array(
  632. '2013-10-29',
  633. '2013-10-30'
  634. );
  635. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  636. $this->assertEquals($correct, $range->toString());
  637. }
  638. /**
  639. * @group Core
  640. */
  641. public function testRangeEndDateIsInFuture()
  642. {
  643. $range = new Range('range', '2013-10-29,2013-10-31', 'UTC', Date::factory('2013-10-30'));
  644. $correct = array(
  645. '2013-10-29',
  646. '2013-10-30',
  647. '2013-10-31'
  648. );
  649. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  650. $this->assertEquals($correct, $range->toString());
  651. }
  652. /**
  653. * @group Core
  654. */
  655. public function testRangePreviousmonthEndDateIsInFutureAndEndOfTheWeek()
  656. {
  657. $range = new Range('range', '2013-10-29,2013-11-03', 'UTC', Date::factory('2013-10-30'));
  658. $correct = array(
  659. '2013-10-29',
  660. '2013-10-30',
  661. '2013-10-31',
  662. '2013-11-01',
  663. '2013-11-02',
  664. '2013-11-03',
  665. );
  666. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  667. $this->assertEquals($correct, $range->toString());
  668. }
  669. /**
  670. * @group Core
  671. */
  672. public function testCustomRangeWeekInsideEndingYesterday()
  673. {
  674. $todays = array(
  675. Date::factory('2008-01-04'),
  676. Date::factory('2008-01-05'),
  677. Date::factory('2008-01-14'),
  678. Date::factory('2008-02-14'),
  679. Date::factory('2009-02-14'),
  680. );
  681. foreach ($todays as $today) {
  682. $range = new Range('range', '2007-12-22,2008-01-03', 'UTC', $today);
  683. $correct = array(
  684. '2007-12-22',
  685. '2007-12-23',
  686. array(
  687. '2007-12-24',
  688. '2007-12-25',
  689. '2007-12-26',
  690. '2007-12-27',
  691. '2007-12-28',
  692. '2007-12-29',
  693. '2007-12-30',
  694. ),
  695. '2007-12-31',
  696. '2008-01-01',
  697. '2008-01-02',
  698. '2008-01-03',
  699. );
  700. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  701. $this->assertEquals($correct, $range->toString());
  702. }
  703. }
  704. /**
  705. * @group Core
  706. */
  707. public function testCustomRangeOnlyDaysLessThanOneWeek()
  708. {
  709. $range = new Range('range', '2007-12-30,2008-01-01');
  710. $correct = array(
  711. '2007-12-30',
  712. '2007-12-31',
  713. '2008-01-01',
  714. );
  715. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  716. $this->assertEquals($correct, $range->toString());
  717. }
  718. /**
  719. * @group Core
  720. */
  721. public function testCustomRangeOneWeekOnly()
  722. {
  723. $range = new Range('range', '2007-12-31,2008-01-06');
  724. $correct = array(
  725. array(
  726. '2007-12-31',
  727. '2008-01-01',
  728. '2008-01-02',
  729. '2008-01-03',
  730. '2008-01-04',
  731. '2008-01-05',
  732. '2008-01-06',
  733. )
  734. );
  735. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  736. $this->assertEquals($correct, $range->toString());
  737. }
  738. /**
  739. * @group Core
  740. */
  741. public function testCustomRangeStartsWithWeek()
  742. {
  743. $range = new Range('range', '2007-12-31,2008-01-08');
  744. $correct = array(
  745. array(
  746. '2007-12-31',
  747. '2008-01-01',
  748. '2008-01-02',
  749. '2008-01-03',
  750. '2008-01-04',
  751. '2008-01-05',
  752. '2008-01-06',
  753. ),
  754. '2008-01-07',
  755. '2008-01-08',
  756. );
  757. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  758. $this->assertEquals($correct, $range->toString());
  759. }
  760. /**
  761. * @group Core
  762. */
  763. public function testCustomRangeEndsWithWeek()
  764. {
  765. $range = new Range('range', '2007-12-21,2008-01-06');
  766. $correct = array(
  767. '2007-12-21',
  768. '2007-12-22',
  769. '2007-12-23',
  770. array(
  771. '2007-12-24',
  772. '2007-12-25',
  773. '2007-12-26',
  774. '2007-12-27',
  775. '2007-12-28',
  776. '2007-12-29',
  777. '2007-12-30',
  778. ),
  779. array(
  780. '2007-12-31',
  781. '2008-01-01',
  782. '2008-01-02',
  783. '2008-01-03',
  784. '2008-01-04',
  785. '2008-01-05',
  786. '2008-01-06',
  787. ),
  788. );
  789. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  790. $this->assertEquals($correct, $range->toString());
  791. }
  792. /**
  793. * @group Core
  794. */
  795. public function testCustomRangeContainsMonthAndWeek()
  796. {
  797. $range = new Range('range', '2011-09-18,2011-11-02', 'UTC', Date::factory('2012-01-01'));
  798. $correct = array(
  799. '2011-09-18',
  800. array(
  801. '2011-09-19',
  802. '2011-09-20',
  803. '2011-09-21',
  804. '2011-09-22',
  805. '2011-09-23',
  806. '2011-09-24',
  807. '2011-09-25',
  808. ),
  809. '2011-09-26',
  810. '2011-09-27',
  811. '2011-09-28',
  812. '2011-09-29',
  813. '2011-09-30',
  814. array(
  815. "2011-10-01",
  816. "2011-10-02",
  817. "2011-10-03",
  818. "2011-10-04",
  819. "2011-10-05",
  820. "2011-10-06",
  821. "2011-10-07",
  822. "2011-10-08",
  823. "2011-10-09",
  824. "2011-10-10",
  825. "2011-10-11",
  826. "2011-10-12",
  827. "2011-10-13",
  828. "2011-10-14",
  829. "2011-10-15",
  830. "2011-10-16",
  831. "2011-10-17",
  832. "2011-10-18",
  833. "2011-10-19",
  834. "2011-10-20",
  835. "2011-10-21",
  836. "2011-10-22",
  837. "2011-10-23",
  838. "2011-10-24",
  839. "2011-10-25",
  840. "2011-10-26",
  841. "2011-10-27",
  842. "2011-10-28",
  843. "2011-10-29",
  844. "2011-10-30",
  845. "2011-10-31",
  846. ),
  847. "2011-11-01",
  848. "2011-11-02",
  849. );
  850. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  851. $this->assertEquals($correct, $range->toString());
  852. }
  853. /**
  854. * @group Core
  855. */
  856. public function testCustomRangeContainsSeveralMonthsAndWeeksStartingWithMonth()
  857. {
  858. // Testing when "today" is in the same month, or later in the future
  859. $todays = array(
  860. Date::factory('2011-10-18'),
  861. Date::factory('2011-10-19'),
  862. Date::factory('2011-10-24'),
  863. Date::factory('2011-11-01'),
  864. Date::factory('2011-11-30'),
  865. Date::factory('2011-12-31'),
  866. Date::factory('2021-10-18')
  867. );
  868. foreach ($todays as $today) {
  869. $range = new Range('range', '2011-08-01,2011-10-17', 'UTC', $today);
  870. $correct = array(
  871. array(
  872. "2011-08-01",
  873. "2011-08-02",
  874. "2011-08-03",
  875. "2011-08-04",
  876. "2011-08-05",
  877. "2011-08-06",
  878. "2011-08-07",
  879. "2011-08-08",
  880. "2011-08-09",
  881. "2011-08-10",
  882. "2011-08-11",
  883. "2011-08-12",
  884. "2011-08-13",
  885. "2011-08-14",
  886. "2011-08-15",
  887. "2011-08-16",
  888. "2011-08-17",
  889. "2011-08-18",
  890. "2011-08-19",
  891. "2011-08-20",
  892. "2011-08-21",
  893. "2011-08-22",
  894. "2011-08-23",
  895. "2011-08-24",
  896. "2011-08-25",
  897. "2011-08-26",
  898. "2011-08-27",
  899. "2011-08-28",
  900. "2011-08-29",
  901. "2011-08-30",
  902. "2011-08-31",
  903. ),
  904. array(
  905. "2011-09-01",
  906. "2011-09-02",
  907. "2011-09-03",
  908. "2011-09-04",
  909. "2011-09-05",
  910. "2011-09-06",
  911. "2011-09-07",
  912. "2011-09-08",
  913. "2011-09-09",
  914. "2011-09-10",
  915. "2011-09-11",
  916. "2011-09-12",
  917. "2011-09-13",
  918. "2011-09-14",
  919. "2011-09-15",
  920. "2011-09-16",
  921. "2011-09-17",
  922. "2011-09-18",
  923. "2011-09-19",
  924. "2011-09-20",
  925. "2011-09-21",
  926. "2011-09-22",
  927. "2011-09-23",
  928. "2011-09-24",
  929. "2011-09-25",
  930. "2011-09-26",
  931. "2011-09-27",
  932. "2011-09-28",
  933. "2011-09-29",
  934. "2011-09-30",
  935. ),
  936. "2011-10-01",
  937. "2011-10-02",
  938. array(
  939. "2011-10-03",
  940. "2011-10-04",
  941. "2011-10-05",
  942. "2011-10-06",
  943. "2011-10-07",
  944. "2011-10-08",
  945. "2011-10-09",
  946. ),
  947. array(
  948. "2011-10-10",
  949. "2011-10-11",
  950. "2011-10-12",
  951. "2011-10-13",
  952. "2011-10-14",
  953. "2011-10-15",
  954. "2011-10-16",
  955. ),
  956. "2011-10-17",
  957. );
  958. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  959. $this->assertEquals($correct, $range->toString());
  960. }
  961. }
  962. /**
  963. * @group Core
  964. */
  965. public function testCustomRangeOneMonthOnly()
  966. {
  967. $range = new Range('range', '2011-09-01,2011-09-30');
  968. $correct = array(
  969. array(
  970. "2011-09-01",
  971. "2011-09-02",
  972. "2011-09-03",
  973. "2011-09-04",
  974. "2011-09-05",
  975. "2011-09-06",
  976. "2011-09-07",
  977. "2011-09-08",
  978. "2011-09-09",
  979. "2011-09-10",
  980. "2011-09-11",
  981. "2011-09-12",
  982. "2011-09-13",
  983. "2011-09-14",
  984. "2011-09-15",
  985. "2011-09-16",
  986. "2011-09-17",
  987. "2011-09-18",
  988. "2011-09-19",
  989. "2011-09-20",
  990. "2011-09-21",
  991. "2011-09-22",
  992. "2011-09-23",
  993. "2011-09-24",
  994. "2011-09-25",
  995. "2011-09-26",
  996. "2011-09-27",
  997. "2011-09-28",
  998. "2011-09-29",
  999. "2011-09-30",
  1000. ));
  1001. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  1002. $this->assertEquals($correct, $range->toString());
  1003. }
  1004. /**
  1005. * @group Core
  1006. */
  1007. public function test_CustomRange_startsWithWeek_EndsWithMonth()
  1008. {
  1009. $range = new Range('range', '2011-07-25,2011-08-31');
  1010. $correct = array(
  1011. array(
  1012. '2011-07-25',
  1013. '2011-07-26',
  1014. '2011-07-27',
  1015. '2011-07-28',
  1016. '2011-07-29',
  1017. '2011-07-30',
  1018. '2011-07-31',
  1019. ),
  1020. array(
  1021. "2011-08-01",
  1022. "2011-08-02",
  1023. "2011-08-03",
  1024. "2011-08-04",
  1025. "2011-08-05",
  1026. "2011-08-06",
  1027. "2011-08-07",
  1028. "2011-08-08",
  1029. "2011-08-09",
  1030. "2011-08-10",
  1031. "2011-08-11",
  1032. "2011-08-12",
  1033. "2011-08-13",
  1034. "2011-08-14",
  1035. "2011-08-15",
  1036. "2011-08-16",
  1037. "2011-08-17",
  1038. "2011-08-18",
  1039. "2011-08-19",
  1040. "2011-08-20",
  1041. "2011-08-21",
  1042. "2011-08-22",
  1043. "2011-08-23",
  1044. "2011-08-24",
  1045. "2011-08-25",
  1046. "2011-08-26",
  1047. "2011-08-27",
  1048. "2011-08-28",
  1049. "2011-08-29",
  1050. "2011-08-30",
  1051. "2011-08-31",
  1052. ));
  1053. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  1054. $this->assertEquals($correct, $range->toString());
  1055. }
  1056. /**
  1057. * @group Core
  1058. */
  1059. public function testCustomRangeBeforeIsAfterYearRight()
  1060. {
  1061. try {
  1062. $range = new Range('range', '2007-02-09,2007-02-01');
  1063. $this->assertEquals(0, $range->getNumberOfSubperiods());
  1064. $this->assertEquals(array(), $range->toString());
  1065. $range->getPrettyString();
  1066. } catch (Exception $e) {
  1067. return;
  1068. }
  1069. $this->fail('Expected exception not raised');
  1070. }
  1071. /**
  1072. * @group Core
  1073. */
  1074. public function testCustomRangeLastN()
  1075. {
  1076. $range = new Range('range', 'last4');
  1077. $range->setDefaultEndDate(Date::factory('2008-01-03'));
  1078. $correct = array(
  1079. '2007-12-31',
  1080. '2008-01-01',
  1081. '2008-01-02',
  1082. '2008-01-03',
  1083. );
  1084. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  1085. $this->assertEquals($correct, $range->toString());
  1086. }
  1087. /**
  1088. * @group Core
  1089. */
  1090. public function testCustomRangePreviousN()
  1091. {
  1092. $range = new Range('range', 'previous3');
  1093. $range->setDefaultEndDate(Date::factory('2008-01-03'));
  1094. $correct = array(
  1095. '2007-12-31',
  1096. '2008-01-01',
  1097. '2008-01-02',
  1098. );
  1099. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  1100. $this->assertEquals($correct, $range->toString());
  1101. }
  1102. /**
  1103. * @group Core
  1104. */
  1105. public function testCustomRangePreviousNEndToday()
  1106. {
  1107. $range = new Range('range', 'previous3');
  1108. $correct = array(
  1109. date('Y-m-d', time() - 86400 * 3),
  1110. date('Y-m-d', time() - 86400 * 2),
  1111. date('Y-m-d', time() - 86400 * 1),
  1112. );
  1113. $this->assertEquals(count($correct), $range->getNumberOfSubperiods());
  1114. $this->assertEquals($correct, $range->toString());
  1115. }
  1116. /**
  1117. * @group Core
  1118. */
  1119. public function testInvalidRangeThrows()
  1120. {
  1121. try {
  1122. $range = new Range('range', '0001-01-01,today');
  1123. $range->getLocalizedLongString();
  1124. } catch (Exception $e) {
  1125. return;
  1126. }
  1127. $this->fail('Expected exception not raised');
  1128. }
  1129. /**
  1130. * @group Core
  1131. */
  1132. public function testGetLocalizedShortString()
  1133. {
  1134. Translate::loadEnglishTranslation();
  1135. $month = new Range('range', '2000-12-09,2001-02-01');
  1136. $shouldBe = '9 Dec 00 - 1 Feb 01';
  1137. $this->assertEquals($shouldBe, $month->getLocalizedShortString());
  1138. }
  1139. /**
  1140. * @group Core
  1141. */
  1142. public function testGetLocalizedLongString()
  1143. {
  1144. Translate::loadEnglishTranslation();
  1145. $month = new Range('range', '2023-05-09,2023-05-21');
  1146. $shouldBe = '8 May 23 - 21 May 23';
  1147. $this->assertEquals($shouldBe, $month->getLocalizedLongString());
  1148. }
  1149. /**
  1150. * @group Core
  1151. */
  1152. public function testGetPrettyString()
  1153. {
  1154. Translate::loadEnglishTranslation();
  1155. $month = new Range('range', '2007-02-09,2007-03-15');
  1156. $shouldBe = 'From 2007-02-09 to 2007-03-15';
  1157. $this->assertEquals($shouldBe, $month->getPrettyString());
  1158. }
  1159. /**
  1160. * Data provider for testLastNLimits.
  1161. */
  1162. public function getDataForLastNLimitsTest()
  1163. {
  1164. return array(array('day', 5 * 365 + 1, 5 * 365),
  1165. array('week', 10 * 52 + 1, 10 * 52),
  1166. array('month', 121, 120),
  1167. array('year', 11, 10));
  1168. }
  1169. /**
  1170. * @group Core
  1171. *
  1172. *
  1173. * @dataProvider getDataForLastNLimitsTest
  1174. */
  1175. public function testLastNLimits($period, $lastN, $expectedLastN)
  1176. {
  1177. $range = new Range($period, 'last' . $lastN);
  1178. $this->assertEquals($expectedLastN, $range->getNumberOfSubperiods());
  1179. }
  1180. }