Newer
Older
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit\Period;
use Exception;
use Piwik\Period\Month;
use Piwik\Period\Range;
mattab
a validé
class RangeTest extends BasePeriodTest
$range = new Range('day', 'last1');
$today = Date::today();
$correct = array(
$today->toString(),
);
$correct = array_reverse($correct);
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
{
// 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
$range = new Range('day', 'last1', 'UTC+23');
$today = Date::now()->addHour(23);
$correct = array(
$today->toString(),
);
$correct = array_reverse($correct);
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('day', 'last2');
$today = Date::today();
$correct = array(
$today->toString(),
$today->subDay(1)->toString()
);
$correct = array_reverse($correct);
$this->assertEquals(2, $range->getNumberOfSubperiods());
$range = new Range('day', 'last50');
$today = Date::today();
for ($i = 0; $i < 50; $i++) {
$correct[] = $today->subDay($i)->toString();
$this->assertEquals(50, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('day', 'previous3');
$yesterday = Date::yesterday();
for ($i = 0; $i < 3; $i++) {
$correct[] = $yesterday->subDay($i)->toString();
$this->assertEquals(3, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('day', '2008-01-01,2008-01-03');
'2008-01-01',
'2008-01-02',
'2008-01-03',
);
$this->assertEquals(3, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
// test range date1,date2
$rangeString = '2007-12-22,2008-01-03';
$range = new Range('day', $rangeString);
'2007-12-22',
'2007-12-23',
'2007-12-24',
'2007-12-25',
'2007-12-26',
'2007-12-27',
'2007-12-28',
'2007-12-29',
'2007-12-30',
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
);
$this->assertEquals(13, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
$this->assertEquals($rangeString, $range->getRangeString());
$range = new Range('week', '2007-12-22,2008-01-03');
$range2String = '2007-12-19,2008-01-03';
$range2 = new Range('week', $range2String);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
array(
'2007-12-17',
'2007-12-18',
'2007-12-19',
'2007-12-20',
'2007-12-21',
'2007-12-22',
'2007-12-23',
),
array(
'2007-12-24',
'2007-12-25',
'2007-12-26',
'2007-12-27',
'2007-12-28',
'2007-12-29',
'2007-12-30',
),
array(
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
'2008-01-04',
'2008-01-05',
'2008-01-06',
)
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
benakamoorthi
a validé
$this->assertEquals(count($correct), $range2->getNumberOfSubperiods());
benakamoorthi
a validé
$this->assertEquals($correct, $range2->toString());
$this->assertEquals('2007-12-17,2008-01-06' , $range2->getRangeString());
$range = new Range('year', '2006-12-22,2007-01-03');
array(
0 => '2006-01-01',
1 => '2006-02-01',
2 => '2006-03-01',
3 => '2006-04-01',
4 => '2006-05-01',
5 => '2006-06-01',
6 => '2006-07-01',
7 => '2006-08-01',
8 => '2006-09-01',
9 => '2006-10-01',
),
1 =>
array(
0 => '2007-01-01',
1 => '2007-02-01',
2 => '2007-03-01',
3 => '2007-04-01',
4 => '2007-05-01',
5 => '2007-06-01',
6 => '2007-07-01',
7 => '2007-08-01',
8 => '2007-09-01',
9 => '2007-10-01',
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
$this->assertEquals('2006-01-01,2007-12-31', $range->getRangeString());
$range = new Range('month', '2006-12-22,2007-01-03');
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
array(
'2006-12-01',
'2006-12-02',
'2006-12-03',
'2006-12-04',
'2006-12-05',
'2006-12-06',
'2006-12-07',
'2006-12-08',
'2006-12-09',
'2006-12-10',
'2006-12-11',
'2006-12-12',
'2006-12-13',
'2006-12-14',
'2006-12-15',
'2006-12-16',
'2006-12-17',
'2006-12-18',
'2006-12-19',
'2006-12-20',
'2006-12-21',
'2006-12-22',
'2006-12-23',
'2006-12-24',
'2006-12-25',
'2006-12-26',
'2006-12-27',
'2006-12-28',
'2006-12-29',
'2006-12-30',
'2006-12-31',
),
array(
'2007-01-01',
'2007-01-02',
'2007-01-03',
'2007-01-04',
'2007-01-05',
'2007-01-06',
'2007-01-07',
'2007-01-08',
'2007-01-09',
'2007-01-10',
'2007-01-11',
'2007-01-12',
'2007-01-13',
'2007-01-14',
'2007-01-15',
'2007-01-16',
'2007-01-17',
'2007-01-18',
'2007-01-19',
'2007-01-20',
'2007-01-21',
'2007-01-22',
'2007-01-23',
'2007-01-24',
'2007-01-25',
'2007-01-26',
'2007-01-27',
'2007-01-28',
'2007-01-29',
'2007-01-30',
'2007-01-31',
),
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
$this->assertEquals('2006-12-01,2007-01-31', $range->getRangeString());
$range = new Range('week', 'last50');
$today = Date::today();
for ($i = 0; $i < 50; $i++) {
$date = $today->subDay($i * 7);
$week = new Week($date);
$correct[] = $week->toString();
$this->assertEquals(50, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('week', 'last1');
$currentWeek = new Week(Date::today());
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals(array($currentWeek->toString()), $range->toString());
}
$range = new Range('month', 'last20');
$today = Date::today();
for ($i = 0; $i < 20; $i++) {
$week = new Month($date);
$correct[] = $week->toString();
$this->assertEquals(20, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('month', 'last1');
$month = new Month(Date::today());
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals(array($month->toString()), $range->toString());
}
$range = new Range('month', 'previous10');
$end = Date::today();
for ($i = 0; $i < 10; $i++) {
$week = new Month($date);
$correct[] = $week->toString();
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testRangePreviousmonth_onLastDayOfMonth()
{
$end = Date::factory('2013-10-31');
$range = new Range('month', 'previous10', 'UTC', $end);
$end = $end->subMonth(1);
$correct = array();
for ($i = 0; $i < 10; $i++) {
$date = $end->subMonth($i);
$week = new Month($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
public function testRangePreviousweek_onLastDayOfWeek()
{
$end = Date::factory('2013-11-03');
$range = new Range('week', 'previous2', 'UTC', $end);
$end = $end->subWeek(1);
$correct = array();
for ($i = 0; $i < 2; $i++) {
$date = $end->subWeek($i);
$week = new Week($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals($correct, $range->toString());
}
public function testRangePreviousweek_onFirstDayOfWeek()
{
$end = Date::factory('2013-11-04');
$range = new Range('week', 'previous2', 'UTC', $end);
$end = $end->subWeek(1);
$correct = array();
for ($i = 0; $i < 2; $i++) {
$date = $end->subWeek($i);
$week = new Week($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals($correct, $range->toString());
}
public function testRangeLastweek_onFirstDayOfWeek()
{
$end = Date::factory('2013-11-04');
$range = new Range('week', 'last2', 'UTC', $end);
$correct = array();
for ($i = 0; $i < 2; $i++) {
$date = $end->subWeek($i);
$week = new Week($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals($correct, $range->toString());
}
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
public function testRangeLastmonth_onLastDayOfMonth()
{
$end = Date::factory('2013-10-31');
$range = new Range('month', 'last10', 'UTC', $end);
$correct = array();
for ($i = 0; $i < 10; $i++) {
$date = $end->subMonth($i);
$week = new Month($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function _testRangePreviousmonth_onFirstOfMonth()
{
$end = Date::factory('2013-11-01');
$range = new Range('month', 'previous10', 'UTC', $end);
$end = $end->subMonth(1);
$correct = array();
for ($i = 0; $i < 10; $i++) {
$date = $end->subMonth($i);
$week = new Month($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function _testRangeLastmonth_onFirstOfMonth()
{
$end = Date::factory('2013-11-01');
$range = new Range('month', 'last10', 'UTC', $end);
$correct = array();
for ($i = 0; $i < 10; $i++) {
$date = $end->subMonth($i);
$week = new Month($date);
$correct[] = $week->toString();
}
$correct = array_reverse($correct);
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('year', 'last10');
$today = Date::today();
for ($i = 0; $i < 10; $i++) {
$date = $today->subMonth(12 * $i);
$week = new Year($date);
$correct[] = $week->toString();
$this->assertEquals(10, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('year', 'last1');
$currentYear = new Year(Date::today());
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals(array($currentYear->toString()), $range->toString());
}
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
public function testCustomRangeYearUsesYearIfPossible()
{
$range = new Range('range', '2005-12-17,2008-01-03', 'UTC', Date::factory('2008-01-03'));
$year2006 = new Year(Date::factory('2006-02-02'));
$year2007 = new Year(Date::factory('2007-02-02'));
$year2008 = new Year(Date::factory('2008-02-02'));
$correct = array(
'2005-12-17',
'2005-12-18',
array (
"2005-12-19",
"2005-12-20",
"2005-12-21",
"2005-12-22",
"2005-12-23",
"2005-12-24",
"2005-12-25"
),
"2005-12-26",
"2005-12-27",
"2005-12-28",
"2005-12-29",
"2005-12-30",
"2005-12-31",
$year2006->toString(),
$year2007->toString(),
$year2008->toString()
);
$this->assertEquals(12, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeIsYear_UsesFullYear()
{
$range = new Range('range', '2011-01-01,2011-12-31', 'UTC', Date::factory('2012-01-03'));
$year2011 = new Year(Date::factory('2011-02-02'));
$correct = array(
$year2011->toString()
);
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeYear_UsesCurrentYear()
{
$rangeString = '2013-01-01,2013-11-01';
$range = new Range('range', $rangeString, 'UTC', Date::factory('2013-11-01'));
$year2013 = new Year(Date::factory('2013-02-02'));
$correct = array(
$year2013->toString()
);
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
$this->assertEquals($correct, $range->toString());
$this->assertEquals($rangeString, $range->getRangeString());
}
public function testCustomRangeYearUsesCurrentYear_onLastDayOfYear()
{
$range = new Range('range', '2013-01-01,2013-12-31', 'UTC', Date::factory('2013-12-31'));
$year2013 = new Year(Date::factory('2013-01-01'));
$correct = array(
$year2013->toString()
);
$this->assertEquals(1, $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeWeekInsideEndingToday()
$range = new Range('range', '2007-12-22,2008-01-03', 'UTC', Date::factory('2008-01-03'));
$correct = array(
'2007-12-22',
'2007-12-23',
array(
'2007-12-24',
'2007-12-25',
'2007-12-26',
'2007-12-27',
'2007-12-28',
'2007-12-29',
'2007-12-30',
),
array(
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
'2008-01-04',
'2008-01-05',
'2008-01-06',
)
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testRangeEndDateIsTodayAndStartDateNotStartOfTheWeek()
{
$range = new Range('range', '2013-10-29,2013-10-30', 'UTC', Date::factory('2013-10-30'));
$correct = array(
'2013-10-29',
'2013-10-30'
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
Thomas Steur
a validé
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
public function testRangeEndDateIsInFuture()
{
$range = new Range('range', '2013-10-29,2013-10-31', 'UTC', Date::factory('2013-10-30'));
$correct = array(
'2013-10-29',
'2013-10-30',
'2013-10-31'
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testRangePreviousmonthEndDateIsInFutureAndEndOfTheWeek()
{
$range = new Range('range', '2013-10-29,2013-11-03', 'UTC', Date::factory('2013-10-30'));
$correct = array(
'2013-10-29',
'2013-10-30',
'2013-10-31',
'2013-11-01',
'2013-11-02',
'2013-11-03',
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeWeekInsideEndingYesterday()
Date::factory('2008-01-04'),
Date::factory('2008-01-05'),
Date::factory('2008-01-14'),
Date::factory('2008-02-14'),
Date::factory('2009-02-14'),
foreach ($todays as $today) {
$range = new Range('range', '2007-12-22,2008-01-03', 'UTC', $today);
$correct = array(
'2007-12-22',
'2007-12-23',
array(
'2007-12-24',
'2007-12-25',
'2007-12-26',
'2007-12-27',
'2007-12-28',
'2007-12-29',
'2007-12-30',
),
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
}
public function testCustomRangeOnlyDaysLessThanOneWeek()
$range = new Range('range', '2007-12-30,2008-01-01');
$correct = array(
'2007-12-30',
'2007-12-31',
'2008-01-01',
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
$range = new Range('range', '2007-12-31,2008-01-06');
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
'2008-01-04',
'2008-01-05',
'2008-01-06',
)
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeStartsWithWeek()
$range = new Range('range', '2007-12-31,2008-01-08');
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
'2008-01-04',
'2008-01-05',
'2008-01-06',
),
'2008-01-07',
'2008-01-08',
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeEndsWithWeek()
$range = new Range('range', '2007-12-21,2008-01-06');
$correct = array(
'2007-12-21',
'2007-12-22',
'2007-12-23',
array(
'2007-12-24',
'2007-12-25',
'2007-12-26',
'2007-12-27',
'2007-12-28',
'2007-12-29',
'2007-12-30',
'2007-12-31',
'2008-01-01',
'2008-01-02',
'2008-01-03',
'2008-01-04',
'2008-01-05',
'2008-01-06',
),
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeContainsMonthAndWeek()
$range = new Range('range', '2011-09-18,2011-11-02', 'UTC', Date::factory('2012-01-01'));
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
'2011-09-18',
array(
'2011-09-19',
'2011-09-20',
'2011-09-21',
'2011-09-22',
'2011-09-23',
'2011-09-24',
'2011-09-25',
),
'2011-09-26',
'2011-09-27',
'2011-09-28',
'2011-09-29',
'2011-09-30',
array(
"2011-10-01",
"2011-10-02",
"2011-10-03",
"2011-10-04",
"2011-10-05",
"2011-10-06",
"2011-10-07",
"2011-10-08",
"2011-10-09",
"2011-10-10",
"2011-10-11",
"2011-10-12",
"2011-10-13",
"2011-10-14",
"2011-10-15",
"2011-10-16",
"2011-10-17",
"2011-10-18",
"2011-10-19",
"2011-10-20",
"2011-10-21",
"2011-10-22",
"2011-10-23",
"2011-10-24",
"2011-10-25",
"2011-10-26",
"2011-10-27",
"2011-10-28",
"2011-10-29",
"2011-10-30",
"2011-10-31",
),
"2011-11-01",
"2011-11-02",
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
public function testCustomRangeContainsSeveralMonthsAndWeeksStartingWithMonth()
{
// Testing when "today" is in the same month, or later in the future
$todays = array(
Date::factory('2011-10-18'),
Date::factory('2011-10-19'),
Date::factory('2011-10-24'),
Date::factory('2011-11-01'),
Date::factory('2011-11-30'),
Date::factory('2011-12-31'),
Date::factory('2021-10-18')
foreach ($todays as $today) {
$range = new Range('range', '2011-08-01,2011-10-17', 'UTC', $today);
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
array(
"2011-08-01",
"2011-08-02",
"2011-08-03",
"2011-08-04",
"2011-08-05",
"2011-08-06",
"2011-08-07",
"2011-08-08",
"2011-08-09",
"2011-08-10",
"2011-08-11",
"2011-08-12",
"2011-08-13",
"2011-08-14",
"2011-08-15",
"2011-08-16",
"2011-08-17",
"2011-08-18",
"2011-08-19",
"2011-08-20",
"2011-08-21",
"2011-08-22",
"2011-08-23",
"2011-08-24",
"2011-08-25",
"2011-08-26",
"2011-08-27",
"2011-08-28",
"2011-08-29",
"2011-08-30",
"2011-08-31",
),
array(
"2011-09-01",
"2011-09-02",
"2011-09-03",
"2011-09-04",
"2011-09-05",
"2011-09-06",
"2011-09-07",
"2011-09-08",
"2011-09-09",
"2011-09-10",
"2011-09-11",
"2011-09-12",
"2011-09-13",
"2011-09-14",
"2011-09-15",
"2011-09-16",
"2011-09-17",
"2011-09-18",
"2011-09-19",
"2011-09-20",
"2011-09-21",
"2011-09-22",
"2011-09-23",
"2011-09-24",
"2011-09-25",
"2011-09-26",
"2011-09-27",
"2011-09-28",
"2011-09-29",
"2011-09-30",
),
"2011-10-01",
"2011-10-02",
array(
"2011-10-03",
"2011-10-04",
"2011-10-05",
"2011-10-06",
"2011-10-07",
"2011-10-08",
"2011-10-09",
),
array(
"2011-10-10",
"2011-10-11",
"2011-10-12",
"2011-10-13",
"2011-10-14",
"2011-10-15",
"2011-10-16",
),
"2011-10-17",
);
$this->assertEquals(count($correct), $range->getNumberOfSubperiods());
$this->assertEquals($correct, $range->toString());
}
}
public function testCustomRangeOneMonthOnly()
{
$range = new Range('range', '2011-09-01,2011-09-30');
$correct = array(
array(
"2011-09-01",
"2011-09-02",
"2011-09-03",
"2011-09-04",
"2011-09-05",
"2011-09-06",
"2011-09-07",
"2011-09-08",
"2011-09-09",
"2011-09-10",
"2011-09-11",
"2011-09-12",
"2011-09-13",
"2011-09-14",
"2011-09-15",
"2011-09-16",
"2011-09-17",