7
7
8
8
namespace Magento \Directory \Test \Unit \Model ;
9
9
10
- use Magento \Directory \Model \Currency ;
10
+ use Magento \Directory \Model \Currency as CurrencyModel ;
11
+ use Magento \Framework \Currency ;
11
12
use Magento \Framework \Locale \CurrencyInterface ;
12
13
use Magento \Framework \Locale \ResolverInterface as LocalResolverInterface ;
13
14
use Magento \Framework \NumberFormatterFactory ;
19
20
class CurrencyTest extends TestCase
20
21
{
21
22
/**
22
- * @var Currency
23
+ * @var CurrencyModel
23
24
*/
24
25
protected $ currency ;
25
26
27
+ /**
28
+ * @var string
29
+ */
26
30
protected $ currencyCode = 'USD ' ;
27
31
28
32
/**
@@ -63,7 +67,7 @@ protected function setUp(): void
63
67
64
68
$ objectManager = new ObjectManager ($ this );
65
69
$ this ->currency = $ objectManager ->getObject (
66
- Currency ::class,
70
+ CurrencyModel ::class,
67
71
[
68
72
'localeCurrency ' => $ this ->localeCurrencyMock ,
69
73
'currencyFilterFactory ' => $ currencyFilterFactory ,
@@ -81,7 +85,7 @@ public function testGetCurrencySymbol(): void
81
85
{
82
86
$ currencySymbol = '$ ' ;
83
87
84
- $ currencyMock = $ this ->getMockBuilder (\ Magento \ Framework \ Currency::class)
88
+ $ currencyMock = $ this ->getMockBuilder (Currency::class)
85
89
->disableOriginalConstructor ()
86
90
->getMock ();
87
91
$ currencyMock ->expects (self ::once ())
@@ -97,22 +101,31 @@ public function testGetCurrencySymbol(): void
97
101
98
102
/**
99
103
* @dataProvider getOutputFormatDataProvider
100
- * @param $expected
101
- * @param $locale
104
+ * @param string $locale
105
+ * @param string $currency
106
+ * @param string $expected
102
107
*/
103
- public function testGetOutputFormat ($ expected , $ locale ): void
108
+ public function testGetOutputFormat (string $ locale , string $ currency , string $ expected ): void
104
109
{
105
- $ this ->localeResolver ->method ('getLocale ' )->willReturn ($ locale );
110
+ $ this ->localeResolver ->expects (self ::atLeastOnce ())
111
+ ->method ('getLocale ' )
112
+ ->willReturn ($ locale );
106
113
$ this ->numberFormatterFactory
114
+ ->expects (self ::exactly (2 ))
107
115
->method ('create ' )
108
- ->with (['locale ' => $ locale , 'style ' => 2 ])
109
- ->willReturn (new \Magento \Framework \NumberFormatter ($ locale , 2 ));
116
+ ->willReturnCallback (
117
+ function (array $ args ) {
118
+ return new \Magento \Framework \NumberFormatter ($ args ['locale ' ], $ args ['style ' ]);
119
+ }
120
+ );
110
121
$ this ->serializer ->method ('serialize ' )->willReturnMap (
111
122
[
112
123
[[], '[] ' ],
113
124
[['display ' => 1 ], '{"display":1} ' ]
114
125
]
115
126
);
127
+
128
+ $ this ->currency ->load ($ currency );
116
129
self ::assertEquals ($ expected , $ this ->currency ->getOutputFormat ());
117
130
}
118
131
@@ -124,42 +137,58 @@ public function testGetOutputFormat($expected, $locale): void
124
137
public function getOutputFormatDataProvider (): array
125
138
{
126
139
return [
127
- 'no_unicode ' => [
128
- 'expected ' => '$%s ' ,
129
- 'locale ' => 'en_US '
130
- ],
131
- 'arabic_unicode ' => [
132
- 'expected ' => json_decode ('"\u200E" ' ) . '$%s ' ,
133
- 'locale ' => 'fa_IR '
134
- ]
140
+ 'en_US:USD ' => ['en_US ' , 'USD ' , '$%s ' ],
141
+ 'en_US:PLN ' => ['en_US ' , 'PLN ' , "PLN \u{00A0}%s " ],
142
+ 'en_US:PKR ' => ['en_US ' , 'PKR ' , "PKR \u{00A0}%s " ],
143
+ 'af_ZA:VND ' => ['af_ZA ' , 'VND ' , "\u{20AB}%s " ],
144
+ 'ar_DZ:EGP ' => ['ar_DZ ' , 'EGP ' , "\u{062C}. \u{0645}. \u{200F}\u{00A0}%s " ],
145
+ 'ar_SA:USD ' => ['ar_SA ' , 'USD ' , "%s \u{00A0}US$ " ],
146
+ 'ar_SA:LBP ' => ['ar_SA ' , 'LBP ' , "%s \u{00A0}\u{0644}. \u{0644}. \u{200F}" ],
147
+ 'fa_IR:USD ' => ['fa_IR ' , 'USD ' , "\u{200E}$%s " ],
148
+ 'ar_KW:USD ' => ['ar_KW ' , 'USD ' , "%s \u{00A0}US$ " ],
149
+ 'bn_BD:IQD ' => ['bn_BD ' , 'IQD ' , "%s \u{00A0}IQD " ],
150
+ 'ca_ES:VND ' => ['ca_ES ' , 'VND ' , "%s \u{00A0}\u{20AB}" ],
151
+ 'de_DE:USD ' => ['de_DE ' , 'USD ' , "%s \u{00A0}$ " ],
152
+ 'de_DE:AED ' => ['de_DE ' , 'AED ' , "%s \u{00A0}AED " ],
153
+ 'es_VE:VEF ' => ['es_VE ' , 'VEF ' , "Bs. \u{00A0}%s " ],
154
+ 'pl_PL:USD ' => ['pl_PL ' , 'USD ' , "%s \u{00A0}USD " ],
155
+ 'pl_PL:PLN ' => ['pl_PL ' , 'PLN ' , "%s \u{00A0}z \u{0142}" ],
135
156
];
136
157
}
137
158
138
159
/**
139
160
* @dataProvider getFormatTxtNumberFormatterDataProvider
161
+ * @param string $locale
162
+ * @param string $currency
140
163
* @param string $price
141
164
* @param array $options
142
- * @param string $locale
143
165
* @param string $expected
144
166
*/
145
167
public function testFormatTxtWithNumberFormatter (
168
+ string $ locale ,
169
+ string $ currency ,
146
170
string $ price ,
147
171
array $ options ,
148
- string $ locale ,
149
172
string $ expected
150
173
): void {
151
- $ this ->localeResolver ->expects (self ::exactly (2 ))->method ('getLocale ' )->willReturn ($ locale );
174
+ $ this ->localeResolver ->expects (self ::once ())
175
+ ->method ('getLocale ' )
176
+ ->willReturn ($ locale );
152
177
$ this ->numberFormatterFactory
153
178
->expects (self ::once ())
154
179
->method ('create ' )
155
- ->with (['locale ' => $ locale , 'style ' => 2 ])
156
- ->willReturn (new \Magento \Framework \NumberFormatter ($ locale , 2 ));
180
+ ->willReturnCallback (
181
+ function (array $ args ) {
182
+ return new \Magento \Framework \NumberFormatter ($ args ['locale ' ], $ args ['style ' ]);
183
+ }
184
+ );
157
185
$ this ->serializer ->method ('serialize ' )->willReturnMap (
158
186
[
159
187
[[], '[] ' ]
160
188
]
161
189
);
162
190
191
+ $ this ->currency ->load ($ currency );
163
192
self ::assertEquals ($ expected , $ this ->currency ->formatTxt ($ price , $ options ));
164
193
}
165
194
@@ -171,16 +200,39 @@ public function testFormatTxtWithNumberFormatter(
171
200
public function getFormatTxtNumberFormatterDataProvider (): array
172
201
{
173
202
return [
174
- ['9999 ' , [], 'en_US ' , '$9,999.00 ' ],
175
- ['9999 ' , ['display ' => \Magento \Framework \Currency::NO_SYMBOL , 'precision ' => 2 ], 'en_US ' , '9,999.00 ' ],
176
- ['9999 ' , ['display ' => \Magento \Framework \Currency::NO_SYMBOL ], 'en_US ' , '9,999.00 ' ],
177
- [' 9999 ' , ['display ' => \Magento \Framework \Currency::NO_SYMBOL ], 'en_US ' , '9,999.00 ' ],
178
- ['9999 ' , ['precision ' => 1 ], 'en_US ' , '$9,999.0 ' ],
179
- ['9999 ' , ['precision ' => 2 , 'symbol ' => '# ' ], 'en_US ' , '#9,999.00 ' ],
203
+ ['en_US ' , 'USD ' , '9999 ' , [], '$9,999.00 ' ],
204
+ ['en_US ' , 'EUR ' , '9999 ' , [], '€9,999.00 ' ],
205
+ ['en_US ' , 'LBP ' , '9999 ' , [], "LBP \u{00A0}9,999 " ],
206
+ ['ar_AE ' , 'USD ' , '9 ' , [], "\u{0669}\u{066B}\u{0660}\u{0660}\u{00A0}US$ " ],
207
+ ['ar_AE ' , 'AED ' , '9 ' , [], "\u{0669}\u{066B}\u{0660}\u{0660}\u{00A0}\u{062F}. \u{0625}. \u{200F}" ],
208
+ ['de_DE ' , 'USD ' , '9999 ' , [], "9.999,00 \u{00A0}$ " ],
209
+ ['de_DE ' , 'EUR ' , '9999 ' , [], "9.999,00 \u{00A0}€ " ],
210
+ ['en_US ' , 'USD ' , '9999 ' , ['display ' => Currency::NO_SYMBOL , 'precision ' => 2 ], '9,999.00 ' ],
211
+ ['en_US ' , 'USD ' , '9999 ' , ['display ' => Currency::NO_SYMBOL ], '9,999.00 ' ],
212
+ ['en_US ' , 'PLN ' , '9999 ' , ['display ' => Currency::NO_SYMBOL ], '9,999.00 ' ],
213
+ ['en_US ' , 'LBP ' , '9999 ' , ['display ' => Currency::NO_SYMBOL ], '9,999 ' ],
214
+ [
215
+ 'ar_AE ' ,
216
+ 'USD ' ,
217
+ '9999 ' ,
218
+ ['display ' => Currency::NO_SYMBOL ],
219
+ "\u{0669}\u{066C}\u{0669}\u{0669}\u{0669}\u{066B}\u{0660}\u{0660}"
220
+ ],
221
+ [
222
+ 'ar_AE ' ,
223
+ 'AED ' ,
224
+ '9999 ' ,
225
+ ['display ' => Currency::NO_SYMBOL ],
226
+ "\u{0669}\u{066C}\u{0669}\u{0669}\u{0669}\u{066B}\u{0660}\u{0660}"
227
+ ],
228
+ ['en_US ' , 'USD ' , ' 9999 ' , ['display ' => Currency::NO_SYMBOL ], '9,999.00 ' ],
229
+ ['en_US ' , 'USD ' , '9999 ' , ['precision ' => 1 ], '$9,999.0 ' ],
230
+ ['en_US ' , 'USD ' , '9999 ' , ['precision ' => 2 , 'symbol ' => '# ' ], '#9,999.00 ' ],
180
231
[
181
- '9999.99 ' ,
182
- ['precision ' => 2 , 'symbol ' => '# ' , 'display ' => \Magento \Framework \Currency::NO_SYMBOL ],
183
232
'en_US ' ,
233
+ 'USD ' ,
234
+ '9999.99 ' ,
235
+ ['precision ' => 2 , 'symbol ' => '# ' , 'display ' => Currency::NO_SYMBOL ],
184
236
'9,999.99 '
185
237
],
186
238
];
@@ -217,11 +269,11 @@ public function testFormatTxtWithZendCurrency(string $price, array $options, str
217
269
public function getFormatTxtZendCurrencyDataProvider (): array
218
270
{
219
271
return [
220
- ['9999 ' , ['display ' => \ Magento \ Framework \ Currency::USE_SYMBOL , 'foo ' => 'bar ' ], '$9,999.00 ' ],
221
- ['9999 ' , ['display ' => \ Magento \ Framework \ Currency::USE_SHORTNAME , 'foo ' => 'bar ' ], 'USD9,999.00 ' ],
272
+ ['9999 ' , ['display ' => Currency::USE_SYMBOL , 'foo ' => 'bar ' ], '$9,999.00 ' ],
273
+ ['9999 ' , ['display ' => Currency::USE_SHORTNAME , 'foo ' => 'bar ' ], 'USD9,999.00 ' ],
222
274
['9999 ' , ['currency ' => 'USD ' ], '$9,999.00 ' ],
223
275
['9999 ' , ['currency ' => 'CNY ' ], 'CN¥9,999.00 ' ],
224
- ['9999 ' , ['locale ' => 'fr_FR ' ], ' 9 999,00 $ ' ]
276
+ ['9999 ' , ['locale ' => 'fr_FR ' ], " 9 \u{00A0} 999,00 \u{00A0} $ " ]
225
277
];
226
278
}
227
279
}
0 commit comments