13
13
use Magento \CheckoutAgreements \Model \AgreementsProvider ;
14
14
use Magento \CheckoutAgreements \Model \Api \SearchCriteria \ActiveStoreAgreementsFilter ;
15
15
use Magento \CheckoutAgreements \Model \Checkout \Plugin \GuestValidation ;
16
+ use Magento \CheckoutAgreements \Model \EmulateStore ;
16
17
use Magento \Framework \Api \SearchCriteria ;
17
18
use Magento \Framework \App \Config \ScopeConfigInterface ;
19
+ use Magento \Quote \Api \CartRepositoryInterface ;
18
20
use Magento \Quote \Api \Data \AddressInterface ;
19
21
use Magento \Quote \Api \Data \PaymentExtension ;
22
+ use Magento \Quote \Api \Data \PaymentExtensionInterface ;
20
23
use Magento \Quote \Api \Data \PaymentInterface ;
24
+ use Magento \Quote \Model \MaskedQuoteIdToQuoteId ;
25
+ use Magento \Quote \Model \Quote ;
21
26
use Magento \Store \Model \ScopeInterface ;
22
27
use PHPUnit \Framework \MockObject \MockObject ;
23
28
use PHPUnit \Framework \MockObject \RuntimeException ;
@@ -73,6 +78,26 @@ class GuestValidationTest extends TestCase
73
78
*/
74
79
private $ agreementsFilterMock ;
75
80
81
+ /**
82
+ * @var Quote|MockObject
83
+ */
84
+ private Quote |MockObject $ quoteMock ;
85
+
86
+ /**
87
+ * @var MaskedQuoteIdToQuoteId|MockObject
88
+ */
89
+ private MaskedQuoteIdToQuoteId |MockObject $ maskedQuoteIdToQuoteIdMock ;
90
+
91
+ /**
92
+ * @var CartRepositoryInterface|MockObject
93
+ */
94
+ private CartRepositoryInterface |MockObject $ cartRepositoryMock ;
95
+
96
+ /**
97
+ * @var EmulateStore|MockObject
98
+ */
99
+ private EmulateStore |MockObject $ emulateStoreMock ;
100
+
76
101
protected function setUp (): void
77
102
{
78
103
$ this ->agreementsValidatorMock = $ this ->getMockForAbstractClass (AgreementsValidatorInterface::class);
@@ -87,18 +112,38 @@ protected function setUp(): void
87
112
$ this ->agreementsFilterMock = $ this ->createMock (
88
113
ActiveStoreAgreementsFilter::class
89
114
);
115
+ $ this ->quoteMock = $ this ->createMock (Quote::class);
116
+ $ this ->maskedQuoteIdToQuoteIdMock = $ this ->createMock (MaskedQuoteIdToQuoteId::class);
117
+ $ this ->cartRepositoryMock = $ this ->createMock (CartRepositoryInterface::class);
118
+ $ this ->emulateStoreMock = $ this ->createMock (EmulateStore::class);
119
+
120
+ $ storeId = 1 ;
121
+ $ this ->quoteMock ->expects ($ this ->once ())
122
+ ->method ('getStoreId ' )
123
+ ->willReturn ($ storeId );
124
+ $ this ->maskedQuoteIdToQuoteIdMock ->expects ($ this ->once ())
125
+ ->method ('execute ' )
126
+ ->with ('0CQwCntNHR4yN9P5PUAzbxatvDvBXOce ' )
127
+ ->willReturn (1000 );
128
+ $ this ->cartRepositoryMock ->expects ($ this ->once ())
129
+ ->method ('get ' )
130
+ ->willReturn ($ this ->quoteMock );
90
131
91
132
$ this ->model = new GuestValidation (
92
133
$ this ->agreementsValidatorMock ,
93
134
$ this ->scopeConfigMock ,
94
135
$ this ->checkoutAgreementsListMock ,
95
- $ this ->agreementsFilterMock
136
+ $ this ->agreementsFilterMock ,
137
+ $ this ->maskedQuoteIdToQuoteIdMock ,
138
+ $ this ->cartRepositoryMock ,
139
+ $ this ->emulateStoreMock
96
140
);
97
141
}
98
142
99
143
public function testBeforeSavePaymentInformationAndPlaceOrder ()
100
144
{
101
- $ cartId = '100 ' ;
145
+ $ storeId = 1 ;
146
+ $ cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce ' ;
102
147
$ email = 'email@example.com ' ;
103
148
$ agreements = [1 , 2 , 3 ];
104
149
$ this ->scopeConfigMock
@@ -115,10 +160,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
115
160
->with ($ searchCriteriaMock )
116
161
->willReturn ([1 ]);
117
162
$ this ->extensionAttributesMock ->expects ($ this ->once ())->method ('getAgreementIds ' )->willReturn ($ agreements );
118
- $ this ->agreementsValidatorMock ->expects ($ this ->once ())->method ('isValid ' )->with ($ agreements )->willReturn (true );
119
163
$ this ->paymentMock ->expects (static ::atLeastOnce ())
120
164
->method ('getExtensionAttributes ' )
121
165
->willReturn ($ this ->extensionAttributesMock );
166
+ $ this ->emulateStoreMock ->expects ($ this ->once ())
167
+ ->method ('execute ' )
168
+ ->with ($ storeId , $ this ->callback (function ($ callback ) {
169
+ return is_callable ($ callback );
170
+ }))
171
+ ->willReturn (true );
122
172
$ this ->model ->beforeSavePaymentInformationAndPlaceOrder (
123
173
$ this ->subjectMock ,
124
174
$ cartId ,
@@ -131,9 +181,16 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
131
181
public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid ()
132
182
{
133
183
$ this ->expectException ('Magento\Framework\Exception\CouldNotSaveException ' );
134
- $ cartId = 100 ;
184
+ $ storeId = 1 ;
185
+ $ cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce ' ;
135
186
$ email = 'email@example.com ' ;
136
187
$ agreements = [1 , 2 , 3 ];
188
+ $ this ->emulateStoreMock ->expects ($ this ->once ())
189
+ ->method ('execute ' )
190
+ ->with ($ storeId , $ this ->callback (function ($ callback ) {
191
+ return is_callable ($ callback );
192
+ }))
193
+ ->willReturn (false );
137
194
$ this ->scopeConfigMock
138
195
->expects ($ this ->once ())
139
196
->method ('isSetFlag ' )
@@ -148,7 +205,6 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
148
205
->with ($ searchCriteriaMock )
149
206
->willReturn ([1 ]);
150
207
$ this ->extensionAttributesMock ->expects ($ this ->once ())->method ('getAgreementIds ' )->willReturn ($ agreements );
151
- $ this ->agreementsValidatorMock ->expects ($ this ->once ())->method ('isValid ' )->with ($ agreements )->willReturn (false );
152
208
$ this ->paymentMock ->expects (static ::atLeastOnce ())
153
209
->method ('getExtensionAttributes ' )
154
210
->willReturn ($ this ->extensionAttributesMock );
@@ -172,9 +228,10 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
172
228
*/
173
229
private function getPaymentExtension (): MockObject
174
230
{
175
- $ mockBuilder = $ this ->getMockBuilder (PaymentExtension::class);
231
+ $ mockBuilder = $ this ->getMockBuilder (PaymentExtensionInterface::class)
232
+ ->disableOriginalConstructor ();
176
233
try {
177
- $ mockBuilder ->addMethods (['getAgreementIds ' ]);
234
+ $ mockBuilder ->onlyMethods (['getAgreementIds ' , ' setAgreementIds ' ]);
178
235
} catch (RuntimeException $ e ) {
179
236
// Payment extension already generated.
180
237
}
0 commit comments