6
6
7
7
namespace Magento \Checkout \Test \Unit \Model ;
8
8
9
+ /**
10
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11
+ */
9
12
class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
10
13
{
11
14
/**
@@ -33,6 +36,11 @@ class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
33
36
*/
34
37
private $ loggerMock ;
35
38
39
+ /**
40
+ * @var \PHPUnit_Framework_MockObject_MockObject
41
+ */
42
+ private $ cartRepositoryMock ;
43
+
36
44
protected function setUp ()
37
45
{
38
46
$ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -45,13 +53,14 @@ protected function setUp()
45
53
$ this ->cartManagementMock = $ this ->getMock (\Magento \Quote \Api \CartManagementInterface::class);
46
54
47
55
$ this ->loggerMock = $ this ->getMock (\Psr \Log \LoggerInterface::class);
48
-
56
+ $ this -> cartRepositoryMock = $ this -> getMockBuilder (\ Magento \ Quote \ Api \CartRepositoryInterface::class)-> getMock ();
49
57
$ this ->model = $ objectManager ->getObject (
50
58
\Magento \Checkout \Model \PaymentInformationManagement::class,
51
59
[
52
60
'billingAddressManagement ' => $ this ->billingAddressManagementMock ,
53
61
'paymentMethodManagement ' => $ this ->paymentMethodManagementMock ,
54
- 'cartManagement ' => $ this ->cartManagementMock
62
+ 'cartManagement ' => $ this ->cartManagementMock ,
63
+ 'cartRepository ' => $ this ->cartRepositoryMock
55
64
]
56
65
);
57
66
$ objectManager ->setBackwardCompatibleProperty ($ this ->model , 'logger ' , $ this ->loggerMock );
@@ -64,9 +73,7 @@ public function testSavePaymentInformationAndPlaceOrder()
64
73
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
65
74
$ billingAddressMock = $ this ->getMock (\Magento \Quote \Api \Data \AddressInterface::class);
66
75
67
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
68
- ->method ('assign ' )
69
- ->with ($ cartId , $ billingAddressMock );
76
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
70
77
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
71
78
$ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->with ($ cartId )->willReturn ($ orderId );
72
79
@@ -86,9 +93,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
86
93
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
87
94
$ billingAddressMock = $ this ->getMock (\Magento \Quote \Api \Data \AddressInterface::class);
88
95
89
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
90
- ->method ('assign ' )
91
- ->with ($ cartId , $ billingAddressMock );
96
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
92
97
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
93
98
$ exception = new \Exception (__ ('DB exception ' ));
94
99
$ this ->loggerMock ->expects ($ this ->once ())->method ('critical ' );
@@ -103,7 +108,6 @@ public function testSavePaymentInformationAndPlaceOrderIfBillingAddressNotExist(
103
108
$ orderId = 200 ;
104
109
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
105
110
106
- $ this ->billingAddressManagementMock ->expects ($ this ->never ())->method ('assign ' );
107
111
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
108
112
$ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->with ($ cartId )->willReturn ($ orderId );
109
113
@@ -119,9 +123,7 @@ public function testSavePaymentInformation()
119
123
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
120
124
$ billingAddressMock = $ this ->getMock (\Magento \Quote \Api \Data \AddressInterface::class);
121
125
122
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
123
- ->method ('assign ' )
124
- ->with ($ cartId , $ billingAddressMock );
126
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
125
127
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
126
128
127
129
$ this ->assertTrue ($ this ->model ->savePaymentInformation ($ cartId , $ paymentMock , $ billingAddressMock ));
@@ -132,7 +134,6 @@ public function testSavePaymentInformationWithoutBillingAddress()
132
134
$ cartId = 100 ;
133
135
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
134
136
135
- $ this ->billingAddressManagementMock ->expects ($ this ->never ())->method ('assign ' );
136
137
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
137
138
138
139
$ this ->assertTrue ($ this ->model ->savePaymentInformation ($ cartId , $ paymentMock ));
@@ -148,9 +149,8 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
148
149
$ paymentMock = $ this ->getMock (\Magento \Quote \Api \Data \PaymentInterface::class);
149
150
$ billingAddressMock = $ this ->getMock (\Magento \Quote \Api \Data \AddressInterface::class);
150
151
151
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
152
- ->method ('assign ' )
153
- ->with ($ cartId , $ billingAddressMock );
152
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
153
+
154
154
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
155
155
$ phrase = new \Magento \Framework \Phrase (__ ('DB exception ' ));
156
156
$ exception = new \Magento \Framework \Exception \LocalizedException ($ phrase );
@@ -159,4 +159,31 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
159
159
160
160
$ this ->model ->savePaymentInformationAndPlaceOrder ($ cartId , $ paymentMock , $ billingAddressMock );
161
161
}
162
+
163
+ /**
164
+ * @param int $cartId
165
+ * @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
166
+ */
167
+ private function getMockForAssignBillingAddress ($ cartId , $ billingAddressMock )
168
+ {
169
+ $ billingAddressId = 1 ;
170
+ $ quoteMock = $ this ->getMock (\Magento \Quote \Model \Quote::class, [], [], '' , false );
171
+ $ quoteBillingAddress = $ this ->getMock (\Magento \Quote \Model \Quote \Address::class, [], [], '' , false );
172
+ $ quoteShippingAddress = $ this ->getMock (
173
+ \Magento \Quote \Model \Quote \Address::class,
174
+ ['setLimitCarrier ' , 'getShippingMethod ' ],
175
+ [],
176
+ '' ,
177
+ false
178
+ );
179
+ $ this ->cartRepositoryMock ->expects ($ this ->any ())->method ('getActive ' )->with ($ cartId )->willReturn ($ quoteMock );
180
+ $ quoteMock ->expects ($ this ->once ())->method ('getBillingAddress ' )->willReturn ($ quoteBillingAddress );
181
+ $ quoteMock ->expects ($ this ->once ())->method ('getShippingAddress ' )->willReturn ($ quoteShippingAddress );
182
+ $ quoteBillingAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ billingAddressId );
183
+ $ quoteMock ->expects ($ this ->once ())->method ('removeAddress ' )->with ($ billingAddressId );
184
+ $ quoteMock ->expects ($ this ->once ())->method ('setBillingAddress ' )->with ($ billingAddressMock );
185
+ $ quoteMock ->expects ($ this ->once ())->method ('setDataChanges ' )->willReturnSelf ();
186
+ $ quoteShippingAddress ->expects ($ this ->any ())->method ('getShippingMethod ' )->willReturn ('flatrate_flatrate ' );
187
+ $ quoteShippingAddress ->expects ($ this ->once ())->method ('setLimitCarrier ' )->with ('flatrate ' )->willReturnSelf ();
188
+ }
162
189
}
0 commit comments