1313use Magento \CheckoutAgreements \Model \AgreementsProvider ;
1414use Magento \CheckoutAgreements \Model \Api \SearchCriteria \ActiveStoreAgreementsFilter ;
1515use Magento \CheckoutAgreements \Model \Checkout \Plugin \GuestValidation ;
16+ use Magento \CheckoutAgreements \Model \EmulateStore ;
1617use Magento \Framework \Api \SearchCriteria ;
1718use Magento \Framework \App \Config \ScopeConfigInterface ;
19+ use Magento \Quote \Api \CartRepositoryInterface ;
1820use Magento \Quote \Api \Data \AddressInterface ;
1921use Magento \Quote \Api \Data \PaymentExtension ;
22+ use Magento \Quote \Api \Data \PaymentExtensionInterface ;
2023use Magento \Quote \Api \Data \PaymentInterface ;
24+ use Magento \Quote \Model \MaskedQuoteIdToQuoteId ;
25+ use Magento \Quote \Model \Quote ;
2126use Magento \Store \Model \ScopeInterface ;
2227use PHPUnit \Framework \MockObject \MockObject ;
2328use PHPUnit \Framework \MockObject \RuntimeException ;
@@ -73,6 +78,26 @@ class GuestValidationTest extends TestCase
7378 */
7479 private $ agreementsFilterMock ;
7580
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+
76101 protected function setUp (): void
77102 {
78103 $ this ->agreementsValidatorMock = $ this ->getMockForAbstractClass (AgreementsValidatorInterface::class);
@@ -87,18 +112,38 @@ protected function setUp(): void
87112 $ this ->agreementsFilterMock = $ this ->createMock (
88113 ActiveStoreAgreementsFilter::class
89114 );
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 );
90131
91132 $ this ->model = new GuestValidation (
92133 $ this ->agreementsValidatorMock ,
93134 $ this ->scopeConfigMock ,
94135 $ this ->checkoutAgreementsListMock ,
95- $ this ->agreementsFilterMock
136+ $ this ->agreementsFilterMock ,
137+ $ this ->maskedQuoteIdToQuoteIdMock ,
138+ $ this ->cartRepositoryMock ,
139+ $ this ->emulateStoreMock
96140 );
97141 }
98142
99143 public function testBeforeSavePaymentInformationAndPlaceOrder ()
100144 {
101- $ cartId = '100 ' ;
145+ $ storeId = 1 ;
146+ $ cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce ' ;
102147 $ email = 'email@example.com ' ;
103148 $ agreements = [1 , 2 , 3 ];
104149 $ this ->scopeConfigMock
@@ -115,10 +160,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
115160 ->with ($ searchCriteriaMock )
116161 ->willReturn ([1 ]);
117162 $ this ->extensionAttributesMock ->expects ($ this ->once ())->method ('getAgreementIds ' )->willReturn ($ agreements );
118- $ this ->agreementsValidatorMock ->expects ($ this ->once ())->method ('isValid ' )->with ($ agreements )->willReturn (true );
119163 $ this ->paymentMock ->expects (static ::atLeastOnce ())
120164 ->method ('getExtensionAttributes ' )
121165 ->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 );
122172 $ this ->model ->beforeSavePaymentInformationAndPlaceOrder (
123173 $ this ->subjectMock ,
124174 $ cartId ,
@@ -131,9 +181,16 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
131181 public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid ()
132182 {
133183 $ this ->expectException ('Magento\Framework\Exception\CouldNotSaveException ' );
134- $ cartId = 100 ;
184+ $ storeId = 1 ;
185+ $ cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce ' ;
135186 $ email = 'email@example.com ' ;
136187 $ 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 );
137194 $ this ->scopeConfigMock
138195 ->expects ($ this ->once ())
139196 ->method ('isSetFlag ' )
@@ -148,7 +205,6 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
148205 ->with ($ searchCriteriaMock )
149206 ->willReturn ([1 ]);
150207 $ this ->extensionAttributesMock ->expects ($ this ->once ())->method ('getAgreementIds ' )->willReturn ($ agreements );
151- $ this ->agreementsValidatorMock ->expects ($ this ->once ())->method ('isValid ' )->with ($ agreements )->willReturn (false );
152208 $ this ->paymentMock ->expects (static ::atLeastOnce ())
153209 ->method ('getExtensionAttributes ' )
154210 ->willReturn ($ this ->extensionAttributesMock );
@@ -172,9 +228,10 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
172228 */
173229 private function getPaymentExtension (): MockObject
174230 {
175- $ mockBuilder = $ this ->getMockBuilder (PaymentExtension::class);
231+ $ mockBuilder = $ this ->getMockBuilder (PaymentExtensionInterface::class)
232+ ->disableOriginalConstructor ();
176233 try {
177- $ mockBuilder ->addMethods (['getAgreementIds ' ]);
234+ $ mockBuilder ->onlyMethods (['getAgreementIds ' , ' setAgreementIds ' ]);
178235 } catch (RuntimeException $ e ) {
179236 // Payment extension already generated.
180237 }
0 commit comments