5
5
*/
6
6
namespace Magento \Braintree \Test \Unit \Gateway \Response \PayPal ;
7
7
8
+ use Braintree \Result \Successful ;
8
9
use Braintree \Transaction ;
9
10
use Braintree \Transaction \PayPalDetails ;
10
11
use Magento \Braintree \Gateway \Helper \SubjectReader ;
15
16
use Magento \Sales \Api \Data \OrderPaymentExtensionInterface ;
16
17
use Magento \Sales \Api \Data \OrderPaymentExtensionInterfaceFactory ;
17
18
use Magento \Sales \Model \Order \Payment ;
19
+ use Magento \Vault \Api \Data \PaymentTokenFactoryInterface ;
18
20
use Magento \Vault \Api \Data \PaymentTokenInterface ;
19
- use Magento \Vault \Api \Data \PaymentTokenInterfaceFactory ;
20
- use Magento \Vault \Model \AccountPaymentTokenFactory ;
21
21
use Magento \Vault \Model \PaymentToken ;
22
+ use PHPUnit \Framework \TestCase ;
22
23
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
23
24
24
25
/**
25
26
* Class VaultDetailsHandlerTest
26
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
28
*/
28
- class VaultDetailsHandlerTest extends \ PHPUnit \ Framework \ TestCase
29
+ class VaultDetailsHandlerTest extends TestCase
29
30
{
30
31
private static $ transactionId = '1n2suy ' ;
31
32
32
- /**
33
- * @var SubjectReader|MockObject
34
- */
35
- private $ subjectReader ;
33
+ private static $ token = 'rc39al ' ;
34
+
35
+ private static $ payerEmail = 'john.doe@example.com ' ;
36
36
37
37
/**
38
38
* @var PaymentDataObjectInterface|MockObject
@@ -45,7 +45,7 @@ class VaultDetailsHandlerTest extends \PHPUnit\Framework\TestCase
45
45
private $ paymentInfo ;
46
46
47
47
/**
48
- * @var AccountPaymentTokenFactory |MockObject
48
+ * @var PaymentTokenFactoryInterface |MockObject
49
49
*/
50
50
private $ paymentTokenFactory ;
51
51
@@ -92,7 +92,7 @@ protected function setUp()
92
92
93
93
$ this ->paymentToken = $ objectManager ->getObject (PaymentToken::class);
94
94
95
- $ this ->paymentTokenFactory = $ this ->getMockBuilder (AccountPaymentTokenFactory ::class)
95
+ $ this ->paymentTokenFactory = $ this ->getMockBuilder (PaymentTokenFactoryInterface ::class)
96
96
->setMethods (['create ' ])
97
97
->disableOriginalConstructor ()
98
98
->getMock ();
@@ -109,14 +109,6 @@ protected function setUp()
109
109
$ this ->subject = [
110
110
'payment ' => $ this ->paymentDataObject ,
111
111
];
112
- $ this ->subjectReader = $ this ->getMockBuilder (SubjectReader::class)
113
- ->disableOriginalConstructor ()
114
- ->setMethods (['readPayment ' , 'readTransaction ' ])
115
- ->getMock ();
116
- $ this ->subjectReader ->expects (static ::once ())
117
- ->method ('readPayment ' )
118
- ->with ($ this ->subject )
119
- ->willReturn ($ this ->paymentDataObject );
120
112
121
113
$ this ->dateTimeFactory = $ this ->getMockBuilder (DateTimeFactory::class)
122
114
->disableOriginalConstructor ()
@@ -126,123 +118,102 @@ protected function setUp()
126
118
$ this ->handler = new VaultDetailsHandler (
127
119
$ this ->paymentTokenFactory ,
128
120
$ this ->paymentExtensionFactory ,
129
- $ this -> subjectReader ,
121
+ new SubjectReader () ,
130
122
$ this ->dateTimeFactory
131
123
);
132
124
}
133
125
134
- /**
135
- * @covers \Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler::handle
136
- */
137
126
public function testHandle ()
138
127
{
139
- /** @var Transaction $transaction */
140
128
$ transaction = $ this ->getTransaction ();
141
129
$ response = [
142
130
'object ' => $ transaction
143
131
];
144
132
145
- $ this ->paymentExtension ->expects (static ::once ())
146
- ->method ('setVaultPaymentToken ' )
133
+ $ this ->paymentExtension ->method ('setVaultPaymentToken ' )
147
134
->with ($ this ->paymentToken );
148
- $ this ->paymentExtension ->expects (static ::once ())
149
- ->method ('getVaultPaymentToken ' )
135
+ $ this ->paymentExtension ->method ('getVaultPaymentToken ' )
150
136
->willReturn ($ this ->paymentToken );
151
-
152
- $ this ->subjectReader ->expects (static ::once ())
153
- ->method ('readTransaction ' )
154
- ->with ($ response )
155
- ->willReturn ($ transaction );
156
137
157
- $ this ->paymentDataObject ->expects (static ::once ())
158
- ->method ('getPayment ' )
138
+ $ this ->paymentDataObject ->method ('getPayment ' )
159
139
->willReturn ($ this ->paymentInfo );
160
140
161
- $ this ->paymentTokenFactory ->expects ( static :: once () )
162
- ->method ( ' create ' )
141
+ $ this ->paymentTokenFactory ->method ( ' create ' )
142
+ ->with (PaymentTokenFactoryInterface:: TOKEN_TYPE_ACCOUNT )
163
143
->willReturn ($ this ->paymentToken );
164
144
165
- $ this ->paymentExtensionFactory ->expects (static ::once ())
166
- ->method ('create ' )
145
+ $ this ->paymentExtensionFactory ->method ('create ' )
167
146
->willReturn ($ this ->paymentExtension );
168
147
169
148
$ dateTime = new \DateTime ('2016-07-05 00:00:00 ' , new \DateTimeZone ('UTC ' ));
170
149
$ expirationDate = '2017-07-05 00:00:00 ' ;
171
- $ this ->dateTimeFactory ->expects (static ::once ())
172
- ->method ('create ' )
150
+ $ this ->dateTimeFactory ->method ('create ' )
173
151
->willReturn ($ dateTime );
174
152
175
153
$ this ->handler ->handle ($ this ->subject , $ response );
176
154
177
155
$ extensionAttributes = $ this ->paymentInfo ->getExtensionAttributes ();
178
- /** @var PaymentTokenInterface $paymentToken */
179
156
$ paymentToken = $ extensionAttributes ->getVaultPaymentToken ();
180
- static ::assertNotNull ($ paymentToken );
157
+ self ::assertNotNull ($ paymentToken );
181
158
182
159
$ tokenDetails = json_decode ($ paymentToken ->getTokenDetails (), true );
183
160
184
- static ::assertSame ($ this ->paymentToken , $ paymentToken );
185
- static ::assertEquals ($ transaction -> paypalDetails -> token , $ paymentToken ->getGatewayToken ());
186
- static ::assertEquals ($ transaction -> paypalDetails -> payerEmail , $ tokenDetails ['payerEmail ' ]);
187
- static ::assertEquals ($ expirationDate , $ paymentToken ->getExpiresAt ());
161
+ self ::assertSame ($ this ->paymentToken , $ paymentToken );
162
+ self ::assertEquals (self :: $ token , $ paymentToken ->getGatewayToken ());
163
+ self ::assertEquals (self :: $ payerEmail , $ tokenDetails ['payerEmail ' ]);
164
+ self ::assertEquals ($ expirationDate , $ paymentToken ->getExpiresAt ());
188
165
}
189
166
190
- /**
191
- * @covers \Magento\Braintree\Gateway\Response\PayPal\VaultDetailsHandler::handle
192
- */
193
167
public function testHandleWithoutToken ()
194
168
{
195
169
$ transaction = $ this ->getTransaction ();
196
- $ transaction ->paypalDetails ->token = null ;
170
+ $ transaction ->transaction -> paypalDetails ->token = null ;
197
171
198
172
$ response = [
199
173
'object ' => $ transaction
200
174
];
201
175
202
- $ this ->subjectReader ->expects (static ::once ())
203
- ->method ('readTransaction ' )
204
- ->with ($ response )
205
- ->willReturn ($ transaction );
206
-
207
- $ this ->paymentDataObject ->expects (static ::once ())
208
- ->method ('getPayment ' )
176
+ $ this ->paymentDataObject ->method ('getPayment ' )
209
177
->willReturn ($ this ->paymentInfo );
210
178
211
- $ this ->paymentTokenFactory ->expects (static ::never ())
179
+ $ this ->paymentTokenFactory ->expects (self ::never ())
212
180
->method ('create ' );
213
181
214
- $ this ->dateTimeFactory ->expects (static ::never ())
182
+ $ this ->dateTimeFactory ->expects (self ::never ())
215
183
->method ('create ' );
216
184
217
185
$ this ->handler ->handle ($ this ->subject , $ response );
218
- static ::assertNull ($ this ->paymentInfo ->getExtensionAttributes ());
186
+ self ::assertNull ($ this ->paymentInfo ->getExtensionAttributes ());
219
187
}
220
188
221
189
/**
222
- * Create Braintree transaction
223
- * @return Transaction
190
+ * Creates Braintree transaction.
191
+ *
192
+ * @return Successful
224
193
*/
225
- private function getTransaction ()
194
+ private function getTransaction (): Successful
226
195
{
227
196
$ attributes = [
228
197
'id ' => self ::$ transactionId ,
229
198
'paypalDetails ' => $ this ->getPayPalDetails ()
230
199
];
231
200
232
201
$ transaction = Transaction::factory ($ attributes );
202
+ $ result = new Successful (['transaction ' => $ transaction ]);
233
203
234
- return $ transaction ;
204
+ return $ result ;
235
205
}
236
206
237
207
/**
238
- * Get PayPal transaction details
208
+ * Gets PayPal transaction details.
209
+ *
239
210
* @return PayPalDetails
240
211
*/
241
- private function getPayPalDetails ()
212
+ private function getPayPalDetails (): PayPalDetails
242
213
{
243
214
$ attributes = [
244
- 'token ' => ' rc39al ' ,
245
- 'payerEmail ' => ' john.doe@example.com '
215
+ 'token ' => self :: $ token ,
216
+ 'payerEmail ' => self :: $ payerEmail
246
217
];
247
218
248
219
$ details = new PayPalDetails ($ attributes );
0 commit comments