3
3
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Paypal \Test \Unit \Model \Payflow \Service \Request ;
7
8
8
9
use Magento \Framework \Math \Random ;
9
10
use Magento \Framework \DataObject ;
10
11
use Magento \Framework \UrlInterface ;
11
12
use Magento \Paypal \Model \Payflow \Service \Request \SecureToken ;
12
13
use Magento \Paypal \Model \Payflow \Transparent ;
14
+ use Magento \Paypal \Model \PayflowConfig ;
15
+ use Magento \Quote \Model \Quote ;
13
16
14
17
/**
15
18
* Test class for \Magento\Paypal\Model\Payflow\Service\Request\SecureToken
@@ -36,11 +39,16 @@ class SecureTokenTest extends \PHPUnit_Framework_TestCase
36
39
*/
37
40
protected $ url ;
38
41
42
+ /** @var DataObject */
43
+ private $ request ;
44
+
39
45
protected function setUp ()
40
46
{
41
- $ this ->url = $ this ->getMock ('Magento\Framework\UrlInterface ' , [], [], '' , false );
42
- $ this ->mathRandom = $ this ->getMock ('Magento\Framework\Math\Random ' , [], [], '' , false );
43
- $ this ->transparent = $ this ->getMock ('Magento\Paypal\Model\Payflow\Transparent ' , [], [], '' , false );
47
+ $ this ->url = $ this ->buildMock (UrlInterface::class);
48
+ $ this ->mathRandom = $ this ->buildMock (Random::class);
49
+ $ this ->request = new DataObject ();
50
+
51
+ $ this ->transparent = $ this ->buildPaymentService ($ this ->request );
44
52
45
53
$ this ->model = new SecureToken (
46
54
$ this ->url ,
@@ -49,34 +57,93 @@ protected function setUp()
49
57
);
50
58
}
51
59
60
+ /**
61
+ * Test Request Token
62
+ */
52
63
public function testRequestToken ()
53
64
{
54
- $ request = new DataObject ();
55
65
$ secureTokenID = 'Sdj46hDokds09c8k2klaGJdKLl032ekR ' ;
56
66
57
- $ this ->transparent ->expects ($ this ->once ())
58
- ->method ('buildBasicRequest ' )
59
- ->willReturn ($ request );
60
- $ this ->transparent ->expects ($ this ->once ())
61
- ->method ('fillCustomerContacts ' );
62
- $ this ->transparent ->expects ($ this ->once ())
63
- ->method ('getConfig ' )
64
- ->willReturn ($ this ->getMock ('Magento\Paypal\Model\PayflowConfig ' , [], [], '' , false ));
65
- $ this ->transparent ->expects ($ this ->once ())
66
- ->method ('postRequest ' )
67
- ->willReturn (new DataObject ());
68
-
69
67
$ this ->mathRandom ->expects ($ this ->once ())
70
68
->method ('getUniqueHash ' )
71
69
->willReturn ($ secureTokenID );
72
70
73
71
$ this ->url ->expects ($ this ->exactly (3 ))
74
72
->method ('getUrl ' );
75
73
76
- $ quote = $ this ->getMock ('Magento\Quote\Model\Quote ' , [], [], '' , false );
74
+ /** @var Quote | \PHPUnit_Framework_MockObject_MockObject $quote */
75
+ $ quote = $ this ->buildMock (Quote::class);
76
+
77
+ $ this ->model ->requestToken ($ quote );
78
+
79
+ $ this ->assertEquals ($ secureTokenID , $ this ->request ->getSecuretokenid ());
80
+ }
81
+
82
+ /**
83
+ * Test request currency
84
+ *
85
+ * @dataProvider currencyProvider
86
+ * @param $currency
87
+ */
88
+ public function testCurrency ($ currency )
89
+ {
90
+ /** @var Quote | \PHPUnit_Framework_MockObject_MockObject $quote */
91
+ $ quote = $ this ->buildMock (Quote::class, ['getBaseCurrencyCode ' ]);
92
+ $ quote ->expects (self ::atLeastOnce ())
93
+ ->method ('getBaseCurrencyCode ' )
94
+ ->willReturn ($ currency );
77
95
78
96
$ this ->model ->requestToken ($ quote );
79
97
80
- $ this ->assertEquals ($ secureTokenID , $ request ->getSecuretokenid ());
98
+ $ this ->assertEquals ($ currency , $ this ->request ->getCurrency ());
99
+ }
100
+
101
+ /**
102
+ * Builds default mock object
103
+ *
104
+ * @param string $class className
105
+ * @param array|null $methods
106
+ * @return \PHPUnit_Framework_MockObject_MockObject
107
+ */
108
+ private function buildMock ($ class , array $ methods = [])
109
+ {
110
+ return $ this ->getMockBuilder ($ class )
111
+ ->disableOriginalConstructor ()
112
+ ->setMethods ($ methods )
113
+ ->getMock ();
114
+ }
115
+
116
+ /**
117
+ * Creates payment method service
118
+ *
119
+ * @param DataObject $request
120
+ * @return Transparent | \PHPUnit_Framework_MockObject_MockObject
121
+ */
122
+ private function buildPaymentService (DataObject $ request )
123
+ {
124
+ $ service = $ this ->buildMock (Transparent::class);
125
+ $ service ->expects ($ this ->once ())
126
+ ->method ('buildBasicRequest ' )
127
+ ->willReturn ($ request );
128
+ $ service ->expects ($ this ->once ())
129
+ ->method ('fillCustomerContacts ' );
130
+ $ service ->expects ($ this ->once ())
131
+ ->method ('getConfig ' )
132
+ ->willReturn ($ this ->buildMock (PayflowConfig::class));
133
+ $ service ->expects ($ this ->once ())
134
+ ->method ('postRequest ' )
135
+ ->willReturn (new DataObject ());
136
+
137
+ return $ service ;
138
+ }
139
+
140
+ /**
141
+ * DataProvider for testing currency
142
+ *
143
+ * @return array
144
+ */
145
+ public function currencyProvider ()
146
+ {
147
+ return [['GBP ' ], [null ], ['USD ' ]];
81
148
}
82
149
}
0 commit comments