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

DayTest.php « Period « Core « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a55298569f3c396eabee7f87089952d6b7daa53 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
use Piwik\Date;
use Piwik\Period\Day;
use Piwik\Translate;

/**
 * Testing Period_Day
 */
class Period_DayTest extends PHPUnit_Framework_TestCase
{
    /**
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testInvalidDate()
    {
        try {
            $period = new Day('Invalid Date');
        } catch (Exception $e) {
            return;
        }
        $this->fail('Expected Exception not raised');
    }

    /**
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testToString()
    {
        $period = new Day(Date::today());
        $this->assertEquals(date("Y-m-d"), $period->getPrettyString());
        $this->assertEquals(date("Y-m-d"), (string)$period);
        $this->assertEquals(date("Y-m-d"), $period->toString());
    }

    /**
     * today is NOT finished
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayIsFinishedToday()
    {
        $period = new Day(Date::today());
        $this->assertEquals(date("Y-m-d"), $period->toString());
        $this->assertEquals(array(), $period->getSubperiods());
        $this->assertEquals(0, $period->getNumberOfSubperiods());
    }

    /**
     * yesterday 23:59:59 is finished
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayIsFinishedYesterday()
    {

        $period = new Day(Date::yesterday());
        $this->assertEquals(date("Y-m-d", time() - 86400), $period->toString());
        $this->assertEquals(array(), $period->getSubperiods());
        $this->assertEquals(0, $period->getNumberOfSubperiods());
    }

    /**
     * tomorrow is not finished
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayIsFinishedTomorrow()
    {
        $period = new Day(Date::factory(date("Y-m-d", time() + 86400)));
        $this->assertEquals(date("Y-m-d", time() + 86400), $period->toString());
        $this->assertEquals(array(), $period->getSubperiods());
        $this->assertEquals(0, $period->getNumberOfSubperiods());
    }

    /**
     * test day doesnt exist 31st feb
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayIsFinished31stfeb()
    {
        $period = new Day(Date::factory("2007-02-31"));
        $this->assertEquals("2007-03-03", $period->toString());
        $this->assertEquals(array(), $period->getSubperiods());
        $this->assertEquals(0, $period->getNumberOfSubperiods());
    }

    /**
     * test date that doesn't exist, should return the corresponding correct date
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateStart1()
    {
        // create the period
        $period = new Day(Date::factory("2007-02-31"));

        // start date
        $startDate = $period->getDateStart();

        // expected string
        $this->assertEquals("2007-03-03", $startDate->toString());

        // check that for a day, getDateStart = getStartEnd
        $this->assertEquals($startDate, $period->getDateEnd());
    }

    /**
     * test normal date
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateStart2()
    {
        // create the period
        $period = new Day(Date::factory("2007-01-03"));

        // start date
        $startDate = $period->getDateStart();

        // expected string
        $this->assertEquals("2007-01-03", $startDate->toString());

        // check that for a day, getDateStart = getStartEnd
        $this->assertEquals($startDate, $period->getDateEnd());
    }

    /**
     * test last day of year
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateStart3()
    {
        // create the period
        $period = new Day(Date::factory("2007-12-31"));

        // start date
        $startDate = $period->getDateStart();

        // expected string
        $this->assertEquals("2007-12-31", $startDate->toString());

        // check that for a day, getDateStart = getStartEnd
        $this->assertEquals($startDate, $period->getDateEnd());
    }

    /**
     * test date that doesn't exist, should return the corresponding correct date
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateEnd1()
    {
        // create the period
        $period = new Day(Date::factory("2007-02-31"));

        // end date
        $endDate = $period->getDateEnd();

        // expected string
        $this->assertEquals("2007-03-03", $endDate->toString());
    }

    /**
     * test normal date
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateEnd2()
    {
        // create the period
        $period = new Day(Date::factory("2007-04-15"));

        // end date
        $endDate = $period->getDateEnd();

        // expected string
        $this->assertEquals("2007-04-15", $endDate->toString());
    }

    /**
     * test last day of year
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testDayGetDateEnd3()
    {
        // create the period
        $period = new Day(Date::factory("2007-12-31"));

        // end date
        $endDate = $period->getDateEnd();

        // expected string
        $this->assertEquals("2007-12-31", $endDate->toString());
    }

    /**
     * adding a subperiod should not be possible
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testAddSubperiodFails()
    {
        // create the period
        $period = new Day(Date::factory("2007-12-31"));

        try {
            $period->addSubperiod('');
        } catch (Exception $e) {
            return;
        }
        // expected string
        $this->fail('Exception not raised');
    }

    /**
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testGetLocalizedShortString()
    {
        Translate::getInstance()->loadEnglishTranslation();
        $month = new Day(Date::factory('2024-10-09'));
        $shouldBe = 'Wed 9 Oct';
        $this->assertEquals($shouldBe, $month->getLocalizedShortString());
    }

    /**
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testGetLocalizedLongString()
    {
        Translate::getInstance()->loadEnglishTranslation();
        $month = new Day(Date::factory('2024-10-09'));
        $shouldBe = 'Wednesday 9 October 2024';
        $this->assertEquals($shouldBe, $month->getLocalizedLongString());
    }

    /**
     * @group Core
     * @group Period
     * @group Period_Day
     */
    public function testGetPrettyString()
    {
        Translate::getInstance()->loadEnglishTranslation();
        $month = new Day(Date::factory('2024-10-09'));
        $shouldBe = '2024-10-09';
        $this->assertEquals($shouldBe, $month->getPrettyString());
    }
}