@@ -98,32 +98,29 @@ protected function setUp()
98
98
);
99
99
}
100
100
101
- /**
102
- * Run test execute method
103
- *
104
- * @param array $result
105
- * @param array $resultExpectation
106
- *
107
- * @dataProvider executeDataProvider
108
- */
109
- public function testExecute (array $ result , array $ resultExpectation )
101
+ public function testExecuteSuccess ()
110
102
{
111
103
$ quoteId = 99 ;
104
+ $ tokenFields = ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ];
105
+ $ secureToken = 'token_hash ' ;
106
+ $ resultExpectation = [
107
+ 'transparent ' => [
108
+ 'fields ' => ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ]
109
+ ],
110
+ 'success ' => true ,
111
+ 'error ' => false
112
+ ];
112
113
113
114
$ quoteMock = $ this ->getMockBuilder ('Magento\Quote\Model\Quote ' )
114
115
->disableOriginalConstructor ()
115
116
->getMock ();
116
117
$ tokenMock = $ this ->getMockBuilder ('Magento\Framework\DataObject ' )
117
- ->setMethods (['getData ' , 'getSecuretoken ' ])
118
118
->disableOriginalConstructor ()
119
119
->getMock ();
120
120
$ jsonMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Json ' )
121
121
->disableOriginalConstructor ()
122
122
->getMock ();
123
123
124
- $ this ->transparentMock ->expects ($ this ->once ())
125
- ->method ('getCode ' )
126
- ->willReturn ('transparent ' );
127
124
$ this ->sessionManagerMock ->expects ($ this ->atLeastOnce ())
128
125
->method ('getQuote ' )
129
126
->willReturn ($ quoteMock );
@@ -140,12 +137,14 @@ public function testExecute(array $result, array $resultExpectation)
140
137
$ this ->transparentMock ->expects ($ this ->once ())
141
138
->method ('getCode ' )
142
139
->willReturn ('transparent ' );
143
- $ tokenMock ->expects ($ this ->once ())
140
+ $ tokenMock ->expects ($ this ->atLeastOnce ())
144
141
->method ('getData ' )
145
- ->willReturn ($ result ['transparent ' ]['fields ' ]);
146
- $ tokenMock ->expects ($ this ->once ())
147
- ->method ('getSecuretoken ' )
148
- ->willReturn ($ result ['success ' ]);
142
+ ->willReturnMap (
143
+ [
144
+ ['' , null , $ tokenFields ],
145
+ ['securetoken ' , null , $ secureToken ]
146
+ ]
147
+ );
149
148
$ this ->resultJsonFactoryMock ->expects ($ this ->once ())
150
149
->method ('create ' )
151
150
->willReturn ($ jsonMock );
@@ -157,42 +156,71 @@ public function testExecute(array $result, array $resultExpectation)
157
156
$ this ->assertEquals ($ jsonMock , $ this ->controller ->execute ());
158
157
}
159
158
160
- /**
161
- * @return array
162
- */
163
- public function executeDataProvider ()
159
+ public function testExecuteTokenRequestException ()
164
160
{
165
- return [
166
- [
167
- 'result ' => [
168
- 'transparent ' => [
169
- 'fields ' => ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ]
170
- ],
171
- 'success ' => 1
172
- ],
173
- 'result_expectation ' => [
174
- 'transparent ' => [
175
- 'fields ' => ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ]
176
- ],
177
- 'success ' => true
178
- ]
179
- ],
180
- [
181
- 'result ' => [
182
- 'transparent ' => [
183
- 'fields ' => ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ]
184
- ],
185
- 'success ' => null ,
186
- ],
187
- 'result_expectation ' => [
188
- 'transparent ' => [
189
- 'fields ' => ['fields-1 ' , 'fields-2 ' , 'fields-3 ' ]
190
- ],
191
- 'success ' => false ,
192
- 'error ' => true ,
193
- 'error_messages ' => __ ('Secure Token Error. Try again. ' )
194
- ]
195
- ]
161
+ $ quoteId = 99 ;
162
+ $ resultExpectation = [
163
+ 'success ' => false ,
164
+ 'error ' => true ,
165
+ 'error_messages ' => __ ('Your payment has been declined. Please try again. ' )
196
166
];
167
+
168
+ $ quoteMock = $ this ->getMockBuilder ('Magento\Quote\Model\Quote ' )
169
+ ->disableOriginalConstructor ()
170
+ ->getMock ();
171
+ $ jsonMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Json ' )
172
+ ->disableOriginalConstructor ()
173
+ ->getMock ();
174
+
175
+ $ this ->sessionManagerMock ->expects ($ this ->atLeastOnce ())
176
+ ->method ('getQuote ' )
177
+ ->willReturn ($ quoteMock );
178
+ $ quoteMock ->expects ($ this ->once ())
179
+ ->method ('getId ' )
180
+ ->willReturn ($ quoteId );
181
+ $ this ->sessionTransparentMock ->expects ($ this ->once ())
182
+ ->method ('setQuoteId ' )
183
+ ->with ($ quoteId );
184
+ $ this ->secureTokenServiceMock ->expects ($ this ->once ())
185
+ ->method ('requestToken ' )
186
+ ->with ($ quoteMock )
187
+ ->willThrowException (new \Exception ());
188
+ $ this ->resultJsonFactoryMock ->expects ($ this ->once ())
189
+ ->method ('create ' )
190
+ ->willReturn ($ jsonMock );
191
+ $ jsonMock ->expects ($ this ->once ())
192
+ ->method ('setData ' )
193
+ ->with ($ resultExpectation )
194
+ ->willReturnSelf ();
195
+
196
+ $ this ->assertEquals ($ jsonMock , $ this ->controller ->execute ());
197
197
}
198
+
199
+ public function testExecuteEmptyQuoteError ()
200
+ {
201
+ $ resultExpectation = [
202
+ 'success ' => false ,
203
+ 'error ' => true ,
204
+ 'error_messages ' => __ ('Your payment has been declined. Please try again. ' )
205
+ ];
206
+
207
+ $ quoteMock = null ;
208
+ $ jsonMock = $ this ->getMockBuilder ('Magento\Framework\Controller\Result\Json ' )
209
+ ->disableOriginalConstructor ()
210
+ ->getMock ();
211
+
212
+ $ this ->sessionManagerMock ->expects ($ this ->atLeastOnce ())
213
+ ->method ('getQuote ' )
214
+ ->willReturn ($ quoteMock );
215
+ $ this ->resultJsonFactoryMock ->expects ($ this ->once ())
216
+ ->method ('create ' )
217
+ ->willReturn ($ jsonMock );
218
+ $ jsonMock ->expects ($ this ->once ())
219
+ ->method ('setData ' )
220
+ ->with ($ resultExpectation )
221
+ ->willReturnSelf ();
222
+
223
+ $ this ->assertEquals ($ jsonMock , $ this ->controller ->execute ());
224
+ }
225
+
198
226
}
0 commit comments