9
9
10
10
use Magento \Directory \Model \Currency ;
11
11
use Magento \Framework \Locale \CurrencyInterface ;
12
+ use Magento \Framework \Locale \ResolverInterface as LocalResolverInterface ;
13
+ use Magento \Framework \NumberFormatterFactory ;
12
14
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13
15
use PHPUnit \Framework \MockObject \MockObject ;
14
16
use PHPUnit \Framework \TestCase ;
@@ -27,82 +29,165 @@ class CurrencyTest extends TestCase
27
29
*/
28
30
protected $ localeCurrencyMock ;
29
31
32
+ /**
33
+ * @var LocalResolverInterface
34
+ */
35
+ private $ localeResolver ;
36
+
37
+ /**
38
+ * @var NumberFormatterFactory
39
+ */
40
+ private $ numberFormatterFactory ;
41
+
30
42
protected function setUp (): void
31
43
{
32
44
$ this ->localeCurrencyMock = $ this ->getMockForAbstractClass (CurrencyInterface::class);
45
+ $ currencyFilterFactory = $ this ->getMockBuilder (\Magento \Directory \Model \Currency \FilterFactory::class)
46
+ ->disableOriginalConstructor ()
47
+ ->getMock ();
48
+ $ this ->localeResolver = $ this ->getMockBuilder (LocalResolverInterface::class)
49
+ ->getMockForAbstractClass ();
50
+ $ this ->numberFormatterFactory = $ this ->getMockBuilder (NumberFormatterFactory::class)
51
+ ->disableOriginalConstructor ()
52
+ ->setMethods (['create ' ])
53
+ ->getMock ();
33
54
34
55
$ objectManager = new ObjectManager ($ this );
35
56
$ this ->currency = $ objectManager ->getObject (
36
57
Currency::class,
37
58
[
38
59
'localeCurrency ' => $ this ->localeCurrencyMock ,
60
+ 'currencyFilterFactory ' => $ currencyFilterFactory ,
61
+ 'localeResolver ' => $ this ->localeResolver ,
62
+ 'numberFormatterFactory ' => $ this ->numberFormatterFactory ,
39
63
'data ' => [
40
64
'currency_code ' => $ this ->currencyCode ,
41
65
]
42
66
]
43
67
);
44
68
}
45
69
46
- public function testGetCurrencySymbol ()
70
+ public function testGetCurrencySymbol (): void
47
71
{
48
72
$ currencySymbol = '$ ' ;
49
73
50
74
$ currencyMock = $ this ->getMockBuilder (\Magento \Framework \Currency::class)
51
75
->disableOriginalConstructor ()
52
76
->getMock ();
53
- $ currencyMock ->expects ($ this -> once ())
77
+ $ currencyMock ->expects (self :: once ())
54
78
->method ('getSymbol ' )
55
79
->willReturn ($ currencySymbol );
56
80
57
- $ this ->localeCurrencyMock ->expects ($ this -> once ())
81
+ $ this ->localeCurrencyMock ->expects (self :: once ())
58
82
->method ('getCurrency ' )
59
83
->with ($ this ->currencyCode )
60
84
->willReturn ($ currencyMock );
61
- $ this -> assertEquals ($ currencySymbol , $ this ->currency ->getCurrencySymbol ());
85
+ self :: assertEquals ($ currencySymbol , $ this ->currency ->getCurrencySymbol ());
62
86
}
63
87
64
88
/**
65
89
* @dataProvider getOutputFormatDataProvider
66
- * @param $withCurrency
67
- * @param $noCurrency
68
90
* @param $expected
91
+ * @param $locale
69
92
*/
70
- public function testGetOutputFormat ($ withCurrency , $ noCurrency , $ expected )
93
+ public function testGetOutputFormat ($ expected , $ locale ): void
71
94
{
72
- $ currencyMock = $ this ->getMockBuilder (\Magento \Framework \Currency::class)
73
- ->disableOriginalConstructor ()
74
- ->getMock ();
75
- $ currencyMock ->expects ($ this ->at (0 ))
76
- ->method ('toCurrency ' )
77
- ->willReturn ($ withCurrency );
78
- $ currencyMock ->expects ($ this ->at (1 ))
79
- ->method ('toCurrency ' )
80
- ->willReturn ($ noCurrency );
81
- $ this ->localeCurrencyMock ->expects ($ this ->atLeastOnce ())
82
- ->method ('getCurrency ' )
83
- ->with ($ this ->currencyCode )
84
- ->willReturn ($ currencyMock );
85
- $ this ->assertEquals ($ expected , $ this ->currency ->getOutputFormat ());
95
+ $ this ->localeResolver ->method ('getLocale ' )->willReturn ($ locale );
96
+ $ this ->numberFormatterFactory
97
+ ->method ('create ' )
98
+ ->with (['locale ' => $ locale , 'style ' => 2 ])
99
+ ->willReturn (new \NumberFormatter ($ locale , 2 ));
100
+ self ::assertEquals ($ expected , $ this ->currency ->getOutputFormat ());
86
101
}
87
102
88
103
/**
89
- * Return data sets for testGetCurrencySymbol ()
104
+ * Return data sets for testGetOutputFormat ()
90
105
*
91
106
* @return array
92
107
*/
93
- public function getOutputFormatDataProvider ()
108
+ public function getOutputFormatDataProvider (): array
94
109
{
95
110
return [
96
111
'no_unicode ' => [
97
- 'withCurrency ' => '$0.00 ' ,
98
- 'noCurrency ' => '0.00 ' ,
99
112
'expected ' => '$%s ' ,
113
+ 'locale ' => 'en_US '
100
114
],
101
115
'arabic_unicode ' => [
102
- 'withCurrency ' => json_decode ('"\u200E" ' ) . '$0.00 ' ,
103
- 'noCurrency ' => json_decode ('"\u200E" ' ) . '0.00 ' ,
104
116
'expected ' => json_decode ('"\u200E" ' ) . '$%s ' ,
117
+ 'locale ' => 'fa_IR '
105
118
]
106
119
];
107
120
}
121
+
122
+ /**
123
+ * @dataProvider getFormatTxtNumberFormatterDataProvider
124
+ * @param string $price
125
+ * @param array $options
126
+ * @param string $locale
127
+ * @param string $expected
128
+ */
129
+ public function testFormatTxtWithNumberFormatter (
130
+ string $ price ,
131
+ array $ options ,
132
+ string $ locale ,
133
+ string $ expected
134
+ ): void {
135
+ $ this ->localeResolver ->expects (self ::once ())->method ('getLocale ' )->willReturn ($ locale );
136
+ $ this ->numberFormatterFactory
137
+ ->expects (self ::once ())
138
+ ->method ('create ' )
139
+ ->with (['locale ' => $ locale , 'style ' => 2 ])
140
+ ->willReturn (new \NumberFormatter ($ locale , 2 ));
141
+
142
+ self ::assertEquals ($ expected , $ this ->currency ->formatTxt ($ price , $ options ));
143
+ }
144
+
145
+ /**
146
+ * Return data sets for testFormatTxtWithNumberFormatter()
147
+ *
148
+ * @return array
149
+ */
150
+ public function getFormatTxtNumberFormatterDataProvider (): array
151
+ {
152
+ return [
153
+ ['9999 ' , [], 'en_US ' , '$9,999.00 ' ],
154
+ ['9999 ' , ['display ' => \Magento \Framework \Currency::NO_SYMBOL , 'precision ' => 2 ], 'en_US ' , '9,999.00 ' ],
155
+ ['9999 ' , ['display ' => \Magento \Framework \Currency::NO_SYMBOL ], 'en_US ' , '9,999.00 ' ],
156
+ ['9999 ' , ['precision ' => 1 ], 'en_US ' , '$9,999.0 ' ]
157
+ ];
158
+ }
159
+
160
+ /**
161
+ * @dataProvider getFormatTxtZendCurrencyDataProvider
162
+ * @param string $price
163
+ * @param array $options
164
+ * @param string $expected
165
+ * @throws \Zend_Currency_Exception
166
+ */
167
+ public function testFormatTxtWithZendCurrency (string $ price , array $ options , string $ expected ): void
168
+ {
169
+ $ this ->localeCurrencyMock
170
+ ->expects (self ::once ())
171
+ ->method ('getCurrency ' )
172
+ ->with ($ this ->currencyCode )
173
+ ->willReturn (new \Zend_Currency ($ options , 'en_US ' ));
174
+
175
+ self ::assertEquals ($ expected , $ this ->currency ->formatTxt ($ price , $ options ));
176
+ }
177
+
178
+ /**
179
+ * Return data sets for testFormatTxtWithZendCurrency()
180
+ *
181
+ * @return array
182
+ */
183
+ public function getFormatTxtZendCurrencyDataProvider (): array
184
+ {
185
+ return [
186
+ ['9999 ' , ['display ' => \Magento \Framework \Currency::USE_SYMBOL , 'foo ' => 'bar ' ], '$9,999.00 ' ],
187
+ ['9999 ' , ['display ' => \Magento \Framework \Currency::USE_SHORTNAME , 'foo ' => 'bar ' ], 'USD9,999.00 ' ],
188
+ ['9999 ' , ['currency ' => 'USD ' ], '$9,999.00 ' ],
189
+ ['9999 ' , ['currency ' => 'CNY ' ], 'CN¥9,999.00 ' ],
190
+ ['9999 ' , ['locale ' => 'fr_FR ' ], '9 999,00 $ ' ]
191
+ ];
192
+ }
108
193
}
0 commit comments