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