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