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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit\Metrics;
use Piwik\Container\StaticContainer;
use Piwik\Metrics\Formatter;
use Piwik\NumberFormatter;
use Piwik\Translate;
use Piwik\Plugins\SitesManager\API as SitesManagerAPI;
/**
* @group Core
*/
class FormatterTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Formatter
*/
private $formatter;
private $sitesInfo;
public function setUp()
{
$this->sitesInfo = array(
1 => array(
'idsite' => '1',
'currency' => 'EUR'
),
2 => array(
'idsite' => '2',
'currency' => 'DKK'
),
3 => array(
'idsite' => '3',
'currency' => 'PLN'
),
4 => array(
'idsite' => '4',
'currency' => 'NZD'
),
5 => array(
'idsite' => '5',
'currency' => 'JPY'
)
);
$this->formatter = new Formatter();
Translate::loadAllTranslations();
$this->setSiteManagerApiMock();
}
public function tearDown()
{
Translate::reset();
NumberFormatter::getInstance()->clearCache();
$this->unsetSiteManagerApiMock();
}
/**
* @dataProvider getPrettyNumberTestData
*/
public function test_getPrettyNumber_ReturnsCorrectResult($number, $expected)
{
$this->assertEquals($expected, $this->formatter->getPrettyNumber($number, 2));
}
/**
* @dataProvider getPrettyNumberLocaleTestData
*/
public function test_getPrettyNumber_ReturnsCorrectResult_WhenLocaleIsEuropean($number, $expected)
{
StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage('de');
$this->assertEquals($expected, $this->formatter->getPrettyNumber($number, 2));
}
/**
* @dataProvider getPrettySizeFromBytesTestData
*/
public function test_getPrettySizeFromBytes_ReturnsCorrectResult($bytesSize, $unit, $expected)
{
$this->assertEquals($expected, $this->formatter->getPrettySizeFromBytes($bytesSize, $unit));
}
/**
* @dataProvider getPrettyMoneyTestData
*/
public function test_getPrettyMoney_ReturnsCorrectResult($value, $idSite, $language, $expected)
{
StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage($language);
$this->assertEquals($expected, $this->formatter->getPrettyMoney($value, $idSite));
}
/**
* @dataProvider getPrettyPercentFromQuotientTestData
*/
public function test_getPrettyPercentFromQuotient_ReturnsCorrectResult($value, $language, $expected)
{
StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage($language);
$this->assertEquals($expected, $this->formatter->getPrettyPercentFromQuotient($value));
}
/**
* @dataProvider getPrettyTimeFromSecondsData
*/
public function test_getPrettyTimeFromSeconds_ReturnsCorrectResult($seconds, $expected)
{
if (($seconds * 100) > PHP_INT_MAX || ($seconds * 100 * -1) > PHP_INT_MAX) {
$this->markTestSkipped("Will not pass on 32-bit machine.");
}
$sentenceExpected = $expected[0];
$this->assertEquals($sentenceExpected, $this->formatter->getPrettyTimeFromSeconds($seconds, $sentence = true));
$numericExpected = $expected[1];
$this->assertEquals($numericExpected, $this->formatter->getPrettyTimeFromSeconds($seconds, $sentence = false));
}
public function getPrettyNumberTestData()
{
return array(
array(0.14, '0.14'),
array(0.14567, '0.15'),
array(100.1234, '100.12'),
array(1000.45, '1,000.45'),
array(23456789.00, '23,456,789')
);
}
public function getPrettyNumberLocaleTestData()
{
return array(
array(0.14, '0,14'),
array(0.14567, '0,15'),
array(100.1234, '100,12'),
array(1000.45, '1.000,45'),
array(23456789.00, '23.456.789'),
);
}
public function getPrettySizeFromBytesTestData()
{
return array(
array(767, null, '767 B'),
array(1024, null, '1 K'),
array(1536, null, '1.5 K'),
array(1024 * 1024, null, '1 M'),
array(1.25 * 1024 * 1024, null, '1.3 M'),
array(1.25 * 1024 * 1024 * 1024, null, '1.3 G'),
array(1.25 * 1024 * 1024 * 1024 * 1024, null, '1.3 T'),
array(1.25 * 1024 * 1024 * 1024 * 1024 * 1024, null, '1280 T'),
array(1.25 * 1024 * 1024, 'M', '1.3 M'),
array(1.25 * 1024 * 1024 * 1024, 'M', '1280 M'),
array(0, null, '0 M')
);
}
public function getPrettyMoneyTestData()
{
return array(
array(1, 1, 'en', '€1'),
array(1.045, 2, 'en', 'DKK1.05'),
array(1000.4445, 3, 'en', 'PLN1,000.44'),
array(1234.56, 4, 'en', 'NZ$1,234.56'),
array(234.76, 5, 'en', '¥234.76'),
array(234.76, 5, 'de', '234,76 ¥'),
array(234.76, 5, 'kr', '¥234.76'),
);
}
public function getPrettyPercentFromQuotientTestData()
{
return array(
array(100, 'en', '10,000%'),
array(1, 'en', '100%'),
array(.85, 'en', '85%'),
array(.89999, 'en', '89.999%'),
array(.0004, 'en', '0.04%'),
array(0.123, 'eu', '% 12,3'),
array(0.103, 'zh-cn', '10.3%'),
);
}
/**
* Dataprovider for testGetPrettyTimeFromSeconds
*/
public function getPrettyTimeFromSecondsData()
{
return array(
array(30, array('30s', '00:00:30')),
array(60, array('1 min 0s', '00:01:00')),
array(100, array('1 min 40s', '00:01:40')),
array(3600, array('1 hours 0 min', '01:00:00')),
array(3700, array('1 hours 1 min', '01:01:40')),
array(86400 + 3600 * 10, array('1 days 10 hours', '1 days 10:00:00')),
array(86400 * 365, array('365 days 0 hours', '365 days 00:00:00')),
array((86400 * (365.25 + 10)), array('1 years 10 days', '375 days 06:00:00')),
array(1.342, array('1.34s', '00:00:01.34')),
array(.342, array('0.34s', '00:00:00.34')),
array(.02, array('0.02s', '00:00:00.02')),
array(.002, array('0.002s', '00:00:00')),
array(1.002, array('1s', '00:00:01')),
array(1.02, array('1.02s', '00:00:01.02')),
array(1.2, array('1.2s', '00:00:01.20')),
array(122.1, array('2 min 2.1s', '00:02:02.10')),
array(-122.1, array('-2 min 2.1s', '-00:02:02.10')),
array(86400 * -365, array('-365 days 0 hours', '-365 days 00:00:00'))
);
}
private function unsetSiteManagerApiMock()
{
SitesManagerAPI::unsetInstance();
}
private function setSiteManagerApiMock()
{
$sitesInfo = $this->sitesInfo;
$mock = $this->getMock('stdClass', array('getSiteFromId'));
$mock->expects($this->any())->method('getSiteFromId')->willReturnCallback(function ($idSite) use ($sitesInfo) {
return $sitesInfo[$idSite];
});
SitesManagerAPI::setSingletonInstance($mock);
}
}
|