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

NumberFormatterTest.php « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44e5cf31589ac78797fb5e03c0b3f98f843bb850 (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
<?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\Integration;

use Piwik\Container\StaticContainer;
use Piwik\NumberFormatter;
use Piwik\Translation\Translator;

/**
 * @group Core
 * @group NumberFormatter
 */
class NumberFormatterTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Translator
     */
    private $translator;

    public function setUp(): void
    {
        \Piwik\Plugin\Manager::getInstance()->loadPluginTranslations();

        $this->translator = StaticContainer::get('Piwik\Translation\Translator');
    }

    public function tearDown(): void
    {
        $this->translator->reset();
    }

    /**
     * @dataProvider getFormatMethodTestData
     */
    public function test_format_CorrectlyFormatsValueAsNumberOrPercent(
        $language, $value, $maximumFractionDigits, $minimumFractionDigits, $expected)
    {
        $this->translator->setCurrentLanguage($language);
        $numberFormatter = new NumberFormatter($this->translator);
        $this->assertEquals($expected, $numberFormatter->format($value, $maximumFractionDigits,$minimumFractionDigits));
    }

    public function getFormatMethodTestData()
    {
        return array(
            // number formatting
            array('en', 5, 0, 0, '5'),
            array('en', -5, 0, 3, '-5'),
            array('en', 5.299, 0, 0, '5'),
            array('en', 5.299, 3, 0, '5.299'),
            array('en', sqrt(33), 2, 0, '5.74'),

            // percent formatting
            array('en', '5.299%', 0, 0, '5%'),
            array('en', '5.299%', 3, 0, '5.299%'),
        );
    }

    /**
     * @dataProvider getNumberFormattingTestData
     */
    public function testNumberFormatting($language, $value, $maximumFractionDigits, $minimumFractionDigits, $expected)
    {
        $this->translator->setCurrentLanguage($language);
        $numberFormatter = new NumberFormatter($this->translator);

        $this->assertSame($expected, $numberFormatter->formatNumber($value, $maximumFractionDigits, $minimumFractionDigits));
    }

    public function getNumberFormattingTestData()
    {
        return array(
            // english formats
            array('en', 5, 0, 0, '5'),
            array('en', -5, 0, 3, '-5'),
            array('en', 5.299, 0, 0, '5'),
            array('en', 5.299, 3, 0, '5.299'),
            array('en', -50, 3, 3, '-50.000'),
            array('en', 5000, 0, 0, '5,000'),
            array('en', 5000000, 0, 0, '5,000,000'),
            array('en', -5000000, 0, 0, '-5,000,000'),

            // foreign languages
            array('ar', 51239.56, 3, 0, '51٬239٫56'),
            array('be', 51239.56, 3, 0, '51 239,56'),
            array('de', 51239.56, 3, 0, '51.239,56'),
            array('bn', 152551239.56, 3, 0, '15,25,51,239.56'),
            array('hi', 152551239.56, 0, 0, '15,25,51,240'),
            array('lt', -152551239.56, 0, 0, '−152 551 240'),
        );
    }

    /**
     * @dataProvider getPercentNumberFormattingTestData
     */
    public function testPercentNumberFormatting($language, $value, $maximumFractionDigits, $minimumFractionDigits, $expected)
    {
        $this->translator->setCurrentLanguage($language);
        $numberFormatter = new NumberFormatter($this->translator);
        $this->assertEquals($expected, $numberFormatter->formatPercent($value, $maximumFractionDigits, $minimumFractionDigits));
    }

    public function getPercentNumberFormattingTestData()
    {
        return array(
            // english formats
            array('en', 5, 0, 0, '5%'),
            array('en', -5, 0, 3, '-5%'),
            array('en', 5.299, 0, 0, '5%'),
            array('en', 5.299, 3, 0, '5.299%'),
            array('en', -50, 3, 3, '-50.000%'),
            array('en', -50.1, 3, 3, '-50.100%'),
            array('en', 5000, 0, 0, '5,000%'),
            array('en', +5000, 0, 0, '5,000%'),
            array('en', 5000000, 0, 0, '5,000,000%'),
            array('en', -5000000, 0, 0, '-5,000,000%'),

            // foreign languages
            array('ar', 51239.56, 3, 0, '51٬239٫56٪؜'),
            array('be', 51239.56, 3, 0, '51 239,56 %'),
            array('de', 51239.56, 3, 0, '51.239,56 %'),
            array('bn', 152551239.56, 3, 0, '152,551,239.56%'),
            array('hi', 152551239.56, 0, 0, '15,25,51,240%'),
            array('lt', -152551239.56, 0, 0, '−152 551 240 %'),
        );
    }

    /**
     * @dataProvider getPercentNumberEvolutionFormattingTestData
     */
    public function testPercentEvolutionNumberFormatting($language, $value, $expected)
    {
        $this->translator->setCurrentLanguage($language);
        $numberFormatter = new NumberFormatter($this->translator);
        $this->assertEquals($expected, $numberFormatter->formatPercentEvolution($value));
    }

    public function getPercentNumberEvolutionFormattingTestData()
    {
        return array(
            // english formats
            array('en', 5, '+5%'),
            array('en', -5, '-5%'),
            array('en', 5.299, '+5%'),
            array('en', -50, '-50%'),
            array('en', 5000, '+5,000%'),
            array('en', +5000, '+5,000%'),
            array('en', 5000000, '+5,000,000%'),
            array('en', -5000000, '-5,000,000%'),
        );
    }

    public function testChangeLanguage()
    {
        $this->translator->setCurrentLanguage('en');
        $numberFormatter = new NumberFormatter($this->translator);
      
        $this->assertEquals('5,000.1', $numberFormatter->formatNumber(5000.1, 1));
        $this->assertEquals('50.1%', $numberFormatter->formatPercent(50.1, 1));
        $this->assertEquals('+50%', $numberFormatter->formatPercentEvolution(50));
        $this->assertEquals('$5,000.10', $numberFormatter->formatCurrency(5000.1, '$'));
      
        $this->translator->setCurrentLanguage('de');
        $this->assertEquals('5.000,1', $numberFormatter->formatNumber(5000.1, 1));
        $this->assertEquals('50,1 %', $numberFormatter->formatPercent(50.1, 1));
        $this->assertEquals('+50 %', $numberFormatter->formatPercentEvolution(50));
        $this->assertEquals('5.000,10 €', $numberFormatter->formatCurrency(5000.1, '€'));

        $this->translator->setCurrentLanguage('ar');
        $this->assertEquals('5٬000٫1٪؜', $numberFormatter->formatPercent(5000.1, 1));

        $this->translator->setCurrentLanguage('bn');
        $this->assertEquals('50,00,000', $numberFormatter->formatNumber(5000000));
    }
}