Skip to content

Commit 84e2b49

Browse files
committed
ACP2E-2850: fix static unit tests
1 parent 9a9b5ac commit 84e2b49

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/GuestValidationTest.php

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
use Magento\CheckoutAgreements\Model\AgreementsProvider;
1414
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1515
use Magento\CheckoutAgreements\Model\Checkout\Plugin\GuestValidation;
16+
use Magento\CheckoutAgreements\Model\EmulateStore;
1617
use Magento\Framework\Api\SearchCriteria;
1718
use Magento\Framework\App\Config\ScopeConfigInterface;
19+
use Magento\Quote\Api\CartRepositoryInterface;
1820
use Magento\Quote\Api\Data\AddressInterface;
1921
use Magento\Quote\Api\Data\PaymentExtension;
22+
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2023
use Magento\Quote\Api\Data\PaymentInterface;
24+
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
25+
use Magento\Quote\Model\Quote;
2126
use Magento\Store\Model\ScopeInterface;
2227
use PHPUnit\Framework\MockObject\MockObject;
2328
use 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
}

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/ValidationTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
use Magento\CheckoutAgreements\Model\AgreementsProvider;
1414
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1515
use Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation;
16+
use Magento\CheckoutAgreements\Model\EmulateStore;
1617
use Magento\Framework\Api\SearchCriteria;
1718
use Magento\Framework\App\Config\ScopeConfigInterface;
1819
use Magento\Quote\Api\CartRepositoryInterface;
1920
use Magento\Quote\Api\Data\AddressInterface;
2021
use Magento\Quote\Api\Data\PaymentExtension;
22+
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2123
use Magento\Quote\Api\Data\PaymentInterface;
2224
use Magento\Quote\Model\Quote;
2325
use Magento\Store\Model\ScopeInterface;
@@ -86,6 +88,11 @@ class ValidationTest extends TestCase
8688
*/
8789
private $quoteRepositoryMock;
8890

91+
/**
92+
* @var EmulateStore|MockObject
93+
*/
94+
private EmulateStore|MockObject $emulateStoreMock;
95+
8996
protected function setUp(): void
9097
{
9198
$this->agreementsValidatorMock = $this->getMockForAbstractClass(AgreementsValidatorInterface::class);
@@ -94,6 +101,7 @@ protected function setUp(): void
94101
$this->addressMock = $this->getMockForAbstractClass(AddressInterface::class);
95102
$this->quoteMock = $this->getMockBuilder(Quote::class)
96103
->addMethods(['getIsMultiShipping'])
104+
->onlyMethods(['getStoreId'])
97105
->disableOriginalConstructor()
98106
->getMock();
99107
$this->quoteRepositoryMock = $this->getMockForAbstractClass(CartRepositoryInterface::class);
@@ -105,20 +113,37 @@ protected function setUp(): void
105113
$this->agreementsFilterMock = $this->createMock(
106114
ActiveStoreAgreementsFilter::class
107115
);
116+
$this->emulateStoreMock = $this->createMock(EmulateStore::class);
117+
118+
$storeId = 1;
119+
$this->quoteMock->expects($this->once())
120+
->method('getStoreId')
121+
->willReturn($storeId);
122+
$this->quoteRepositoryMock->expects($this->once())
123+
->method('get')
124+
->willReturn($this->quoteMock);
108125

109126
$this->model = new Validation(
110127
$this->agreementsValidatorMock,
111128
$this->scopeConfigMock,
112129
$this->checkoutAgreementsListMock,
113130
$this->agreementsFilterMock,
114-
$this->quoteRepositoryMock
131+
$this->quoteRepositoryMock,
132+
$this->emulateStoreMock
115133
);
116134
}
117135

118136
public function testBeforeSavePaymentInformationAndPlaceOrder()
119137
{
138+
$storeId = 1;
120139
$cartId = 100;
121140
$agreements = [1, 2, 3];
141+
$this->emulateStoreMock->expects($this->once())
142+
->method('execute')
143+
->with($storeId, $this->callback(function ($callback) {
144+
return is_callable($callback);
145+
}))
146+
->willReturn(true);
122147
$this->scopeConfigMock
123148
->expects($this->once())
124149
->method('isSetFlag')
@@ -140,7 +165,6 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
140165
->with($searchCriteriaMock)
141166
->willReturn([1]);
142167
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
143-
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
144168
$this->paymentMock->expects(static::atLeastOnce())
145169
->method('getExtensionAttributes')
146170
->willReturn($this->extensionAttributesMock);
@@ -155,8 +179,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
155179
public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid()
156180
{
157181
$this->expectException('Magento\Framework\Exception\CouldNotSaveException');
182+
$storeId = 1;
158183
$cartId = 100;
159184
$agreements = [1, 2, 3];
185+
$this->emulateStoreMock->expects($this->once())
186+
->method('execute')
187+
->with($storeId, $this->callback(function ($callback) {
188+
return is_callable($callback);
189+
}))
190+
->willReturn(false);
160191
$this->scopeConfigMock
161192
->expects($this->once())
162193
->method('isSetFlag')
@@ -178,7 +209,6 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
178209
->with($searchCriteriaMock)
179210
->willReturn([1]);
180211
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
181-
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
182212
$this->paymentMock->expects(static::atLeastOnce())
183213
->method('getExtensionAttributes')
184214
->willReturn($this->extensionAttributesMock);
@@ -201,9 +231,10 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
201231
*/
202232
private function getPaymentExtension(): MockObject
203233
{
204-
$mockBuilder = $this->getMockBuilder(PaymentExtension::class);
234+
$mockBuilder = $this->getMockBuilder(PaymentExtensionInterface::class)
235+
->disableOriginalConstructor();
205236
try {
206-
$mockBuilder->addMethods(['getAgreementIds']);
237+
$mockBuilder->onlyMethods(['getAgreementIds', 'setAgreementIds']);
207238
} catch (RuntimeException $e) {
208239
// Payment extension already generated.
209240
}

0 commit comments

Comments
 (0)