7
7
8
8
namespace Magento \Backend \Test \Unit \Block \Widget \Grid \Column \Renderer ;
9
9
10
- use Magento \Backend \Block \Widget \Grid \Column ;
11
- use Magento \Backend \Block \Widget \Grid \Column \Renderer \Currency ;
12
10
use Magento \Directory \Model \Currency \DefaultLocator ;
13
- use Magento \Directory \Model \CurrencyFactory ;
14
11
use Magento \Framework \App \RequestInterface ;
15
- use Magento \Framework \Currency \Data \Currency as CurrencyData ;
12
+ use Magento \Directory \Model \CurrencyFactory ;
13
+ use Magento \Backend \Block \Widget \Grid \Column ;
16
14
use Magento \Framework \DataObject ;
17
- use Magento \Framework \Locale \CurrencyInterface ;
18
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
+ use Magento \Backend \Block \Widget \Grid \Column \Renderer \Currency ;
17
+ use Magento \Framework \Locale \Currency as LocaleCurrency ;
18
+ use Magento \Directory \Model \Currency as CurrencyData ;
19
19
use Magento \Store \Model \Store ;
20
20
use Magento \Store \Model \StoreManagerInterface ;
21
- use Magento \Store \Model \Website ;
22
21
use PHPUnit \Framework \MockObject \MockObject ;
23
22
use PHPUnit \Framework \TestCase ;
24
23
@@ -30,152 +29,128 @@ class CurrencyTest extends TestCase
30
29
/**
31
30
* @var Currency
32
31
*/
33
- protected $ _blockCurrency ;
32
+ private $ currencyRenderer ;
34
33
35
34
/**
36
- * @var MockObject
35
+ * @var ObjectManager
37
36
*/
38
- protected $ _localeMock ;
37
+ private $ objectManager ;
39
38
40
39
/**
41
- * @var MockObject
40
+ * @var Store| MockObject
42
41
*/
43
- protected $ _curLocatorMock ;
42
+ private $ storeManagerMock ;
44
43
45
44
/**
46
- * @var MockObject
45
+ * @var DefaultLocator| MockObject
47
46
*/
48
- protected $ _columnMock ;
47
+ private $ currencyLocatorMock ;
49
48
50
49
/**
51
- * @var MockObject
50
+ * @var CurrencyFactory| MockObject
52
51
*/
53
- protected $ _storeManagerMock ;
52
+ private $ currencyFactoryMock ;
54
53
55
54
/**
56
- * @var MockObject
55
+ * @var LocaleCurrency| MockObject
57
56
*/
58
- protected $ _requestMock ;
57
+ private $ localeCurrencyMock ;
59
58
60
59
/**
61
- * @var MockObject
60
+ * @var RequestInterface| MockObject
62
61
*/
63
- protected $ _currencyMock ;
62
+ private $ requestMock ;
64
63
65
64
/**
66
- * @var DataObject
65
+ * @var Column|MockObject
67
66
*/
68
- protected $ _row ;
67
+ private $ columnMock ;
69
68
70
69
protected function setUp (): void
71
70
{
72
- $ this ->_storeManagerMock = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
73
- $ this ->_localeMock = $ this ->getMockForAbstractClass (CurrencyInterface::class);
74
- $ this ->_requestMock = $ this ->getMockForAbstractClass (RequestInterface::class);
75
-
76
- $ this ->_curLocatorMock = $ this ->createMock (DefaultLocator::class);
77
- $ this ->_columnMock = $ this ->getMockBuilder (Column::class)
78
- ->addMethods (['getIndex ' , 'getCurrency ' , 'getRateField ' ])
71
+ $ this ->objectManager = new ObjectManager ($ this );
72
+ $ this ->storeManagerMock = $ this ->createMock (StoreManagerInterface::class);
73
+ $ this ->currencyLocatorMock = $ this ->createMock (DefaultLocator::class);
74
+ $ this ->currencyFactoryMock = $ this ->createMock (CurrencyFactory::class);
75
+ $ this ->requestMock = $ this ->createMock (RequestInterface::class);
76
+ $ defaultCurrencyCode = 'USD ' ;
77
+ $ currencyMock = $ this ->createMock (CurrencyData::class);
78
+ $ this ->currencyFactoryMock ->method ('create ' )
79
+ ->willReturn ($ currencyMock );
80
+ $ currencyMock ->method ('load ' )
81
+ ->with ($ defaultCurrencyCode )
82
+ ->willReturnSelf ();
83
+ $ this ->currencyLocatorMock ->method ('getDefaultCurrency ' )
84
+ ->with ($ this ->requestMock )
85
+ ->willReturn ($ defaultCurrencyCode );
86
+ $ this ->columnMock = $ this ->getMockBuilder (Column::class)
87
+ ->addMethods (['getIndex ' , 'getShowNumberSign ' , 'getDefault ' ])
79
88
->disableOriginalConstructor ()
80
89
->getMock ();
81
- $ this ->_columnMock ->expects ($ this ->any ())->method ('getIndex ' )->willReturn ('columnIndex ' );
82
-
83
- $ this ->_currencyMock = $ this ->createMock (\Magento \Directory \Model \Currency::class);
84
- $ this ->_currencyMock ->expects ($ this ->any ())->method ('load ' )->willReturnSelf ();
85
- $ currencyFactoryMock = $ this ->createPartialMock (CurrencyFactory::class, ['create ' ]);
86
- $ currencyFactoryMock ->expects ($ this ->any ())->method ('create ' )->willReturn ($ this ->_currencyMock );
87
-
88
- $ this ->_row = new DataObject (['columnIndex ' => '10 ' ]);
89
-
90
- $ helper = new ObjectManager ($ this );
91
- $ this ->_blockCurrency = $ helper ->getObject (
90
+ $ this ->columnMock ->method ('getIndex ' )->willReturn ('value ' );
91
+ $ this ->columnMock ->method ('getShowNumberSign ' )->willReturn (false );
92
+ $ this ->columnMock ->method ('getDefault ' )->willReturn ('' );
93
+ $ this ->localeCurrencyMock = $ this ->getMockBuilder (LocaleCurrency::class)
94
+ ->onlyMethods (['getCurrency ' ])
95
+ ->addMethods (['toCurrency ' ])
96
+ ->disableOriginalConstructor ()
97
+ ->getMock ();
98
+ $ this ->currencyRenderer = $ this ->objectManager ->getObject (
92
99
Currency::class,
93
100
[
94
- 'storeManager ' => $ this ->_storeManagerMock ,
95
- 'localeCurrency ' => $ this ->_localeMock ,
96
- 'currencyLocator ' => $ this ->_curLocatorMock ,
97
- 'request ' => $ this ->_requestMock ,
98
- 'currencyFactory ' => $ currencyFactoryMock
101
+ 'storeManager ' => $ this ->storeManagerMock ,
102
+ 'localeCurrency ' => $ this ->localeCurrencyMock ,
103
+ 'currencyLocator ' => $ this ->currencyLocatorMock ,
104
+ 'request ' => $ this ->requestMock ,
105
+ 'currencyFactory ' => $ this -> currencyFactoryMock
99
106
]
100
107
);
101
-
102
- $ this ->_blockCurrency ->setColumn ($ this ->_columnMock );
103
- }
104
-
105
- protected function tearDown (): void
106
- {
107
- unset($ this ->_localeMock );
108
- unset($ this ->_curLocatorMock );
109
- unset($ this ->_columnMock );
110
- unset($ this ->_row );
111
- unset($ this ->_storeManagerMock );
112
- unset($ this ->_requestMock );
113
- unset($ this ->_blockCurrency );
114
108
}
115
109
116
- /**
117
- * @covers \Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency::render
118
- */
119
110
public function testRenderWithDefaultCurrency ()
120
111
{
121
- $ this ->_currencyMock ->expects ($ this ->once ())
122
- ->method ('getRate ' )
123
- ->with ('defaultCurrency ' )
124
- ->willReturn (1.5 );
125
- $ this ->_curLocatorMock ->expects ($ this ->any ())
126
- ->method ('getDefaultCurrency ' )
127
- ->with ($ this ->_requestMock )
128
- ->willReturn ('defaultCurrency ' );
129
- $ currLocaleMock = $ this ->createMock (CurrencyData::class);
130
- $ currLocaleMock ->expects ($ this ->once ())
131
- ->method ('toCurrency ' )
132
- ->with (15.0000 )
133
- ->willReturn ('15USD ' );
134
- $ this ->_localeMock ->expects ($ this ->once ())
135
- ->method ('getCurrency ' )
136
- ->with ('defaultCurrency ' )
137
- ->willReturn ($ currLocaleMock );
138
- $ this ->_columnMock ->method ('getCurrency ' )->willReturn ('USD ' );
139
- $ this ->_columnMock ->method ('getRateField ' )->willReturn ('test_rate_field ' );
140
-
141
- $ this ->assertEquals ('15USD ' , $ this ->_blockCurrency ->render ($ this ->_row ));
112
+ $ defaultCurrencyCode = 'USD ' ;
113
+ $ amount = 123.45 ;
114
+ $ formattedAmount = '$123.45 ' ;
115
+ $ row = new DataObject (['value ' => $ amount ]);
116
+ $ this ->currencyRenderer ->setColumn ($ this ->columnMock );
117
+ $ this ->localeCurrencyMock ->method ('getCurrency ' )
118
+ ->with ($ defaultCurrencyCode )
119
+ ->willReturn ($ this ->localeCurrencyMock );
120
+ $ this ->localeCurrencyMock ->method ('toCurrency ' )
121
+ ->with (sprintf ("%f " , $ amount ))
122
+ ->willReturn ($ formattedAmount );
123
+ $ result = $ this ->currencyRenderer ->render ($ row );
124
+ $ this ->assertEquals ($ formattedAmount , $ result );
142
125
}
143
126
144
- /**
145
- * @covers \Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency::render
146
- */
147
127
public function testRenderWithNonDefaultCurrency ()
148
128
{
149
- $ this ->_currencyMock ->expects ($ this ->once ())
150
- ->method ('getRate ' )
151
- ->with ('nonDefaultCurrency ' )
152
- ->willReturn (2.0 );
153
- $ this ->_curLocatorMock ->expects ($ this ->never ())
154
- ->method ('getDefaultCurrency ' );
155
- $ currLocaleMock = $ this ->createMock (CurrencyData::class);
156
- $ currLocaleMock ->expects ($ this ->once ())
157
- ->method ('toCurrency ' )
158
- ->with (20.0000 )
159
- ->willReturn ('20EUR ' );
160
- $ this ->_localeMock ->expects ($ this ->once ())
161
- ->method ('getCurrency ' )
162
- ->with ('nonDefaultCurrency ' )
163
- ->willReturn ($ currLocaleMock );
164
- $ this ->_columnMock ->method ('getCurrency ' )->willReturn ('EUR ' );
165
- $ this ->_columnMock ->method ('getRateField ' )->willReturn ('test_rate_field ' );
166
- $ this ->_row ->setData ('store_id ' , 1 );
167
- $ storeMock = $ this ->createMock (Store::class);
168
- $ websiteMock = $ this ->createMock (Website::class);
169
- $ websiteMock ->expects ($ this ->exactly (2 ))
170
- ->method ('getBaseCurrencyCode ' )
171
- ->willReturn ('nonDefaultCurrency ' );
172
- $ storeMock ->expects ($ this ->exactly (2 ))
173
- ->method ('getWebsite ' )
174
- ->willReturn ($ websiteMock );
175
- $ this ->_storeManagerMock ->expects ($ this ->exactly (2 ))
176
- ->method ('getStore ' )
177
- ->with (1 )
129
+ $ nonDefaultCurrencyCode = 'EUR ' ;
130
+ $ amount = 123.45 ;
131
+ $ formattedAmount = '€123.45 ' ;
132
+ $ storeId = 2 ;
133
+ $ row = new DataObject ([
134
+ 'value ' => $ amount ,
135
+ 'store_id ' => $ storeId
136
+ ]);
137
+ $ this ->currencyRenderer ->setColumn ($ this ->columnMock );
138
+ $ storeMock = $ this ->getMockBuilder (Store::class)
139
+ ->onlyMethods (['getCurrentCurrencyCode ' ])
140
+ ->disableOriginalConstructor ()
141
+ ->getMock ();
142
+ $ this ->storeManagerMock ->method ('getStore ' )
143
+ ->with ($ storeId )
178
144
->willReturn ($ storeMock );
179
- $ this ->assertEquals ('20EUR ' , $ this ->_blockCurrency ->render ($ this ->_row ));
145
+ $ storeMock ->method ('getCurrentCurrencyCode ' )
146
+ ->willReturn ($ nonDefaultCurrencyCode );
147
+ $ this ->localeCurrencyMock ->method ('getCurrency ' )
148
+ ->with ($ nonDefaultCurrencyCode )
149
+ ->willReturn ($ this ->localeCurrencyMock );
150
+ $ this ->localeCurrencyMock ->method ('toCurrency ' )
151
+ ->with (sprintf ("%f " , $ amount ))
152
+ ->willReturn ($ formattedAmount );
153
+ $ result = $ this ->currencyRenderer ->render ($ row );
154
+ $ this ->assertEquals ($ formattedAmount , $ result );
180
155
}
181
156
}
0 commit comments