Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PeriodTest.php « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 76af0c719e4637fe0ce941e028399e8ec40b962b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
 * 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;

use Piwik\Date;
use Piwik\Period\Day;
use Piwik\Period;
use Piwik\Period\Month;
use Piwik\Period\Week;
use Piwik\Period\Year;
use Piwik\Translation\Translator;

/**
 * @group Core
 */
class PeriodTest extends \PHPUnit_Framework_TestCase
{
    public function testGetId()
    {
        $period = new Day(Date::today());
        $this->assertNotEquals(0, $period->getId());
        $period = new Week(Date::today());
        $this->assertNotEquals(0, $period->getId());
        $period = new Month(Date::today());
        $this->assertNotEquals(0, $period->getId());
        $period = new Year(Date::today());
        $this->assertNotEquals(0, $period->getId());
    }

    public function testGetLabel()
    {
        $period = new Day(Date::today());
        $label = $period->getLabel();
        $this->assertInternalType('string', $label);
        $this->assertNotEmpty($label);
        $period = new Week(Date::today());
        $label = $period->getLabel();
        $this->assertInternalType('string', $label);
        $this->assertNotEmpty($label);
        $period = new Month(Date::today());
        $label = $period->getLabel();
        $this->assertInternalType('string', $label);
        $this->assertNotEmpty($label);
        $period = new Year(Date::today());
        $label = $period->getLabel();
        $this->assertInternalType('string', $label);
        $this->assertNotEmpty($label);
    }

    public function testValidate_ValidDates()
    {
        Period::checkDateFormat('today');
        Period::checkDateFormat('yesterday');
        Period::checkDateFormat('yesterdaySameTime');
        Period::checkDateFormat('now');
        Period::checkDateFormat('2013-01-01,2013-02-02');
        Period::checkDateFormat('2013-01-01,today');
        Period::checkDateFormat('last7');
        Period::checkDateFormat('previous30');
        Period::checkDateFormat('+1 day');
        Period::checkDateFormat('next Thursday');
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Date format must be: YYYY-MM-DD, or 'today' or 'yesterday' or any keyword supported by the strtotime function (see http://php.net/strtotime for more information):
     * @dataProvider getInvalidDateFormats
     */
    public function testValidate_InvalidDates($invalidDateFormat)
    {
        Period::checkDateFormat($invalidDateFormat);
    }

    public function getInvalidDateFormats()
    {
        return array(
            array('last7testfoobar'),
            array('today,last7'),
            array('2013-01-01,last7'),
            array('today,2013-01-01'),
            array('1990-01-0111'),
            array('foobar'),
        );
    }
    
    /**
     * @expectedException \Exception
     * @expectedExceptionMessage is a date before first website was online. Try date that's after
     * @dataProvider getInvalidDatesBeforeFirstWebsite
     */
    public function testValidate_InvalidDatesBeforeFirstWebsite($invalidDatesBeforeFirstWebsite)
    {
        Period::checkDateFormat($invalidDatesBeforeFirstWebsite);
    }
    
    public function getInvalidDatesBeforeFirstWebsite()
    {
        return array(
            array('1990-01-01'),
            array(3434),
        );
    }

    /**
     * @dataProvider getTestDataForGetAllOverlappingChildPeriods
     */
    public function test_getAllOverlappingChildPeriods_ReturnsTheCorrectChildPeriods($periodType, $dateRange, $expectedChildPeriodRanges)
    {
        $period = Period\Factory::build($periodType, $dateRange);

        $overlappingPeriods = $period->getAllOverlappingChildPeriods();
        $overlappingPeriods = $this->getPeriodInfoForAssert($overlappingPeriods);

        $this->assertEquals($expectedChildPeriodRanges, $overlappingPeriods);
    }

    public function getTestDataForGetAllOverlappingChildPeriods()
    {
        return array(
            array(
                'month',
                '2015-09-10',
                array(
                    array('week', '2015-08-31,2015-09-06'),
                    array('week', '2015-09-07,2015-09-13'),
                    array('week', '2015-09-14,2015-09-20'),
                    array('week', '2015-09-21,2015-09-27'),
                    array('week', '2015-09-28,2015-10-04'),
                    array('day', '2015-09-01,2015-09-01'),
                    array('day', '2015-09-02,2015-09-02'),
                    array('day', '2015-09-03,2015-09-03'),
                    array('day', '2015-09-04,2015-09-04'),
                    array('day', '2015-09-05,2015-09-05'),
                    array('day', '2015-09-06,2015-09-06'),
                    array('day', '2015-09-07,2015-09-07'),
                    array('day', '2015-09-08,2015-09-08'),
                    array('day', '2015-09-09,2015-09-09'),
                    array('day', '2015-09-10,2015-09-10'),
                    array('day', '2015-09-11,2015-09-11'),
                    array('day', '2015-09-12,2015-09-12'),
                    array('day', '2015-09-13,2015-09-13'),
                    array('day', '2015-09-14,2015-09-14'),
                    array('day', '2015-09-15,2015-09-15'),
                    array('day', '2015-09-16,2015-09-16'),
                    array('day', '2015-09-17,2015-09-17'),
                    array('day', '2015-09-18,2015-09-18'),
                    array('day', '2015-09-19,2015-09-19'),
                    array('day', '2015-09-20,2015-09-20'),
                    array('day', '2015-09-21,2015-09-21'),
                    array('day', '2015-09-22,2015-09-22'),
                    array('day', '2015-09-23,2015-09-23'),
                    array('day', '2015-09-24,2015-09-24'),
                    array('day', '2015-09-25,2015-09-25'),
                    array('day', '2015-09-26,2015-09-26'),
                    array('day', '2015-09-27,2015-09-27'),
                    array('day', '2015-09-28,2015-09-28'),
                    array('day', '2015-09-29,2015-09-29'),
                    array('day', '2015-09-30,2015-09-30'),
                ),
            ),

            array(
                'week',
                '2015-09-03',
                array(
                    array('day', '2015-08-31,2015-08-31'),
                    array('day', '2015-09-01,2015-09-01'),
                    array('day', '2015-09-02,2015-09-02'),
                    array('day', '2015-09-03,2015-09-03'),
                    array('day', '2015-09-04,2015-09-04'),
                    array('day', '2015-09-05,2015-09-05'),
                    array('day', '2015-09-06,2015-09-06'),
                ),
            ),

            array(
                'day',
                '2015-09-05',
                array(),
            ),
        );
    }

    private function getPeriodInfoForAssert($periods)
    {
        return array_map(function (Period $period) {
            return array($period->getLabel(), $period->getRangeString());
        }, $periods);
    }
}